LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 12-07-2009, 03:02 PM   #1
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Rep: Reputation: 0
Need some help with scripting in BASH


Need to write a small script that takes a colon-seperated list of items and outputs the items, one per line to standard output without the colons. Thanks,

Al
 
Old 12-07-2009, 03:03 PM   #2
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
Hi, welcome to LQ!

You need the `sed` command. It will work perfectly for that task.

Sasha
 
Old 12-07-2009, 03:10 PM   #3
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Sasha, appreciate the info but linux is pretty much "greek" to me. Can you give me an example? Just a script that read a small file, and removes the colons : and writes to a second file. Been trying to use the "cut" command but no luck. Thanks,
Al
 
Old 12-07-2009, 03:12 PM   #4
gerryd
LQ Newbie
 
Registered: Jul 2009
Distribution: slackware 13
Posts: 21

Rep: Reputation: 1
a sed or awk script would probably be better suited for this task.
 
Old 12-07-2009, 03:18 PM   #5
gerryd
LQ Newbie
 
Registered: Jul 2009
Distribution: slackware 13
Posts: 21

Rep: Reputation: 1
check this link out. you can use sed to find and replace characters. http://www.brunolinux.com/02-The_Ter..._with_Sed.html
 
Old 12-07-2009, 03:28 PM   #6
allenms
LQ Newbie
 
Registered: Dec 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Thanks for the link. Will try to get a handle on this in the AM as it's late on this side of the globe and the old melon needs a rest.

Al
 
Old 12-07-2009, 04:51 PM   #7
GrapefruiTgirl
LQ Guru
 
Registered: Dec 2006
Location: underground
Distribution: Slackware64
Posts: 7,594

Rep: Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556Reputation: 556
A *very* basic example, to just remove the colons from a file, and put the file back where it was, leaving a backup of the orioginal:
Code:
shell$ sed -i.BAK 's/://g' filename
NOTE: very simple -- but to see other ways of doing this, and other things sed can do, check the man page with:
Code:
shell$ man sed

And, if you need more help, just ask!
Sasha
 
Old 12-07-2009, 07:11 PM   #8
mehorter
LQ Newbie
 
Registered: Mar 2008
Location: NJ
Distribution: Suse
Posts: 18

Rep: Reputation: 3
Code:
sed 's/:/\n/g' filename
This should do what you want.

sed --- calls the sed program.
' --- wrap "code" in single quotes.
s --- substitute command
: --- any ":" matches.
\n --- replace matching ":" with "\n" which is a "new-line" character.
g --- do the command globally; that is, at every occurrence on line.
" filename" ---space then filename.

If this works for you as evidenced by what rolls across your screen then simply redirect your output from the screen to a file like this:

Code:
sed 's/:/\n/g' filename > new_filename
choose a new filename or you will clobber the old one and end up with a zero-byte file or use the -i option, as mentioned above.
 
1 members found this post helpful.
Old 12-07-2009, 08:16 PM   #9
alienDog
Member
 
Registered: Apr 2004
Location: Europe
Distribution: Debian, Slackware
Posts: 505

Rep: Reputation: 48
sed will work, but you can also do:

Code:
IFS=":"
for ITEM in $@; do
  echo $ITEM
done
unset IFS

Last edited by alienDog; 12-07-2009 at 08:19 PM.
 
  


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
bash scripting ZAMO Linux - General 1 12-04-2008 02:53 AM
Reading a bash variable in bash scripting problem freeindy Programming 3 11-27-2008 02:29 AM
Bash Scripting B-Boy Programming 2 05-28-2008 01:15 AM
bash scripting Springs Linux - Newbie 7 04-17-2008 05:14 AM
Bash scripting JonCooperUK Programming 3 03-04-2004 08:55 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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