Shell 删除空格并用空格更改符号
我有一个有几千行的文件。例如:Shell 删除空格并用空格更改符号,shell,unix,awk,Shell,Unix,Awk,我有一个有几千行的文件。例如: 205.188.213.249:193.219. 43. 13:193.219. 62.126: 82 76: 12: 2868 193.219.168. 18:206.126. 6. 38: 62. 40.103.217: 4 82: 11: 701 193.219. 52.163:217. 44.206.181: 62. 40.103.217: 76 82:
205.188.213.249:193.219. 43. 13:193.219. 62.126: 82 76: 12: 2868
193.219.168. 18:206.126. 6. 38: 62. 40.103.217: 4 82: 11: 701
193.219. 52.163:217. 44.206.181: 62. 40.103.217: 76 82: 9: 531
193.219. 74.113:195. 22.175. 2:193.219. 62.126: 0 76: 29: 10396
193.219. 32. 13:195. 34. 96. 3: 62. 40.103.217: 0 82: 1: 227
我需要:1) 删除IP地址号之间的空格
2) 要将此符号更改为空格,请执行以下操作: 应该是这样的:
205.188.213.249 193.219.43.13 193.219.62.126 82 76 12 2868
我正在使用以下命令进行尝试:
tr-d'|tr':''我的文件
,但不起作用。请尝试以下内容(使用提供的示例和GNUawk
进行测试)
说明:此处使用gsub
进行全局替换。首先用空格全局替换冒号,然后用空格全局替换点。最后用单个空间全局替换空间。然后通过提及1
让awk
知道如何打印编辑/未编辑的行
awk
使用regexp/condition方法,然后执行操作。如果任何regexp/条件为TRUE,则将执行其旁边提到的操作。在这种情况下,我没有提到任何操作,所以默认情况下会打印当前行
关于gsub
来自awk
的人工输入:
gsub(r, s [, t]) For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions. If t is not supplied, use $0. An & in the
replacement text is replaced with the text that was actually matched. Use \& to get a literal &. (This must be typed as "\\&"; see GAWK: Effective AWK Programming for a fuller dis-
cussion of the rules for &’s and backslashes in the replacement text of sub(), gsub(), and gensub().)
EDIT1:立即添加
sed
解决方案(在GNUsed
中测试)
请尝试以下内容(使用提供的样本和GNU
awk
进行测试)
说明:此处使用gsub
进行全局替换。首先用空格全局替换冒号,然后用空格全局替换点。最后用单个空间全局替换空间。然后通过提及1
让awk
知道如何打印编辑/未编辑的行
awk
使用regexp/condition方法,然后执行操作。如果任何regexp/条件为TRUE,则将执行其旁边提到的操作。在这种情况下,我没有提到任何操作,所以默认情况下会打印当前行
关于gsub
来自awk
的人工输入:
gsub(r, s [, t]) For each substring matching the regular expression r in the string t, substitute the string s, and return the number of substitutions. If t is not supplied, use $0. An & in the
replacement text is replaced with the text that was actually matched. Use \& to get a literal &. (This must be typed as "\\&"; see GAWK: Effective AWK Programming for a fuller dis-
cussion of the rules for &’s and backslashes in the replacement text of sub(), gsub(), and gensub().)
EDIT1:立即添加
sed
解决方案(在GNUsed
中测试)
试试这个:
awk -F: '{for (i=1;i<=NF;i++) if (split($i,tmp,".") == 4) gsub(/[[:space:]]/,"",$i); else {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$i);gsub(/[[:space:]]+/,OFS,$i);}}1' myfile
awk-F:'{for(i=1;i试试这个:
awk -F: '{for (i=1;i<=NF;i++) if (split($i,tmp,".") == 4) gsub(/[[:space:]]/,"",$i); else {gsub(/^[[:space:]]+|[[:space:]]+$/,"",$i);gsub(/[[:space:]]+/,OFS,$i);}}1' myfile
awk-F:'{for(i=1;i使用Perl
perl -lpe 's/\.\s+(?=\d+)/./g;s/:/ /g; s/\s+/ /g ' input_file
使用您给定的输入
$ cat edgaras.txt
205.188.213.249:193.219. 43. 13:193.219. 62.126: 82 76: 12: 2868
193.219.168. 18:206.126. 6. 38: 62. 40.103.217: 4 82: 11: 701
193.219. 52.163:217. 44.206.181: 62. 40.103.217: 76 82: 9: 531
193.219. 74.113:195. 22.175. 2:193.219. 62.126: 0 76: 29: 10396
193.219. 32. 13:195. 34. 96. 3: 62. 40.103.217: 0 82: 1: 227
$ perl -lpe 's/\.\s+(?=\d+)/./g;s/:/ /g; s/\s+/ /g ' edgaras.txt
205.188.213.249 193.219.43.13 193.219.62.126 82 76 12 2868
193.219.168.18 206.126.6.38 62.40.103.217 4 82 11 701
193.219.52.163 217.44.206.181 62.40.103.217 76 82 9 531
193.219.74.113 195.22.175.2 193.219.62.126 0 76 29 10396
193.219.32.13 195.34.96.3 62.40.103.217 0 82 1 227
$
使用Perl
perl -lpe 's/\.\s+(?=\d+)/./g;s/:/ /g; s/\s+/ /g ' input_file
使用您给定的输入
$ cat edgaras.txt
205.188.213.249:193.219. 43. 13:193.219. 62.126: 82 76: 12: 2868
193.219.168. 18:206.126. 6. 38: 62. 40.103.217: 4 82: 11: 701
193.219. 52.163:217. 44.206.181: 62. 40.103.217: 76 82: 9: 531
193.219. 74.113:195. 22.175. 2:193.219. 62.126: 0 76: 29: 10396
193.219. 32. 13:195. 34. 96. 3: 62. 40.103.217: 0 82: 1: 227
$ perl -lpe 's/\.\s+(?=\d+)/./g;s/:/ /g; s/\s+/ /g ' edgaras.txt
205.188.213.249 193.219.43.13 193.219.62.126 82 76 12 2868
193.219.168.18 206.126.6.38 62.40.103.217 4 82 11 701
193.219.52.163 217.44.206.181 62.40.103.217 76 82 9 531
193.219.74.113 195.22.175.2 193.219.62.126 0 76 29 10396
193.219.32.13 195.34.96.3 62.40.103.217 0 82 1 227
$
您可以尝试使用sed:
sed '
:A
s/\([^[:blank:]]*\)[[:blank:]]\(.*\.[^\.]*\)/\1\2/
tA
s/:\?[[:blank:]]\{1,\}\|:/ /g
' infile
您可以尝试使用sed:
sed '
:A
s/\([^[:blank:]]*\)[[:blank:]]\(.*\.[^\.]*\)/\1\2/
tA
s/:\?[[:blank:]]\{1,\}\|:/ /g
' infile
它不是tr-d'| tr':''文件
它是tr-d''文件| tr':''
是一个管道,输入必须转到第一个命令。或者你可以命令未找到请发布完整的错误消息。它是bash:blablabla:command未找到
。未找到哪个命令?有我的命令:>文件名tr-d“”| tr':“”
并且有错误消息:文件名:第2462行:193.219.169.108:208.254.:未找到命令
它不是文件名tr-d“”| tr':“”
但它不是tr-d“”| tr':“”“文件
它是tr-d''文件
。
。
是一个管道,必须输入到第一个命令。或者,您可以command not found请发布完整的错误消息。它是bash:blabla:command not found
。找不到哪个命令?有我的命令:fileName tr-d'| tr':''
,还有错误消息:fileName:line 2462:193.219.169.108:208.254.:command not found
这不是fileName tr-d''|tr':''
,而是@EdgarasAlšauskas,欢迎您。在这个伟大的网站上继续学习和分享。@EdgarasAlšauskas,欢迎您。在这个伟大的网站上继续学习和分享吧。