Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

move lines into a file by number of columns using awk

Tags:

linux

bash

sed

awk

I have a sample file with '||o||' as field separator.

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

I want to move the lines with only 1 field in 1.txt and those having more than 1 field in not_1.txt. I am using the following command:

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

The problem is that it is moving not the original lines but the replaced ones.

The output I am getting is (not_1.txt):

[email protected]||o||srScSG2C5tg=||o||bnm
[email protected]||o||4sQVj09gpls=

1.txt:

ngas
[email protected]

As you can see the original lines are modified. I don't want to modify the lines. Any help would be highly appreciated.

like image 391
Bhawan Avatar asked Mar 26 '18 06:03

Bhawan


2 Answers

Awk solution:

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

Results:

$ 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||
like image 188
RomanPerekhrest Avatar answered Sep 20 '22 04:09

RomanPerekhrest


Following awk may help you on same.

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

Adding a non-one liner form of solution too now.

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

Explanation: Adding explanation for above code too now.

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.
like image 23
RavinderSingh13 Avatar answered Sep 22 '22 04:09

RavinderSingh13