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?

  • 2
    for and while loops are definitely the wrong tools for this job. Try <file_list.txt xargs cmd – don_crissti Nov 28 '17 at 12:56
  • 1
    Is it about editing the content of file_list.txt or the content of every file whose path is referenced in file_list.txt? Or their name (rename the files referenced in file_list.txt)? – Stéphane Chazelas Nov 28 '17 at 13:04
  • Its about editting the content of every file in the file_list.txt – Raghunath Choudhary Nov 28 '17 at 13:24
  • What do you mean by remove trailing blank spaces and extra newline characters? Do you mean you want to remove whitespace characters at the end of every line and remove empty lines? – Stéphane Chazelas Nov 28 '17 at 14:07
  • Yes. Exactly. And I want to do that for every file in file_list.txt. (The list of file names is present in file_list.txt) – Raghunath Choudhary Nov 28 '17 at 14:10
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 /./' --

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.