Awk : an other way more elegant to do it ? See inside?

75 Views Asked by At

I wrote a awk program which give me the result i needed. Finding any line beginning with 130AB : if the 2nd field contain data, move it on the 9th field

My input file :

130DD2532||1|||1|
130AB00100501||20|17112023|17112023||N|||0||
130DD2532||1|||1|
130AB00100502||20|17112023|17112023||N|||0||
130DD2532||1|||1|
130AB00100112||20|17112023|17112023||N|||0||
130DD2532||1|||1|
130AB00100113|00100113|20|17112023|17112023||N|||0||

See the last line

My program file prog.awk:

/^130AB/ { if ($2==""){print $1"|"$2"|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"$9"|"$10"|"$11"|"$12"|"$13"|"$14"|"$15} else { print $1"|""|"$3"|"$4"|"$5"|"$6"|"$7"|"$8"|"$2"|"$10"|"$11"|"$12"|"$13"|"$14"|"$15}; next; }; 
{ print; }

My command line :

awk -F "|" -f prog.awk myFile.txt > newFile.txt

The result : newFile.txt

130DD2532||1|||1|
130AB00100501||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100502||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100112||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100113||20|17112023|17112023||N||00100113|0|||||

It work fine but does my prog.awk should'nt be more clear ? :-)

Thanks for you help

Abou Ilyès

2

There are 2 best solutions below

0
markp-fuso On BEST ANSWER

One alternative to the current code:

$ cat prog.awk
BEGIN    { FS=OFS="|" }                                 # define input/output field delimiters
/^130AB/ { if ($2 != "")            { $9=$2; $2="" }    # if 2nd field not blank then redefine 2nd/9th fields
           for (i=NF+1; i<=15; i++)   $i=""             # add additional blank fields until we have a total of 15 fields
         }
1                                                       # print current line

NOTE: I've moved the -F"|" into a BEGIN block so this will change the command line invocation, eg:

$ awk -f prog.awk myFile.txt > newFile.txt

This generates:

$ cat newFile.txt
130DD2532||1|||1|
130AB00100501||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100502||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100112||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100113||20|17112023|17112023||N||00100113|0|||||
0
Ed Morton On

You don't need to test for $2 being populated or not because it is isn't then assigning the empty $9 to the value of the empty $2 changes nothing.

This does what you asked for in the textual description (moves $2 to $9):

$ awk 'BEGIN{FS=OFS="|"} /^130AB/{$9=$2; $2=""} 1' file
130DD2532||1|||1|
130AB00100501||20|17112023|17112023||N|||0||
130DD2532||1|||1|
130AB00100502||20|17112023|17112023||N|||0||
130DD2532||1|||1|
130AB00100112||20|17112023|17112023||N|||0||
130DD2532||1|||1|
130AB00100113||20|17112023|17112023||N||00100113|0||

while this produces the expected output you show (moves $2 to $9 and adds empty fields up to field number 15):

$ awk 'BEGIN{FS=OFS="|"} /^130AB/{$9=$2; $2=""; $15=$15} 1' file
130DD2532||1|||1|
130AB00100501||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100502||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100112||20|17112023|17112023||N|||0|||||
130DD2532||1|||1|
130AB00100113||20|17112023|17112023||N||00100113|0|||||