The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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 and shell scripting languages 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-06-2008 01:43 AM
Alternative to date +%s Data469 HP-UX 6 03-31-2008 03:28 PM
.NET Alternative goon12 UNIX for Dummies Questions & Answers 3 04-06-2005 12:07 PM
cut -f (or awk alternative) gefa Shell Programming and Scripting 6 03-02-2005 01:26 PM
loop alternative? apalex Shell Programming and Scripting 2 05-02-2002 12:47 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 03-08-2007
michaeltravisuk michaeltravisuk is offline
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?
  #2 (permalink)  
Old 03-08-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
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 ) '
  #3 (permalink)  
Old 03-08-2007
vgersh99's Avatar
vgersh99 vgersh99 is online now Forum Staff  
Moderator
  
 

Join Date: Feb 2005
Location: Boston, MA
Posts: 5,119
or alternatively with 'nawk':
Code:
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | nawk -v RS=: '$1=$1'
  #4 (permalink)  
Old 03-08-2007
ghostdog74 ghostdog74 is offline Forum Advisor  
Registered User
  
 

Join Date: Sep 2006
Posts: 2,509
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
  #5 (permalink)  
Old 03-08-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
echo 'name is Earl:My name is Dorothy:My name is Bernard:' | awk -F":" -v OFS="\n" '$1=$1'
  #6 (permalink)  
Old 03-08-2007
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
  
 

Join Date: Mar 2007
Location: Toronto, Canada
Posts: 2,361
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'
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 06:34 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0