Need help to solve a loop issue in awk
- Inicie sesión ou rexístrese para enviar comentarios
I got a problem when I tried to use loop in awk
The input file is
I|A;B;C|a;b;c|1;2;3
II|D;E;F|d;e;f|4;5;6
III|G;H;I|g;h;i|7;8;9
The working awk script is
awk 'BEGIN{FS="|"}
{
printf("%s ", $1);
split($2, arr, ";");
printf("%s ", arr[2]);
split($3, arr, ";");
printf("%s ", arr[2]);
split($4, arr, ";");
printf("%s ", arr[2]);
printf("\n");
}' inputfile
But the following script does not work:
awk 'BEGIN{FS="|"}
{
printf("%s ", $1);
for (i = 1; i <= 3; i++)
{
split($i, arr, ";");
printf("%s ", arr[2]);
}
printf("\n");
}' inputfile
Can anyone help me fix the issue in the second script?
Your loop should go from i = 2 to i = 4. Not from i = 1 to i = 3.
But there are more elegant solutions. I like this one:
tr ';|' ' \n' < inputfile | awk '{ if (NR % 4 == 1) printf $0; else if (NR % 4 == 0) print " " $2; else printf " " $2 }'
Here, 'tr' turns ';' into ' ' and '|' into a new line. In this way, there is no need to redefine FS and to use split. To know where the AWK program is in the original data, it tests the record number modulo 4 (NR % 4). Notice that I removed the space at the end of every line of your output because I doubt you actually want it. If you do, concatenate " " at the end of the string printed in the case NR % 4 == 0.
Every line of inputfile exactly has three '|'s, right? Otherwise, my solution needs to start with "cut -d '|' -f -4 inputfile" to only keep the first four fields.
You answered my question, and gave a better solution!
Thank you very much!
- Inicie sesión ou rexístrese para enviar comentarios