Break one line to many lines using awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Break one line to many lines using awk
# 8  
Old 05-30-2009
Is the given desired output in your 1st post correct? The solution of vidyadhar85 gives the output as you posted.

Please be more clear with your questions.
# 9  
Old 05-30-2009
Quote:
Originally Posted by pinnacle
Thanks devtakh
The above solution works for the particular file i posted.
But in case i have say file with 10 fields and i want to break line on basis of last 2 columns as we did for the earlier file then this solution doesnt work because if the first 8 columns if they have a space or comma in them then they will be considered as another field.



Would appreciate if you can make the code versatile for this kind of file as well
If you can provide a sample actual like input and output, we will be able to come of with the solution.
# 10  
Old 05-31-2009
Quote:
Originally Posted by devtakh
If you can provide a sample actual like input and output, we will be able to come of with the solution.
This is a sample file, real file will differ in data and has many records but similar format, cannot paste due to sensitivity

Quote:
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|abc,def,ghi|jkl mno pqr
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|abc,def,igh|jkl mno pqr
Points:
1) File is pipe delimited
2) The data field 1,2,3,4,5 can have comma or space or anyspecial characters.
3) In 6th and 7th field if space or comma or SUBSEP occurs then i break that into another line.


Quote:
Output:
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|abc
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|def
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|ghi
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|jkl
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|mno
America|new york,manhattan|uptown downtown|upstate city|statue of liberty,central park|pqr
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|abc
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|def
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|igh
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|jkl
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|mno
America|texas,dallas|uptown downtown|upstate city|sea world,san antonio|pqr
Thanks
# 11  
Old 05-31-2009
this will do if 6th field has "," and 7th field space for differnt combination include different condition inside awk.. give it a try
Code:
awk -F"|" 'BEGIN{OFS="|"}
{if ($6 ~ /,/ && $7 ~ / /)
{split($6,A,"\,");split($7,B," ")}}
{for ( i in A) print $1,$2,$3,$4,$5,A[i]}{for ( i in B) print $1,$2,$3,$4,$5,B[i]}' filename

# 12  
Old 05-31-2009
Code:
awk -F "|" '{for(i=1;i<=NF;i++)
{
if (i == 6) z=split($i,a,"[, SUBSEP]")
else if (i==7) w=split($i,b,"[, SUBSEP]")
else
s=(s=="")?$i:s"|"$i
}
if(z >0) for(i=1;i<=z;i++)
print s,a[i];
if(w>0)for(j=1;j<=w;j++) print s,b[j];
s="";z=0;w=0
}' OFS="|" input

-Devaraj Takhellambam
# 13  
Old 05-31-2009
Quote:
Originally Posted by devtakh
Code:
awk -F "|" '{for(i=1;i<=NF;i++)
{
if (i == 6) z=split($i,a,"[, SUBSEP]")
else if (i==7) w=split($i,b,"[, SUBSEP]")
else
s=(s=="")?$i:s"|"$i
}
if(z >0) for(i=1;i<=z;i++)
print s,a[i];
if(w>0)for(j=1;j<=w;j++) print s,b[j];
s="";z=0;w=0
}' OFS="|" input

-Devaraj Takhellambam
Quote:
Thank you very much Devaraj Takhellambam.
This fits my file scenario completley and is more versatile.

want to thank Vidyadhar85 as well


devtakh,

I was mistaken the code doesnt work when seperator is SUBSEP.
I think code is looking for word "SUBSEP" rather looking for SUBSEP
non-printable character.


I have attached the input file with SUBSEP special character in it.

Help is appreciated

Last edited by pinnacle; 05-31-2009 at 08:31 PM.. Reason: code not working
# 14  
Old 05-31-2009
Quote:
Originally Posted by pinnacle
devtakh,

I was mistaken the code doesnt work when seperator is SUBSEP.
I think code is looking for word "SUBSEP" rather looking for SUBSEP
non-printable character.


I have attached the input file with SUBSEP special character in it.

Help is appreciated

