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?
Asked
Active
Viewed 334 times
0
1 Answers
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 /./' --

Stéphane Chazelas
- 544,893
for
andwhile
loops are definitely the wrong tools for this job. Try<file_list.txt xargs cmd
– don_crissti Nov 28 '17 at 12:56file_list.txt
or the content of every file whose path is referenced infile_list.txt
? Or their name (rename the files referenced infile_list.txt
)? – Stéphane Chazelas Nov 28 '17 at 13:04file_list.txt
? Is that one per line? Do file names contain whitespace or newline characters or single-quote, double-quote or backslash? – Stéphane Chazelas Nov 28 '17 at 14:13Apologies if my question is silly – Raghunath Choudhary Nov 28 '17 at 14:17
perl
was first initially written on Unix 30 years ago, and is available for most Unix-likes and even non-Unix-likes like Microsoft OSes. It's generally installed by default on unix-like systems. – Stéphane Chazelas Nov 28 '17 at 14:23