LinuxQuestions.org
Help answer threads with 0 replies.
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 08-02-2010, 11:50 PM   #1
packets
Member
 
Registered: Oct 2005
Posts: 339

Rep: Reputation: 32
it this possible in bash script


I'm a doing a script. There are some part of the script that I need to do but don't know how to do it.

If I have a text which contains:

Quote:
foo,fee,fii,fuu
Could I rearrange it to:

Quote:
foo
fee
fii
fuu
I don't know if sed can do this. There are some function of sed that I just recently discovered but I don't know if it can do the scenario above.

I'm currently checking a tool/command that could help me do this thing just wandering if there's anyone out there that could suggest on how I could accomplish this.
 
Old 08-02-2010, 11:57 PM   #2
vxc69
Member
 
Registered: Jul 2004
Distribution: Ubuntu
Posts: 387

Rep: Reputation: 33
Try:

Code:
sed -i 's/,/\n/g' YOUR-FILE.txt
 
Old 08-02-2010, 11:57 PM   #3
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
I tried this one and it works

Quote:
echo "test,too,foo" | sed 's/\,/\n/g'
Quote:
test
too
foo
 
Old 08-02-2010, 11:58 PM   #4
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
Quote:
sed -i 's/,/\n/g' YOUR-FILE.txt
Thanks for this one.
 
Old 08-03-2010, 03:40 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Or it can be done entirely within bash which requires a few more lines of code but runs faster as long as the strings are small, say less than 2 kB
Code:
#!/bin/bash

list='foo,fee,fii,fuu'
IFS=,
array=($list)
echo ${array[*]}
unset IFS
Notes:
  1. In the array assignment array=($list), $list is substituted and then split into array elements wherever a character from the IFS (=internal field separator?) variable is found.
  2. unset IFS results in bash treating IFS as if it had the default value. It is a quicker and more concise way of restoring a default IFS than saving the default value and re-assigning it.
 
Old 08-03-2010, 09:04 AM   #6
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Code:
while read -d ,; do echo "$REPLY"; done <<< "foo,fee,fii,fuu"
Code:
while read -d ,; do echo "$REPLY"; done < file.txt
 
Old 08-03-2010, 06:16 PM   #7
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
Thanks to all replies
 
Old 08-03-2010, 09:07 PM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
simply use set

Code:
IFS=","
set -- $list
echo $1, $2 ,$3 etc
 
Old 08-04-2010, 02:19 AM   #9
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
What if I want to do it reverse?

I have

Quote:
foo,
fee,
fii
And I want it to be

Quote:
foo,fee,fii
 
Old 08-04-2010, 02:26 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
iterate the file, and use printf

Code:
while read -r line
do
  printf "%s" $line
done <"file"
or
Code:
$ var=$(<file)
$ echo $var # note: without double quotes
foo, fee, fii
the other method involve removing the newlines, which i will leave it to somebody else to show you

Last edited by ghostdog74; 08-04-2010 at 02:29 AM.
 
Old 08-04-2010, 03:07 AM   #11
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
I search also here in LWQ and it seems to work

Quote:
sed -e :a -e '$!N; s/\n//; ta' lists.txt
 
Old 08-04-2010, 03:18 AM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by packets View Post
I search also here in LWQ and it seems to work
using tr is even simpler

Code:
tr -d "\n" <file>newfile
and of course, awk

Code:
awk -vRS="\n" '1' ORS="" file

Last edited by ghostdog74; 08-04-2010 at 03:21 AM.
 
Old 08-04-2010, 03:31 AM   #13
packets
Member
 
Registered: Oct 2005
Posts: 339

Original Poster
Rep: Reputation: 32
@ghostdog74

Thanks. I'm actually talking to my web developer coz it seems he can parse the data before as a single line separated by comma before it pass to my script.
 
  


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
Variables and Mkvextract in a bash script and a good resource for bash help? gohmifune Linux - General 9 04-13-2011 08:37 AM
passing variable from bash to perl in a bash script quadmore Programming 6 02-21-2011 04:11 AM
[SOLVED] Using a long Bash command including single quotes and pipes in a Bash script antcore Linux - General 9 07-22-2009 11:10 AM
Strange if statement behaviour when using bash/bash script freeindy Programming 7 08-04-2008 06:00 AM
Bash script to create bash script jag7720 Programming 10 09-10-2007 07:01 PM

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

All times are GMT -5. The time now is 08:31 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