separating fields


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting separating fields
# 1  
Old 02-17-2006
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  
Old 02-17-2006
Code:
awk 'BEGIN{FS="[:,]"}{for(i=2;i<=NF;i++)print $1":"$i}' file

# 3  
Old 02-17-2006
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  
Old 02-17-2006
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  
Old 02-17-2006
fast one liner:
while IFS=":," read e b1 b2 ; do echo "${e}:${b1}" ; [[ -n $b2 ]] && echo "${e}:${b2}" ; done < data
# 6  
Old 02-19-2006
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 ?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Separating words in a line

Hi I have the following file # cat red it.d-state = 50988 41498 45 0 0 0 it.age_buffer= 1500 it.dir = 1 I need to grep lines that are present after "=" But when I do so, few lines has more than one value and some has one How to parse the line I have used the follwing... (6 Replies)
Discussion started by: Priya Amaresh
6 Replies

2. Shell Programming and Scripting

Help in separating a multilingual file

Hello, I have a text file running into around 100 thousand+ lines which has the following rigid structure: Each field is separated by a comma. Some examples are given below: 23,Chinttaman Pagare,चिंतमण पगारे 24, Chinttaman Pateel,चिंतामण पाटल 25, Chinttaman Rout,चिंतामण राऊत 26,... (3 Replies)
Discussion started by: gimley
3 Replies

3. Shell Programming and Scripting

Help with separating datatype, column name

Hi All, I am new to unix but have a requirement wherein I need to separate datatype,length, and column name from input file which is of below format -- record integer(10) empid; string(25) name; date("YYYY-MM-DD") dob; decimal(10) salary; end now after getting datatype,its length and... (4 Replies)
Discussion started by: phoenix09
4 Replies

4. Shell Programming and Scripting

Adding and then separating the output

Hi I have this output /vol/vol0 4GB /vol/nonprod00 682GB /vol/prod00 3GB /vol/nasp_archive 201GB /vol/nasp_iface 92GB /vol/nasp_bsi 0GB /vol/nasp_vertex 0GB /vol/nasp_sapmnt_mp2 1GB /vol/nasp_sapmnt_prd 52GB /vol/nasp_sapmnt_srp 1GB /vol/nasp_smd 1GB /vol/nasp_ccms 0GB... (8 Replies)
Discussion started by: bombcan
8 Replies

5. Programming

Separating two classes in two files

I have a file Map.hh shown below. I want to put the two classes Phase and Map in two different files Phase.hh and Map.hh. I have forward declaration before the Map class. How can I tackle this situation? ////////////////////////////////////////////////////////////////////////// #ifndef... (3 Replies)
Discussion started by: kristinu
3 Replies

6. Shell Programming and Scripting

Need help separating a file

Hi all, I have a single text file, Contig3.fasta, that looks like this: >NAME1 ACCTGGTA >NAME2 GGTTGGACA >NAME3 ATTTTGGGCCAnd It has about 100 items like this in it. What I would like to do is copy each item into 100 different text files, and have them named a certain way Output... (4 Replies)
Discussion started by: repiv
4 Replies

7. Shell Programming and Scripting

separating folders

I have folder like main. inside main folder there are subfolders & files like main1 main2 main3, file1, file2, file3. I want folders main1 & main2, file1, file2 from main folder. copy them into new folder. Please suggest me how to do it. I am new to shell programming (2 Replies)
Discussion started by: ypremcha
2 Replies

8. Shell Programming and Scripting

Separating fields

Hi, I have a text file in following format: 2.45 5.67 6.43 I have to cut the values before decimal and store them in a file. So the output file should look like: 2 5 6 . . and so on... Can someone suggest me a sed/awk command for doing this? (2 Replies)
Discussion started by: sajal.bhatia
2 Replies

9. Shell Programming and Scripting

Help with separating syslog messages.

Hello Guys... I am bit new to shell scripting and was looking for help !! I have got syslog data on a linux server recording log messages from a device. I need to seperate the data from log to file so that I can push it excell and get a report from that. Log is in the format below "... (2 Replies)
Discussion started by: raj_26apr
2 Replies

10. Programming

separating commands

hey there well i have a small problem with my code. when for example : " /bin/sleep 10 & ls -l mila > xyz " is entered, the program is supposed to separate the two commands 1) /bin/sleep 10 & and 2) ls -l mila > xyz. im not sure of how to achieve this. my current program stores both commands... (2 Replies)
Discussion started by: mile1982
2 Replies
Login or Register to Ask a Question