使用awk

时间:2018-03-26 06:49:17

标签: linux bash awk sed

我有一个带有'|| o ||'的示例文件作为字段分隔符。

www.google.org||o||srScSG2C5tg=||o||bngwq
farhansingla.it||o||4sQVj09gpls=||o||
ngascash||o||||o||
ms-bronze.com.br||o||||o||

我想移动1.txt中只有1个字段的行和not_1.txt中只有1个字段的字段。我使用以下命令:

sed  's/\(||o||\)\+$//g' sample.txt  | awk -F '[|][|]o[|][|]' '{if (NF == 1) print > "1.txt"; else print > "not_1.txt" }'

问题在于它不是原始线而是移动线。

我得到的输出是(not_1.txt):

td@the-end.org||o||srScSG2C5tg=||o||bnm
erba01@tiscali.it||o||4sQVj09gpls=

的1.txt:

ngas
ms-inside@bol.com.br

如您所见,原始线条已被修改。我不想修改这些行。 任何帮助都将受到高度赞赏。

2 个答案:

答案 0 :(得分:3)

Awk 解决方案:

awk -F '[|][|]o[|][|]' \
'{ 
     c = 0; 
     for (i=1; i<=NF; i++) if ($i != "") c++;
     print > (c == 1? "1" : "not_1")".txt"  
 }' sample.txt

结果:

$ head 1.txt not_1.txt 
==> 1.txt <==
ngascash||o||||o||
ms-bronze.com.br||o||||o||

==> not_1.txt <==
www.google.org||o||srScSG2C5tg=||o||bngwq
farhansingla.it||o||4sQVj09gpls=||o||

答案 1 :(得分:3)

关注awk可能对您有帮助。

awk -F'\\|\\|o\\|\\|' '{for(i=1;i<=NF;i++){count=$i?++count:count};if(count==1){print > "1_field_only"};if(count>1){print > "not_1_field"};count=""}'  Input_file

现在也添加非单线形式的解决方案。

awk -F'\\|\\|o\\|\\|' '
{
  for(i=1;i<=NF;i++){ count=$i?++count:count };
  if(count==1)      { print > "1_field_only" };
  if(count>1)       { print > "not_1_field"  };
  count=""
}
'   Input_file

说明: 现在也为上述代码添加说明。

awk -F'\\|\\|o\\|\\|' '                          ##Setting field separator as ||o|| here and escaping the | here to take it literal character here.
{
  for(i=1;i<=NF;i++){ count=$i?++count:count };  ##Starting a for loop to traverse through all the fields here, increasing variable count value if a field is NOT null.
  if(count==1)      { print > "1_field_only" };  ##Checking if count value is 1 it means fields are only 1 in line so printing current line into 1_field_only file.
  if(count>1)       { print > "not_1_field"  };  ##Checking if count is more than 1 so printing current line into output file named not_1_field file here.
  count=""                                       ##Nullifying the variable count here.
}
' Input_file                                     ##Mentioning Input_file name here.