how to parse this file in unix


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting how to parse this file in unix
# 1  
Old 07-20-2009
how to parse this file in unix

Hi all,
I need to parse a file which is having this format:

Details: 1
Name{
first=james
second=steven
}
Sibling{
first=2
second=3
}
Age{
first=13
second=14
}
Friend{
jessy
}

Details: 2
Name{
first=lily
second=tracy
}
Sibling{
first=2
second=3
}
Age{
first=13
second=14
}
Hobby{
hobby1=swimming
}

The output is
Name_first,Name_second,Sibling_first,Sibling_second,Age_first,Age_second,Friend,Hobby1
james,steven,2,3,13,14,jessy,
lily,tracy,2,3,13,13,,swimming


At first, I hardcode the header and use awk to retrieve the value line by line. e.g:
A=`awk 'NR==3 {
for(i=1;i<=NF;i++){
if ( $i == "first" ) {
print $3 $4
}
}
}' file`


However, the details: 1 and and details: 2 is different.Any idea on how to parse this file? Need it urgently. Thanks for help!
# 2  
Old 07-20-2009
awk-syntax, something like:
Code:
BEGIN { FS="=" 
           object=""
           print "Name_first,Name_second,Sibling_first,Sibling_second,Age_first,Age_second,Friend,Hobby1"
           OFS=","   # print use this delimeter, if you have comma between arguments in print command
           }

/^Detail/ { # start new record, print out previous
                print Name_first,Name_second,...
                # init variables
                Name_first=""
                Name_second=""
                ...
             }
/^Age/ { object="age" ; continue }
/^Name/ { object="name" ; continue }
...
/^first/ { if (object == "age" ) { Age_first=$2 ; continue } 
             if (object == "name" ) { Name_first=$2 ; continue } 
             ...
           }
/^second/ { # like first
           }

END  {  # last record out
           print Name_first,Name_second,...
       }


Last edited by kshji; 07-20-2009 at 05:54 AM..
# 3  
Old 07-20-2009
hi kshji,

I am a newbie in unix and also awk syntax.
Can you explain in more detail? e.g: I do not understand
END { # last record out
print ....
}

Thanks again for your help
# 4  
Old 07-20-2009
When you have Detail line, previous record end => print variables.
After last record you have not Detail line, so after everything END block you must print last record variables.
# 5  
Old 07-20-2009
Hi kshji,

I have try the code,

but the output is
Details 1:


Nothing is display after Details 1.

thanks.
# 6  
Old 07-20-2009
Hi kshji,

Good Script... I just got an interest to learn awk scripting. Can you please post any URL or PDF related awk, which would help me..
# 7  
Old 07-20-2009
I can't see your code ? How did you try it to use ? My code was only "proto", idea of parsing input and make output. Learning awk, not only cut&paste. Giving idea of scripting, in this case using awk. I think that in this forum we are waiting that you have basic knowledge from tool what you are using/asking helps.

Subject is "how to parse this file in unix", better subject is
"how to parse this file using awk", if you like to know/learn solution using awk.
Good subject help me( us ) to browse all subject and which are maybe for just for me. Too generally subjects make this "hoppy" hard - too much noice.

Awk basic:
Part 1
Part II
Part III
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to parse a file?

Hi guys I have a very long file which looks like this: y893 89:send prctmgr exit 106:bas_services_dwn -------------------------------------------------- y895 90:send prctmgr exit 106:bas_services_dwn -------------------------------------------------- y897 90:send prctmgr exit... (3 Replies)
Discussion started by: aoussenko
3 Replies

2. Shell Programming and Scripting

Parse a file

FILE1 2917,065A,RDF1+TDEV,2917_3RAID5,05E:0_10E:0,BL_lmwsp02,0345,xxx,3452(DR) 2917,03EA,RDF1+TDEV,2917_3RAID5,03E:0_12E:0,BL_tv00p02,0455,xxx,3ee4(DR) 2917,03EB,RDF1+TDEV,2917_3RAID5,03E:0_12E:0,BL_tv00p02,0345,xxx,2d34(DR)... (7 Replies)
Discussion started by: greycells
7 Replies

3. UNIX for Dummies Questions & Answers

How can i parse my Unix log files??

Hello, i would like to parse Unix log files and i would like to use a Unix syslog analyzer. I'm going to use Eucalyptus and i would like to parse its log files. Is there any open source/free syslog parser?? Thanks, in advance! (2 Replies)
Discussion started by: g_p
2 Replies

4. Shell Programming and Scripting

Parse file from remote server to calculate count of string existence in that file

Hi I need to parse the file of same name which exist on different servers and calculate the count of string existed in both files. Say a file abc.log exist on 2 servers. I want to search for string "test" on both files and calculate the total count of search string's existence. For... (6 Replies)
Discussion started by: poweroflinux
6 Replies

5. Shell Programming and Scripting

Unix Script to parse a CSV

I am writing a unix script that will parse a CSV and edit the values. My CSV looks like this 0,0,0,0,1,0,1,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,2,0,0,0,0,0,0 10,11,7,0,4,12,2,3,7,0,11,3,12,4,0,5,5,4,5,0,8,6,12,0,9,3,3,0,2,7,8... (16 Replies)
Discussion started by: RJ17
16 Replies

6. Shell Programming and Scripting

Need help in creating a Unix Script to parse xml file

Hi All, My requirement is create an unix script to parse the xml file and display the values of the Elements/value between the tags on console. Like say, I would like to fetch the value of errorCode from the below xml which is 'U007' and display it. Can we use SED command for this? I have tried... (10 Replies)
Discussion started by: Anil.Wmg
10 Replies

7. Shell Programming and Scripting

Unix shell script to parse the contents of comma-separated file

Dear All, I have a comma-separated file. 1. The first line of the file(header) should have 4 commas(5 fields). 2. The last line of the file should have 1 comma(2 fields). Pls help me in checking this condition in a shell script. And the number of lines between the first line and last... (11 Replies)
Discussion started by: KrishnaSaran
11 Replies

8. UNIX for Advanced & Expert Users

How to parse through a file and based on condition form another output file

I have one file say CM.txt which contains values like below.Its just a flat file 1000,A,X 1001,B,Y 1002,B,Z ... .. total around 4 million lines of entries will be in that file. Now i need to write another file CM1.txt which should have 1000,1 1001,2 1002,3 .... ... .. Here i... (6 Replies)
Discussion started by: sivasu.india
6 Replies

9. Shell Programming and Scripting

Need help to parse the file

# Start "ABC" SFFd 0 4 Time SFFT 4 8 {Sec} User SFFTimeVal 12 8 {Sec} # Start "CP" SFFT ... (3 Replies)
Discussion started by: navsharan
3 Replies

10. Shell Programming and Scripting

Parse Unix Mail

Hi, This is my first thread on this forum. I have an urgent requirement where I have to write a shell script to - scan through the mails one by one, - pick up the time when that mail is received, - parse through the contents of that mail and pick up relevant information and... (4 Replies)
Discussion started by: its-ashish
4 Replies
Login or Register to Ask a Question