The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
getopts alternative? krishmaths Shell Programming and Scripting 3 04-05-2008 10:43 PM
Alternative to date +%s Data469 HP-UX 6 03-31-2008 12:28 PM
.NET Alternative goon12 UNIX for Dummies Questions & Answers 3 04-06-2005 09:07 AM
cut -f (or awk alternative) gefa Shell Programming and Scripting 6 03-02-2005 09:26 AM
loop alternative? apalex Shell Programming and Scripting 2 05-02-2002 09:47 AM

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 03-08-2007
Registered User
 

Join Date: Mar 2007
Posts: 9
Using awk (or an alternative)

Hi,

I'm trying to echo a variable that has values separated by colons, putting each value on a new line. So as an example, a variable I might have would contain:

My name is Earl:My name is Dorothy:My name is Bernard:

And I want the output:

My name is Earl
My name is Dorothy
My name is Bernard

Also, I will be unaware of how many values the colon separated variable will contain (as it is generated by another function), so whatever is used will need to be flexible enough to accommodate any number of values. I'm not very competent in awk, so is there a way this can be done either with awk or with something else?

Also, is it possible to specify a field separator (such as a colon or comma) to the for command?
Reply With Quote
Forum Sponsor
  #2 (permalink)  
Old 03-08-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
echo "name is Earl:My name is Dorothy:My name is Bernard:" | tr ':' '\n'
or
Code:
echo "name is Earl:My name is Dorothy:My name is Bernard:" | sed "s/:/\\
/g"
or
Code:
echo "name is Earl:My name is Dorothy:My name is Bernard:" | awk ' gsub( ":" , "\n" ,$0 ) '
Reply With Quote
  #3 (permalink)  
Old 03-08-2007
vgersh99's Avatar
Moderator
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 2,999
or alternatively with 'nawk':
Code:
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | nawk -v RS=: '$1=$1'
Reply With Quote
  #4 (permalink)  
Old 03-08-2007
Registered User
 

Join Date: Sep 2006
Posts: 1,434
if you have Python, here's an alternative:
Code:
#!/usr/bin/python
s = "name is Earl:My name is Dorothy:My name is Bernard:"
for item in s.split(":"):
    print item
Reply With Quote
  #5 (permalink)  
Old 03-08-2007
Registered User
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,397
Code:
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | awk -F":" -v OFS="\n" '$1=$1'
Reply With Quote
  #6 (permalink)  
Old 03-08-2007
cfajohnson's Avatar
Registered User
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 471
Quote:
Originally Posted by michaeltravisuk
Hi,

I'm trying to echo a variable that has values separated by colons, putting each value on a new line. So as an example, a variable I might have would contain:

My name is Earl:My name is Dorothy:My name is Bernard:

And I want the output:

My name is Earl
My name is Dorothy
My name is Bernard

Also, I will be unaware of how many values the colon separated variable will contain (as it is generated by another function), so whatever is used will need to be flexible enough to accommodate any number of values. I'm not very competent in awk, so is there a way this can be done either with awk or with something else?

Also, is it possible to specify a field separator (such as a colon or comma) to the for command?
To answer your last question first: yes, set IFS; see my code below.

You don't need awk:

Code:
var="My name is Earl:My name is Dorothy:My name is Bernard:"
set -f
IFS=:
printf "%s\n" $var
A slower method would be to use tr:

Code:
printf "%s\n" "${var%:}" | tr : '\n'
Reply With Quote
Google UNIX.COM
Reply

Thread Tools
Display Modes




All times are GMT -7. The time now is 09:22 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008 The CEP Blog All Rights Reserved -Ad Management by RedTyger Visit The Global Fact Book

Content Relevant URLs by vBSEO 3.2.0