ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
If you look closely at the output of your command you will see that it prints the first line and then prints the whole file; so the first line gets printed twice. To suppress this behaviour you should use the -n switch:
Code:
sed -n '1p' list1 > list2
Alternatively you could just delete all lines from line two to the end:
ghostdog74, could you explain please, what should i put in the ... bit?
He's just showing you an overall structure---you would put it whatever SED syntax is required for your task. This thread already includes several ways of solving the same problem---with SED or otherwise.
Actually, in this case the for loop is a compliment for the sed command, if I was to use sed to pull the original filename to rename, instead of the $i varibale I would have to write the sed command diferently. So in this case the for loop is not redundant.
He's just showing you an overall structure---you would put it whatever SED syntax is required for your task. This thread already includes several ways of solving the same problem---with SED or otherwise.
I agree, there are many ways to skin a cat, and no one way is correct when is comes to linux.
Actually, in this case the for loop is a compliment for the sed command, if I was to use sed to pull the original filename to rename, instead of the $i varibale I would have to write the sed command diferently. So in this case the for loop is not redundant.
Firstly your script won't work because you forgot to quote "$(ls)" and "$i".
Code:
#!/bin/bash
for i in "$(ls)"; do
echo "$i" |sed "s/[[:space:]]/./g" |sed "s/^/ren $1 /g" >> script_to_rename.sh
done
Secondly the only place you use the variable "$i" is to echo it to sed. As ghostdog74 points out if you use 'ls *' you don't have to echo "$i" so the loop is redundant:
Code:
ls * | sed -e "s/ /./g" -e "s/^/ren $1/g" >> script_to_rename.sh
Firstly your script won't work because you forgot to quote "$(ls)" and "$i".
Code:
#!/bin/bash
for i in "$(ls)"; do
echo "$i" |sed "s/[[:space:]]/./g" |sed "s/^/ren $1 /g" >> script_to_rename.sh
done
You are correct, newbie mistake to forget those quotes....
Quote:
Originally Posted by jozyba
Secondly the only place you use the variable "$i" is to echo it to sed. As ghostdog74 points out if you use 'ls *' you don't have to echo "$i" so the loop is redundant:
Code:
ls * | sed -e "s/ /./g" -e "s/^/ren $1/g" >> script_to_rename.sh
That's a new one for me, I didn't realise the $1, $2, $3 etc, variables could be passed to sed that way. Thanks.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.