There is a crashcourse_test.txt file with the following content:
# cat crashcourse_test.txt
1001|Anvils R Us|123 Main Street|Southfield|MI|48075|USA
1002|LT Supplies|500 Park Street|Anytown|OH|44333|USA
1003|ACME|555 High Street|Los Angeles|CA|90046|USA
1004|Furball Inc.|1000 5th Avenue|New York|NY|11111|USA
1005|Jet Set|42 Galaxy Road|London|NULL|N16 6PS|England
1006|Jouets Et Ours|1 Rue Amusement|Paris|NULL|45678|France
#
The content of the while_read.sh script is as follows. The purpose is to use the above file content as standard input, pass it into the while read loop, and replace the "USA" string in each line with "XXXXXXXXX" through the sed command:
#!/bin/bash
while read line
do
sed 's/USA/XXXXXXXXX/'
done < crashcourse_test.txt
The output result is:
# ./while_read.sh
1002|LT Supplies|500 Park Street|Anytown|OH|44333|XXXXXXXXX
1003|ACME|555 High Street|Los Angeles|CA|90046|XXXXXXXXX
1004|Furball Inc.|1000 5th Avenue|New York|NY|11111|XXXXXXXXX
1005|Jet Set|42 Galaxy Road|London|NULL|N16 6PS|England
1006|Jouets Et Ours|1 Rue Amusement|Paris|NULL|45678|France
#
You can see that the line 1001 contains the string "USA" ( happens to be the first line in the file ), which meets the replacement requirements, but the result is that this line is not output.
I know the way to output it correctly, which can be achieved by using echo $line | sed 's/USA/XXXXXXXXX/'
.
But I want to know the reason why only the first line of output is missed. Where does the first line go after entering the while read as standard input?
First of all, your usage of
sed
may be wrong.Its parameter is a file, your variable
line
is not used.From only outputting a 3 it means that the loop is only executed 1 time. So your file redirect
<crashcourse_test.txt
is actually being used as input toread
and input tosed
, andsed
will Everything afterread
is used as input, so you did not use the first line of input.