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
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|||||