LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 05-11-2009, 01:53 PM   #1
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Rep: Reputation: 15
need help with using sed command twice on one file


ok so what i have is a list of words 6 to a line


aaaa aaaa aaa aaa aaa aaaa im trying to use the sed command to insert a comma in between each word and at the end of the last one

aaaa, aaa, aaaa, aaaa, aaaa, aaaa,

the last line of the variable does not have 6 words
what i have so far is this
Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]\+/,/g s/$\/,/g'`
it dosent work though i originally started off with

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]\+/,/g'
this puts a comma inbetween them but takes out the space and dosent put a comma at the end of the line..

basicaly what i needed to know is how i could you multiple sed commands to fix this
 
Old 05-11-2009, 01:56 PM   #2
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
whoops i made a mistake above!! i have the space figured out i just needed to know how to use multiple sed commands on one line

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g'`
this gives me

a, abed, accede, acceded, ace, aced
ad, add, added, b, baa, baaed
babe, bad, bade, be, bead, beaded
bed, bedded, bee, beef, beefed, c
cab, cabbed, cad, cede, ceded, d
dab, dabbed, dad, dead, deaf, deb


what i need is to use another sed to search for the end of the line and add a comma... but i dont know how to add another sed command to the one i have
 
Old 05-11-2009, 02:07 PM   #3
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
ive tried this

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g'
-e 's/$/,/g'`
but i get an error

./word1.sh: line 27: -e: command not found
 
Old 05-11-2009, 02:08 PM   #4
hemlockz
LQ Newbie
 
Registered: Sep 2005
Posts: 13

Rep: Reputation: 0
Can you use '[ ]|$' to match a space or end of line?

Quote:
Originally Posted by xskycamefalling View Post
whoops i made a mistake above!! i have the space figured out i just needed to know how to use multiple sed commands on one line

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g'`
this gives me

a, abed, accede, acceded, ace, aced
ad, add, added, b, baa, baaed
babe, bad, bade, be, bead, beaded
bed, bedded, bee, beef, beefed, c
cab, cabbed, cad, cede, ceded, d
dab, dabbed, dad, dead, deaf, deb


what i need is to use another sed to search for the end of the line and add a comma... but i dont know how to add another sed command to the one i have
 
Old 05-11-2009, 02:21 PM   #5
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
wouldent that match one or the other?
i need it to match both
im new at this sorry if i sound super retarded lol



Quote:
Originally Posted by hemlockz View Post
Can you use '[ ]|$' to match a space or end of line?
 
Old 05-11-2009, 02:26 PM   #6
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by hemlockz View Post
Can you use '[ ]|$' to match a space or end of line?
also what would the syntax have to look like wouldent ther have to be () around it? if i remember correctly dont u need the () to use the | "or"
ive tried just adding it and it dosent work

ive also tried adding ([ ]|$) that dosent work either

thanks for the response though!
 
Old 05-11-2009, 02:46 PM   #7
hemlockz
LQ Newbie
 
Registered: Sep 2005
Posts: 13

Rep: Reputation: 0
hmmm.. if they are all words you could do this?

sed 's\/([A-Za-z]*/)\&,\g'

puts a comma after every group of letters.

Quote:
Originally Posted by xskycamefalling View Post
also what would the syntax have to look like wouldent ther have to be () around it? if i remember correctly dont u need the () to use the | "or"
ive tried just adding it and it dosent work

ive also tried adding ([ ]|$) that dosent work either

thanks for the response though!
 
Old 05-11-2009, 02:49 PM   #8
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
ok so i figured out a way to do what i was asking but im still wondering if its possible to use the sed command twice on the one line
this is what i figured out though

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g'`
done=`echo "$words" | sed -e 's/$/,/g'`
echo "$done"
 
Old 05-11-2009, 02:55 PM   #9
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by hemlockz View Post
hmmm.. if they are all words you could do this?

sed 's\/([A-Za-z]*/)\&,\g'

puts a comma after every group of letters.
good call that would also solve the problem... but the question still lies could i if i wanted to use multiple sed commands?
pointless i guess but still

thanks for your help hemlockz
 
Old 05-11-2009, 02:57 PM   #10
hemlockz
LQ Newbie
 
Registered: Sep 2005
Posts: 13

Rep: Reputation: 0
You can pipe it together. It is considered sloppy to do it that way because it will run 2 sed processes when one could have been used. But sometimes it is not possible to write it in one expression so you have to pipe two sed commands together... is that what you mean?

Quote:
Originally Posted by xskycamefalling View Post
ok so i figured out a way to do what i was asking but im still wondering if its possible to use the sed command twice on the one line
this is what i figured out though

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g'`
done=`echo "$words" | sed -e 's/$/,/g'`
echo "$done"
 
Old 05-11-2009, 03:14 PM   #11
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by hemlockz View Post
You can pipe it together. It is considered sloppy to do it that way because it will run 2 sed processes when one could have been used. But sometimes it is not possible to write it in one expression so you have to pipe two sed commands together... is that what you mean?

ya kinda and i actually tried that but it was only giving me the output of the last sed process...

also ur syntax was inccorect on the above


Quote:
sed 's\/([A-Za-z]*/)\&,\g'
it should look like

Code:
sed -e 's/\([a-zA-Z]*\)/&,/g'`
but the idea was right and it does work

thanks again
 
Old 05-11-2009, 03:37 PM   #12
hemlockz
LQ Newbie
 
Registered: Sep 2005
Posts: 13

Rep: Reputation: 0
Cool, no worries I am just a newb at this and happened to be working with sed today too. Good learning experience. Thats weird that the pipe didn't work for you... I jsut did:

cat words|sed 's_ _, _g'|sed 's_$_,_'

(I prefer to use _ as the delimiter because I am too dyslexic with forwardslash and backslash!)
 
Old 05-12-2009, 02:40 AM   #13
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
Quote:
Originally Posted by xskycamefalling View Post
what i have so far is this
Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]\+/,/g s/$\/,/g'`
it dosent work...
sed uses semicolon for compound commands:
Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]\+/,/g;s/$\/,/g'`
Quote:
Originally Posted by xskycamefalling View Post
ive tried this

Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g'
-e 's/$/,/g'`
but i get an error

./word1.sh: line 27: -e: command not found
This fails because you put a new line before the -e so it starts a new command, you can use a backslash to escape the newline:
Code:
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed -e 's/[ ]/, /g' \
-e 's/$/,/g'`
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
using sed to insert line into file and overwrite the current file jadeddog Programming 3 06-11-2009 07:14 PM
sed: print section of file from string to end of file samyboy Linux - Newbie 4 02-26-2008 07:23 AM
Inserting file contents into Sed Command ewingtux Programming 4 11-19-2007 06:59 PM
Command interpretation issue in Sed script file angel115 Programming 9 04-21-2006 07:26 PM
Sed command in file not working lbauer Programming 5 04-06-2005 12:31 PM

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

All times are GMT -5. The time now is 01:25 PM.

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