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 and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
separating entries by using cat command za_7565 Shell Programming and Scripting 7 01-29-2008 08:00 AM
combining fields in two text fields shocker Shell Programming and Scripting 3 01-16-2008 11:27 AM
separating filename and extension lucaspewkas Shell Programming and Scripting 2 04-06-2007 08:07 AM
Separating commands/programs with ; dush_19 High Level Programming 2 06-22-2006 06:34 AM
separating commands mile1982 High Level Programming 2 09-13-2004 12:41 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 02-17-2006
new2ss new2ss is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 133
separating fields

Hi,

i have a file as follows:
jonathan:bonus1,bonus2
gerald:bonus1
patrick:bonus1,bonus2

My desired output is
jonathan:bonus1
jonathan:bonus2
gerald:bonus1
patrick:bonus1
patrick:bonus2

my current code is

cat $F | awk -F"[:,]"

how should i continue the code? Can i do something like
if NF = 3, print $1 $2\n print $1 $3. is this legal in a awk statement?

or its inappropriate to use awk here?
  #2 (permalink)  
Old 02-17-2006
futurelet futurelet is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 137
Code:
awk 'BEGIN{FS="[:,]"}{for(i=2;i<=NF;i++)print $1":"$i}' file
  #3 (permalink)  
Old 02-17-2006
new2ss new2ss is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 133
Hi futurelet,

i tried to do it using the if way, and i could only managed to do it using 3 lines. The execution time is definitely longer than your suggestion, since i have to open the file 3 times. Is there any way i can modify it into 1 line? I appreciate your 'for' loop, its elegant and achieves the purpose. But i am raw to UNIX script, hoping to learn more ways to do it.

Code:
awk 'BEGIN(FS="[:,]"} { if (NF == 3) print $1":"$2)' $F
awk 'BEGIN(FS="[:,]"} { if (NF == 3) print $1":"$3)' $F
awk 'BEGIN(FS="[:,]"} { if (NF == 2) print $1":"$2)' $F
Would appreciate if anyone can suggest?

i am looking at something like
awk 'BEGIN(FS="[:,]"} { if (NF == 3) print $1":"$2 \n print $1":"$3 \n else print $1":"$2 print)' $F
but i got a bunch of syntax errors.

awk: cmd. line:1: BEGIN{FS="[:,]"}{if (NF == 2)print $1":"$2\n}
awk: cmd. line:1: ^ backslash not last chine
  #4 (permalink)  
Old 02-17-2006
stobo stobo is offline
Registered User
  
 

Join Date: Feb 2006
Posts: 10
oneliner:

Code:
bash> OIFS=$IFS; IFS=: ; while read name rest; do echo ${rest} | tr ',' '\n' | sed s/^/$name\:/g; done <your_data_filename; IFS=$OIFS
script1:
Code:
#!/usr/bin/bash
file=your_data_filename
for line in `cat $file`; do 
        name=`echo $line | cut -d':' -f1` 
        rest=`echo $line | cut -d':' -f2` 
        for i in `echo $rest | tr ',' ' '`; do
                echo ${name}:${i}
        done
done 
script2:
Code:
#!/usr/bin/bash
file=your_data_filename
for line in `cat $file`; do
        name=`echo $line | cut -d':' -f1` 
        rest=`echo $line | cut -d':' -f2` 
        echo $rest | tr ',' '\n' | sed s/^/$name\:/g
done 

Last edited by stobo; 02-17-2006 at 05:13 PM.. Reason: properly displaying code
  #5 (permalink)  
Old 02-17-2006
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,124
fast one liner:
while IFS=":," read e b1 b2 ; do echo "${e}:${b1}" ; [[ -n $b2 ]] && echo "${e}:${b2}" ; done < data
  #6 (permalink)  
Old 02-19-2006
new2ss new2ss is offline
Registered User
  
 

Join Date: Jan 2006
Posts: 133
Hi Stobo, futurelet and Perderabo,

thank you all the the suggestions.

One small question thought, as i mentioned earlier, i am relatively new to scripting, how can i improve, to the extend i can come up with the one liners?

Will doing more scripting help or are there any references i can refer to ?
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 05:57 AM.


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