0

I found on stackoverflow this awk about printing from match (row4) ( link ), combined with the feature to process several files due to resetting the match indicatior each time a new file is being opened. Input file1, file2 and file 3 look like this each time:

row1;something;in;this;row;;;;;;;
row2;something;in;this;row;;;;;;;
row3;something;in;this;row;;;;;;;
row4;don't;need;to;match;the;whole;line,;
row5;something;in;this;row;;;;;;;
row6;something;in;this;row;;;;;;;
row7;something;in;this;row;;;;;;;
row8;something;in;this;row;;;;;;;
row9;something;in;this;row;;;;;;;
row10;something;in;this;row;;;;;;;

Output should be this:

works something in this row
row6 something in this row
row8 something in this row
row9 something in this row
row10 something in this row
works something in this row
row6 something in this row
row8 something in this row
row9 something in this row
row10 something in this row
works something in this row
row6 something in this row
row8 something in this row
row9 something in this row
row10 something in this row

In the original thread here link it says one can simply add

... $1=="row5"{$6=$5; $5=""} ...

in order to 'move' fields if match condition is true to the command:

awk 'FNR==1{p=0} p; /row4/{p=1} ' file1 file2

But where do I have to add this? I tried all combinations I saw and it did not work. In my case I also want to delete a line after match "row 7" and and change field $1 if it is == "row 5"

if ($1=="row7") {next};
if ($1=="row5") {$1="works"}

In my case I put the rules into a file: test.awk and save it for later use. This I call then in this way:

   gawk -f test.awk *.csv 

( file1.csv, file2.csv and file3.csv). The two new conditions are still being ignored also if I put these before the p rule:

FNR==1     {p=0} 
 $1=="row5" {$5="works"}
 $1=="row7" {next}
 p; 
/row4/{p=1} 
vandyke
  • 11
  • 4
  • I'm confused about what you're trying to do - a sample of your input files and the corresponding output would make things more clear. – Tom Fenech May 15 '17 at 08:48
  • Mr. Fenech, there is the original command that is working for me. This is exemplary. I want to add two if-conditions, for example: IF ($1=="row5") {$6=$5; $5=""}; IF ($1=="row7") {next}; and other commands. But I do not know how to add them. In my case, if I put them before, after or in the middle of the orginal awk it does not work. – vandyke May 15 '17 at 10:59
  • My _guess_ is that the conditions should go before you evaluate the condition `p`, but without sample input and the desired output I can't be sure. If you provide us with a reproducible test case then I'm sure someone can solve your problem. – Tom Fenech May 15 '17 at 12:17

1 Answers1

1
awk -F';' 'FNR==1     {p=0} 
           $1=="row5" {$6=$5; $5=""}
           $1=="row7" {next}
           p; 
           /row4/     {p=1}' file1 file2

p is short for p!=0{print}, the conditions you want to apply should be before printing, otherwise they won't be visible.

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • I see Karakfa. I tried this too before I asked but it still does not work. – vandyke May 15 '17 at 13:01
  • In my case I put the commands into a file: test.awk and save it for later use. This I call then in this way: gawk -f test.awk *.csv ( file1.csv, file2.csv and file3.csv). The two new conditions are still being ignored. – vandyke May 15 '17 at 13:11
  • you are missing the field delimiter, add `-F';'` option. – karakfa May 15 '17 at 13:13
  • Shame on me :( I had a tab as delimter but named the files *.csv. Since I am later working with files that use ';' delimter I changed this right now and it works. Thank you Karakfa for your time! – vandyke May 15 '17 at 13:23