Which is better between a for loop and while loop for Korn shell

Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
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?
ksh
 |Â
show 3 more comments
up vote
0
down vote
favorite
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?
ksh
2
forandwhileloops 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 offile_list.txtor 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: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
 |Â
show 3 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
ksh
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?
ksh
edited Nov 28 '17 at 13:05
Stéphane Chazelas
282k53520854
282k53520854
asked Nov 28 '17 at 12:52
Raghunath Choudhary
588
588
2
forandwhileloops 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 offile_list.txtor 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: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
 |Â
show 3 more comments
2
forandwhileloops 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 offile_list.txtor 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: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
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
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
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
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
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
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
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
 |Â
show 3 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
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 /./' --
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
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 /./' --
add a comment |Â
up vote
2
down vote
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 /./' --
add a comment |Â
up vote
2
down vote
up vote
2
down vote
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 /./' --
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 /./' --
edited Nov 28 '17 at 14:11
answered Nov 28 '17 at 13:18
Stéphane Chazelas
282k53520854
282k53520854
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f407499%2fwhich-is-better-between-a-for-loop-and-while-loop-for-korn-shell%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
2
forandwhileloops 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.txtor 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: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