Try removing double quotes in split function like this.
Code:
awk -F "|" '{for(i=1;i<=NF;i++)
{
if (i == 6) z=split($i,a,[, SUBSEP])
else if (i==7) w=split($i,b,[, SUBSEP])
else
s=(s=="")?$i:s"|"$i
}
if(z >0) for(i=1;i<=z;i++)
print s,a[i];
if(w>0)for(j=1;j<=w;j++) print s,b[j];
s="";z=0;w=0
}' OFS="|" input

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to remove lines that do not start with digit and combine line or lines

I have been searching and trying to come up with an awk that will perform the following on a converted text file (original is a pdf). 1. Since the first two lines are (begin with) text they are removed 2. if $1 is a number then all text is merged (combined) into one line until the next... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Beginners Questions & Answers

awk script for pattern match and line break

Hi, I have input which reads like 9089.00 ----- kl jkjjljk lkkk; (909099) 9097.00 ----- HGJJHHJ jcxkjlkjvhvlk jhdkjksdfkhfskd 898.00 ----- HHHH I am trying to do something like this - As soon as I found pattern match "XYZ.00-----" it will insert a line break to the input and will go to... (3 Replies)
Discussion started by: Indra2011
3 Replies

3. Shell Programming and Scripting

Break line content into multiple lines using delimiter

I need to break the line after every 3rd semi colon(;) using Unix shell scripting Input.txt ABC;DEF;JHY;LKU;QWE;BVF;RGHY; Output.txt ABC;DEF;JHY; LKU;QWE;BVF; RGHY; (1 Reply)
Discussion started by: meet_calramz
1 Replies

4. Shell Programming and Scripting

Break lines that appear together

Hello to all May you help me with a sed or awk script to fix the text below please. I have a file where all lines should have 67 characters but from time to time 2 lines appear together like below. * First column always has 15 characters * Second column always has 32 characters * 3rd,... (5 Replies)
Discussion started by: Ophiuchus
5 Replies

5. Shell Programming and Scripting

Enumerate lines until each line break using awk

Hi, I have the following data: This this DT 0.99955 0 4 is be VBZ 1 5 7 sentence sentence NN 0.916667 8 16 one one NN 0.545078 17 20 . . Fp 1 20 21 This this DT 0.99955 22 26 is be VBZ 1 27 29 the the DT 1 30 33 second 2 JJ 0.930556 34 40 sentence sentence NN 0.916667 41 49... (1 Reply)
Discussion started by: owwow14
1 Replies

6. Shell Programming and Scripting

How to calculate mean in AWK? line by line several files, thousands of lines

I'm kinda stuck on this one, I have 7 files with 30.000 lines/file like this 050 0.023 0.504336 050 0.024 0.529521 050 0.025 0.538908 050 0.026 0.537035 I want to find the mean line by line of the third column from the files named like this: Stat-f-1.dat .... Stat-f-7.dat Stat-s-1.dat... (8 Replies)
Discussion started by: AriasFco
8 Replies

7. Shell Programming and Scripting

BASH: Break line, read, break again, read again...

...when the lines use both a colon and commas to separate the parts you want read as information. The first version of this script used cut and other non-Bash-builtins, frequently, which made it nice and zippy with little more than average processor load in GNOME Terminal but, predictably, slow... (2 Replies)
Discussion started by: SilversleevesX
2 Replies

8. Shell Programming and Scripting

Awk to Break lines to multiple lines.

Input File: nawk -F "|" '{ for(i=1;i<=NF;i++) { if (i == 2) {gsub(",","#",$i);z=split($i,a,"")} else if (i == 3) {gsub(",","#",$i);z=split($i,b,"")} } if(z > 0) for(i=1;i<=z;i++) print $1,a,"Test"; if(w > 0) for(j=1;j<=w;j++) ... (1 Reply)
Discussion started by: pinnacle
1 Replies

9. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

10. Shell Programming and Scripting

join lines on line break in files

i had a file where lines appear to be broken when they shouldn't eg Line 1. kerl abc sdskd sdsjkdlsd sdsdksd \ Line 2. ksdkks sdnjs djsdjsd i can do a shift join to combine the lines but i there are plenty of files with this issue Line 1. kerl abc sdskd sdsjkdlsd sdsdksd ksdkks sdnjs... (6 Replies)
Discussion started by: mad_man12
6 Replies
Login or Register to Ask a Question