The awk command loses the first line of while

1. There is a text test.log, content that is:

1 an A
2 b B
3 c C

2. Execute while to get normal output

cat test.log |while read item;do echo $item;done
1 an A
2 b B
3 c C

3. But executing while+awk loses the first line

cat test.log |while read item;do awk "{print $1"A"}";done
2A
3A
< hr > < H2 > question: why did you lose the first row of data? < / H2 > < H2 > second question: how do I get the following output? < / H2 >
1A
2A
3A
Sep.16,2021

@ Yujiaao has answered your second question, that is, how to get the correct output. Because awk can loop through all delimited lines (usually separated by \ n ), it is no longer necessary to use while to read.

therefore has the answer to your question, why is there a line missing:

you used the read command in while, which reads from standard input, so it reads the first line into the item variable. When awk does not follow the file parameters, it also reads the contents from the standard input, so awk reads and outputs the rest of the content.

in addition, if you use awk, there is no need to use cat to read the contents of the file, and then pipe it to awk; because awk can read the file directly:

awk ' {print $1"A"}' test.log

While is not required for

awk

cat test.log |awk ' {print $1"A"}'
1A
2A
3A
Menu