1

I am using sed within a while loop to remove the trailing blank spaces from a file containing a list of files. The blank spaces are being removed. But I'm getting the message sed:no input files.

Following is the while loop I am using:

while IFS= read -r line; 
        do
            echo "tester: $line"
            sed -i 's/\s*$//' $line ;
        done < file_list.txt
7
  • 5
    Blank line at the end of file_list.txt? Commented Nov 28, 2017 at 10:46
  • Yes. the piece of code is working. But I am getting messages of 'sed:no input files' Commented Nov 28, 2017 at 11:25
  • No, what do you see if you do tail -1 file_list.txt? If you don't see anything except an empty line then you need to delete that empty line.
    – B Layer
    Commented Nov 28, 2017 at 12:00
  • I see output of a blank line Commented Nov 28, 2017 at 12:43
  • Let me explain deeper. I have a file file_list.txt which contains a list of filenames to be processed. And for all the files present in the file_list.txt I have to remove the trailing blank spaces and extra newline characters. I am using while loop to read the file names and process using sed command. Commented Nov 28, 2017 at 12:43

2 Answers 2

2

As Ipor and glenn have both said, based on your report from B Layer's comment regarding tail -1 file_list.txt, since there is a blank line at the end of that file, when the while loop reads that blank line, the $line variable gets assigned an empty value, leaving no filename for sed to process.

$ cat i
file1
file2

$ while IFS= read -r line; do printf -- "-->%s<--\n" "$line"; done < i
-->file1<--
-->file2<--
--><--

The fix here is to delete that trailing blank line from file_list.txt, or wrap a test around your actual processing to test for the existence of the file:

# do ...
if [ -f "$line" ]
then
  # process file
fi
# done ...

and always quote your variables!

0
0

Had same issue, for me it was because I did not pass the input file to sed CMD

Example:

boy=$1
boy2=$2
boy3=$3
while IFS="," read -r col1 col2
do
 if [ "$col1" == "oke" ]; then

    sed -i "s/$col2/$boy/g" test.txt
  fi
done < test.txt

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .