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 320 times
0
-
2`for` and `while` loops are [definitely the wrong tools for this job](https://unix.stackexchange.com/q/169716/22142). Try `
– don_crissti Nov 28 '17 at 12:56 -
1Is 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
-
So, does the xargs-based approach in my answer work for you? How are file names delimited in your `file_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:13
-
No. The filenames do not contain any whitespace or any special characters. Isnt the xargs approach mentioned in your post for perl ? I am on unix platform. Will it be same for unix too ? Apologies if my question is silly – Raghunath Choudhary Nov 28 '17 at 14:17
-
Yes, `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
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
- 522,931
- 91
- 1,010
- 1,501