Script to convert rows to 1 record with delimter


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to convert rows to 1 record with delimter
# 1  
Old 12-01-2008
Script to convert rows to 1 record with delimter

I have a data file as follows. The 1st line of record starts with "<JOB" and the last line of record ends with "</JOB". There are multiple rows (62 -70) within <JOB and </JOB and it varies. There are multiple records in the file starting with <JOB and ending with </JOB.

Sample data:
<JOB
NAME=JOE DIZA
AGE=52
DOB=110956
AAA=001
BBB=070
</JOB
<JOB
NAME=YEZDI RANG
AGE=53
DOB=110955
AAA=001
DDD=070
</JOB

I want the output to look like this with delimter '|'
<JOB | NAME=JOE DIZA| AGE=52 | DOB=110956 |AAA=001|BBB=070|</JOB
<JOB | NAME=YEZDI RANG| AGE=53 | DOB=110955 |AAA=001|DDD=070|</JOB

Any help will be appreciated.
# 2  
Old 12-01-2008
Code:
awk '/JOB/ { JOB = !JOB} JOB {printf $0"|" ; next} 1' file > newfile

# 3  
Old 12-01-2008
Gives me a syntax error. Tried runing using 'awk -f ' getting same error.

awk '/JOB/ { JOB = !JOB} JOB {printf $0"|" ; next} 1' testdata > newfile
awk: syntax error near line 1
awk: illegal statement near line 1
awk: syntax error near line 1
awk: bailing out near line 1
# 4  
Old 12-01-2008
try nawk instead of awk, although I'm not sure that it will work there either and I don't currently have anything other than gawk available to test on.
# 5  
Old 12-02-2008
Code:
$/="</JOB";
open FH,"<a.txt" or die "Can not open file\n";
my @arr=<FH>;
for($i=0;$i<=$#arr;$i++){
	 $arr[$i]=~s/\n/|/g;
	 print $arr[$i],"\n";
}

# 6  
Old 12-02-2008
Hammer & Screwdriver An approach using sed & tr

Code:
> cat file89
<JOB
NAME=JOE DIZA
AGE=52
DOB=110956
AAA=001
BBB=070
</JOB
<JOB
NAME=YEZDI RANG
AGE=53
DOB=110955
AAA=001
DDD=070
</JOB
> sed "s/<\/JOB/&~/" file89 | tr "\n" "|" | tr "~" "\n" | sed "s/^|//"       
<JOB|NAME=JOE DIZA|AGE=52|DOB=110956|AAA=001|BBB=070|</JOB
<JOB|NAME=YEZDI RANG|AGE=53|DOB=110955|AAA=001|DDD=070|</JOB
>

# 7  
Old 12-06-2008
Script to convert rows to 1 record with delimter

Thanks to JOEYG.... The sed..tr... solution worked great with the perfect output format.

Another output I would like is to get using the same input data, is to create a record consisting of only the second field within the <JOB and </JOB statement.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with script to convert rows to columns

Hello I have a large database with the following structure: Headword=Gloss1;Gloss2;Gloss3 The Glosses are separated by a ; What I need is to reduce the multiple glosses on each row to columns Headword=Gloss1 Headword=Gloss2 Headword=Gloss3 I had written the following script in awk... (5 Replies)
Discussion started by: gimley
5 Replies

2. UNIX for Beginners Questions & Answers

Shell script to convert rows into cloumns

B010215861628 MA 01020 CARRIER B010215861695 MA 01020 CARRIER B010215861709 MA 01020 CARRIER My output is in the above format I want the output to be: (4 Replies)
Discussion started by: Teegela Prathyu
4 Replies

3. Shell Programming and Scripting

Shell script to convert rows to columns

Hi I have a file having the values like below ---------------------------- .set A col1=”ABC” col2=34 col3=”DEF” col4=”LMN” col5=25 .set A .set B col1=55 col3=”XYZ” col4=”PQR” col5=66 .set B .set C col2=”NNN” (1 Reply)
Discussion started by: Suneel Mekala
1 Replies

4. Shell Programming and Scripting

Convert Rows into Column

Hi Experts, I have a requirement to convert rows into columns. For e.g. Input File: Output File should be like Appreciate if you could suggest code snippet(may be awk) for above requirement... Thanks in Advance for your help... (3 Replies)
Discussion started by: sai_2507
3 Replies

5. Shell Programming and Scripting

Help on convert rows to colums

Need help to convert the following data Account name: admin Role: admin Description: Administrator Enabled: Yes to Account Name Role Description Enabled admin admin Administrator Yes Perl or AWK? Thanks San (9 Replies)
Discussion started by: sanguy
9 Replies

6. Shell Programming and Scripting

Convert columns to rows in perl script

Hi All I want to have a Perl script which convert columns to rows. The Perl should should read the data from input file. Suppose the input file is 7215484 date to date 173.3 A 1.50 2.23 8.45 10.14 2.00 4.50 2.50 31.32 7216154 month to month (3 Replies)
Discussion started by: parthmittal2007
3 Replies

7. Shell Programming and Scripting

convert rows to columns

hi, i have the file as below: abc def ghi jkl i want the output as abc,def,ghi,jki please reply, Thanks (4 Replies)
Discussion started by: namitai
4 Replies

8. Shell Programming and Scripting

awk, string as record separator, transposing rows into columns

I'm working on a different stage of a project that someone helped me address elsewhere in these threads. The .docs I'm cycling through look roughly like this: 1 of 26 DOCUMENTS Copyright 2010 The Age Company Limited All Rights Reserved The Age (Melbourne, Australia) November 27, 2010... (9 Replies)
Discussion started by: spindoctor
9 Replies

9. Shell Programming and Scripting

convert columns into rows

hi, Apologies if this has been covered. I have requirement where i have to convert a single column into multiple column. My data will be like this - 2 3 4 5 6 Output required - 2 3 4 5 6 (1 Reply)
Discussion started by: Nishithinfy
1 Replies

10. Shell Programming and Scripting

shell script required to convert rows to columns

Hi Friends, I have a log file as below siteid = HYD spc = 100 rset = RS_D_M siteid = DEL spc = 200 rset = RS_K_L siteid = DEL2 spc = 210 rset = RS_D_M Now I need a output like column wise as below. siteid SPC rset HYD 100 RS_D_M (2 Replies)
Discussion started by: suresh3566
2 Replies
Login or Register to Ask a Question