linux - Regarding the mixed use of while read and sed, sed eats the first line of standard input
習慣沉默
習慣沉默 2017-06-10 09:48:43
0
1
1051

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?

習慣沉默
習慣沉默

reply all(1)
淡淡烟草味

First of all, your usage of sed may be wrong.

sed [OPTION]... {script-only-if-no-other-script} [input-file]...

Its parameter is a file, your variable line is not used.

while read line
do
    echo 3
    sed 's/USA/XXXXXXX/'
done < crashcourse_test.txt

输出:
3
1002|LT Supplies|500 Park Street|Anytown|OH|44333|XXXXXXX
1003|ACME|555 High Street|Los Angeles|CA|90046|XXXXXXX
1004|Furball Inc.|1000 5th Avenue|New York|NY|11111|XXXXXXX
1005|Jet Set|42 Galaxy Road|London|NULL|N16 6PS|England
1006|Jouets Et Ours|1 Rue Amusement|Paris|NULL|45678|France

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 to read and input to sed, and sed will Everything after read is used as input, so you did not use the first line of input.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!