0

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. How can I achieve this. Using a while loop will be better or a for loop?

1 Answers1

2

If it's about editing the content of the file_list.txt, you'd use a text editing tool, not a shell loop, like:

sed 's/[[:space:]]*$//; # remove trailing whitespace
     /./!d; # remove empty lines' < file_list.txt > new_file_list.txt

Or for in-place editing:

perl -nli.back -e 's/\s+$//;print if /./' file_list.txt

(remove .back if you don't need backup copies of the original).

If file_list.txt contains a list of files and it's those files whose content you want to edit, then again a loop is not ideal unless you do want to run one editing command per file.

If the content of file_list.txt is compatible with xargs input format, that is where file names are whitespace (including newline) separated and double quotes, single quotes or backslash can escape whitespace and each other (allowing any character), then you can just do:

xargs < file_list.txt perl -nli.back -e 's/\s+$//;print if /./' --