LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 12-29-2007, 06:09 PM   #1
paperplane
Member
 
Registered: Dec 2007
Location: england
Distribution: slackware 11
Posts: 35

Rep: Reputation: 15
confused with sed


hi,

i want sed to send only the first line of a text file to another text file, but it sends them all. this is the commandline ive tried

'cat list1 | sed -e '1p' > list2'

ive had a look at some documentation on sed on the net but i cant figure it out. can anyone help with this?

Last edited by paperplane; 12-29-2007 at 06:11 PM.
 
Old 12-29-2007, 06:25 PM   #2
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
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:
Code:
sed '2,$d' list1 > list2

Last edited by jozyba; 12-29-2007 at 06:46 PM.
 
Old 12-29-2007, 07:15 PM   #3
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,120

Rep: Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120Reputation: 4120
And, as implied by jozyba, the "cat" is redundant.

And if it's a big file,you might want to quit after that first line rather than spin through the entire file. Try this
Code:
sed -n '1p;1q' list1 > list2

Last edited by syg00; 12-29-2007 at 07:19 PM.
 
Old 12-29-2007, 10:24 PM   #4
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
alternative:
Code:
head -1 file > newfile
 
Old 12-30-2007, 09:39 AM   #5
paperplane
Member
 
Registered: Dec 2007
Location: england
Distribution: slackware 11
Posts: 35

Original Poster
Rep: Reputation: 15
thanks guys, in the end i went with 'sed q', which emulates "head -1".

by the way, can the sed command rename files, or just work on the text within them?

i need to rename alot of .gif files which contain whitespace, and substitute it with a dot (.)

if sed cant do this, does anyone know any other way of doing this?

Last edited by paperplane; 12-30-2007 at 10:47 AM.
 
Old 12-30-2007, 11:31 AM   #6
gerbenny
LQ Newbie
 
Registered: Dec 2007
Location: Prince Edward Island
Distribution: Debian Etch
Posts: 17

Rep: Reputation: 0
That's really easy one, here's a quick bash loop to ls over a whole directory, and output a file with a full list of rename commands:

Code:
#!/bin/bash
for i in $( ls ); do
  echo $i |sed "s/[[:space:]]/./g" |sed "s/^/ren $1 /g" >> script_to_rename.sh
done
chmod 777 script_to_rename.sh
Then just run the newly created script_to_rename.sh file.

Last edited by gerbenny; 12-30-2007 at 11:49 AM.
 
Old 12-30-2007, 12:10 PM   #7
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by gerbenny View Post
Code:
#!/bin/bash
for i in $( ls ); do
  echo $i |sed "s/[[:space:]]/./g" |sed "s/^/ren $1 /g" >> script_to_rename.sh
done
chmod 777 script_to_rename.sh
that's a redundant for loop.
Essentially you can just do this:
Code:
ls * | sed .......| >> file
 
Old 12-30-2007, 12:30 PM   #8
paperplane
Member
 
Registered: Dec 2007
Location: england
Distribution: slackware 11
Posts: 35

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by ghostdog74 View Post
that's a redundant for loop.
Essentially you can just do this:
Code:
ls * | sed .......| >> file
ghostdog74, could you explain please, what should i put in the ... bit?

Last edited by paperplane; 12-30-2007 at 12:31 PM.
 
Old 12-30-2007, 01:50 PM   #9
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by paperplane View Post
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.

Here is my favorite SED tutorial: http://www.grymoire.com/Unix/Sed.html
 
Old 12-30-2007, 01:55 PM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ghostdog74 View Post
that's a redundant for loop.
Essentially you can just do this:
Code:
ls * | sed .......| >> file
I'm not sure that you would use a pipe followed by a redirection operator????

i.e.: ls *|sed ...... >>file
 
Old 12-30-2007, 03:58 PM   #11
gerbenny
LQ Newbie
 
Registered: Dec 2007
Location: Prince Edward Island
Distribution: Debian Etch
Posts: 17

Rep: Reputation: 0
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.
 
Old 12-30-2007, 04:01 PM   #12
gerbenny
LQ Newbie
 
Registered: Dec 2007
Location: Prince Edward Island
Distribution: Debian Etch
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by pixellany View Post
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.
 
Old 12-30-2007, 04:18 PM   #13
paperplane
Member
 
Registered: Dec 2007
Location: england
Distribution: slackware 11
Posts: 35

Original Poster
Rep: Reputation: 15
thanks all and thanks for the info on sed, ive just discovered it this weekend and it looks to be a very useful command.
 
Old 12-30-2007, 04:36 PM   #14
jozyba
Member
 
Registered: Sep 2007
Distribution: Debian Etch, Lenny, Lenny/Sid
Posts: 31

Rep: Reputation: 15
Quote:
Originally Posted by gerbenny View Post
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
 
Old 12-30-2007, 05:54 PM   #15
gerbenny
LQ Newbie
 
Registered: Dec 2007
Location: Prince Edward Island
Distribution: Debian Etch
Posts: 17

Rep: Reputation: 0
Quote:
Originally Posted by jozyba View Post
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 View Post
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.

When you stop learning, it's time for a funeral.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Confused nay Very Confused chrystlenight SUSE / openSUSE 3 08-28-2007 05:57 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM
[sed] "Advanced" sed question(s) G00fy Programming 2 03-20-2006 12:34 AM
sed and escaping & in something like: echo $y | sed 's/&/_/g' prx Programming 7 02-03-2005 11:00 PM
Insert character into a line with sed? & variables in sed? jago25_98 Programming 5 03-11-2004 06:12 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:00 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration