Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
Replacing a space " " with a new line?
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
tdm
Tux's lil' helper
Tux's lil' helper


Joined: 06 May 2002
Posts: 117
Location: The Netherlands

PostPosted: Mon Jan 13, 2003 12:22 pm    Post subject: Replacing a space " " with a new line? Reply with quote

I'm creating a little bash script and for this script I need the output of the 'groups' command, as every group on a new line.

This works perfectly if I execute it from the commandline:
Code:
bash-2.05b$ groups | sed 's/ /\
> /g'
users
wheel
patrick


But when I put this line in a executable #!/bin/bash file it doesn't work:
File:
Code:
#!/bin/bash
`groups | sed 's/ /\
/g'`

And when I execute the file I get this:
Code:
bash-2.05b$ ./usergroups
./usergroups: line 2: unexpected EOF while looking for matching ``'
./usergroups: line 4: syntax error: unexpected end of file


What am I doing wrong? As you can see it works on the commandline..
Back to top
View user's profile Send private message
zleepy
n00b
n00b


Joined: 19 Dec 2002
Posts: 19
Location: /sweden/ludvika

PostPosted: Mon Jan 13, 2003 12:53 pm    Post subject: Reply with quote

This works.

Code:
#!/bin/bash
groups | sed 's/ /\n/g'

\n = new line
Back to top
View user's profile Send private message
praxim
Tux's lil' helper
Tux's lil' helper


Joined: 14 May 2002
Posts: 75
Location: State College, PA

PostPosted: Mon Jan 13, 2003 2:45 pm    Post subject: Reply with quote

Instead of using a regexp, it might be a bit faster to use tr:
groups | tr ' ' '\n'
Back to top
View user's profile Send private message
tdm
Tux's lil' helper
Tux's lil' helper


Joined: 06 May 2002
Posts: 117
Location: The Netherlands

PostPosted: Mon Jan 13, 2003 3:58 pm    Post subject: Reply with quote

Thanks for the help guys, praxim your solution seems to be the easiest so I take your tip :)

One thing, I want to put the output in a variable, I do that with:
Code:
#!/bin/bash
mygroups=`groups | tr ' ' '\n'`
echo $mygroups;

But when I execute it, I still see the groups like you see them when you just execute the groups command. But when I don't but the output in a variable it works find:
Code:
#!/bin/bash
groups | tr ' ' '\n'

Am I doing something wrong?

Thanks :)
Back to top
View user's profile Send private message
idl
Retired Dev
Retired Dev


Joined: 24 Dec 2002
Posts: 1728
Location: Nottingham, UK

PostPosted: Mon Jan 13, 2003 4:42 pm    Post subject: Reply with quote

Hi, sorry i dont realy know any bash but here it is in perl:

Code:
perl -e '$groups = `groups`; $groups =~ s/ /\n/g; print $groups;'
Back to top
View user's profile Send private message
Naan Yaar
Bodhisattva
Bodhisattva


Joined: 27 Jun 2002
Posts: 1549

PostPosted: Mon Jan 13, 2003 4:47 pm    Post subject: Reply with quote

bash will do word splitting when it expands the mygroups variable. To prevent this, just change the third line in your script to:
Code:

echo "$mygroups"

The double quotes will prevent bash from splitting words and will preserve whitespace characters, including "\n".
tdm wrote:
Thanks for the help guys, praxim your solution seems to be the easiest so I take your tip :)

One thing, I want to put the output in a variable, I do that with:
Code:
#!/bin/bash
mygroups=`groups | tr ' ' '\n'`
echo $mygroups;

But when I execute it, I still see the groups like you see them when you just execute the groups command. But when I don't but the output in a variable it works find:
Code:
#!/bin/bash
groups | tr ' ' '\n'

Am I doing something wrong?

Thanks :)
Back to top
View user's profile Send private message
tdm
Tux's lil' helper
Tux's lil' helper


Joined: 06 May 2002
Posts: 117
Location: The Netherlands

PostPosted: Mon Jan 13, 2003 6:07 pm    Post subject: Reply with quote

Thanks Naan Yaar, your thing works perfect :D
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum