Concatinating the lines based on number of delimiters


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatinating the lines based on number of delimiters
# 1  
Old 05-15-2012
Concatinating the lines based on number of delimiters

Hi,

I have a problem to concatenate the lines based on number of delimiters (if the delimiter count is 9 then concatenate all the fields & remove the new line char bw delimiters and then write the following data into second line) in a file.

my input file content is
Code:
   Title| ID| Owner| Estimate| Project| Complexity| Done| Feature Group|
   Sprint|
   RR - Bug in m_tgt_fact_org_hierarchy mapping| D-04980| fgfggf
   Sejhh| 5.00| 2012 BI Project| | | 1.3 Ratings Reporting| Sprint 10
   (10 -23 May)|
   Intermediate results reports for US banks -Sprint1| B-49625| fhgh
   fgh| 8.00| 2012 BI Project| | | 3.6 FI-RACE| Sprint 10 (10 -23 May)|

and expecting the output as follows:

Code:
Title| ID| Owner| Estimate| Project| Complexity| Done| Feature Group|Sprint|   
RR - Bug in m_tgt_fact_org_hierarchy mapping| D-04980| fgfggfSejhh| 5.00| 2012 BI Project| | | 1.3 Ratings Reporting| Sprint 10 (10 -23 May)|
Intermediate results reports for US banks -Sprint1| B-49625| fhgh fgh| 8.00| 2012 BI Project| | | 3.6 FI-RACE| Sprint 10 (10 -23 May)|

I would really appreciate your help on this problem.

Last edited by Scrutinizer; 05-15-2012 at 01:09 PM.. Reason: code tags
# 2  
Old 05-15-2012
Code:
[mute@geek ~/temp/bi.infa]$ cat script
#!/usr/bin/awk -f
BEGIN { FS=OFS="|"; RS=""; cols=9 }
{
        gsub(/\n/, "")
        for (i=1;i<NF;i++) {
                printf("%s%s",$i,++p==cols?OFS ORS:OFS)
                if (p==cols) { p=0; }
        }
}

Code:
[mute@geek ~/temp/bi.infa]$ ./script input
Title| ID| Owner| Estimate| Project| Complexity| Done| Feature Group|Sprint|
RR - Bug in m_tgt_fact_org_hierarchy mapping| D-04980| fgfggfSejhh| 5.00| 2012 BI Project| | | 1.3 Ratings Reporting| Sprint 10(10 -23 May)|
Intermediate results reports for US banks -Sprint1| B-49625| fhghfgh| 8.00| 2012 BI Project| | | 3.6 FI-RACE| Sprint 10 (10 -23 May)|

edit: ah. it seems newlines should be replaced with spaces in some instances? I understand after "Sprint 10", but "fgfggf Sejhh" and "fhgh fgh" seems to differ. should they both have newline replaced by space?
# 3  
Old 05-15-2012
Alternatively try:
Code:
awk '{while(NF<10 && getline p)$0=$0p}1' FS=\| OFS=\| infile


Last edited by Scrutinizer; 05-15-2012 at 01:34 PM..
These 2 Users Gave Thanks to Scrutinizer For This Post:
# 4  
Old 05-15-2012
derrp. my avoidance of getline bit me. i was also anticipating concating two lines may make NF>10
# 5  
Old 05-15-2012
Hi bi.infa,

One way using perl:
Code:
$ cat infile
Title| ID| Owner| Estimate| Project| Complexity| Done| Feature Group|
Sprint|
RR - Bug in m_tgt_fact_org_hierarchy mapping| D-04980| fgfggf
Sejhh| 5.00| 2012 BI Project| | | 1.3 Ratings Reporting| Sprint 10
(10 -23 May)|
Intermediate results reports for US banks -Sprint1| B-49625| fhgh
fgh| 8.00| 2012 BI Project| | | 3.6 FI-RACE| Sprint 10 (10 -23 May)|
$ perl -ne 'BEGIN { $/ = q[|] } s/\n//g; print $_, $. % 9 == 0 ? "\n" : ""' infile
Title| ID| Owner| Estimate| Project| Complexity| Done| Feature Group|Sprint|
RR - Bug in m_tgt_fact_org_hierarchy mapping| D-04980| fgfggfSejhh| 5.00| 2012 BI Project| | | 1.3 Ratings Reporting| Sprint 10(10 -23 May)|
Intermediate results reports for US banks -Sprint1| B-49625| fhghfgh| 8.00| 2012 BI Project| | | 3.6 FI-RACE| Sprint 10 (10 -23 May)|

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: Combine multiple lines based on number of fields

If a file has following kind of data, comma delimited 1,2,3,4 1 1 1,2,3,4 1,2 2 2,3,4 My required output must have only 4 columns with comma delimited 1,2,3,4 111,2,3,4 1,222,3,4 I have tried many awk command using ORS="" but couldnt progress (10 Replies)
Discussion started by: mdkm
10 Replies

2. Shell Programming and Scripting

Print lines based on line number and specified condition

Hi, I have a file like below. 1,2,3,4,5,6,7,8,9I would like to print or copied to a file based of line count in perl If I gave a condition 1 to 3 then it should iterate over above file and print 1 to 3 and then again 1 to 3 etc. output should be 1,2,3 4,5,6 7,8,9 (10 Replies)
Discussion started by: Anjan1
10 Replies

3. UNIX for Dummies Questions & Answers

Split file based on number of blank lines

Hello All , I have a file which needs to split based on the blank lines Name ABC Address London Age 32 (4 blank new line) Name DEF Address London Age 30 (4 blank new line) Name DEF Address London (8 Replies)
Discussion started by: Pratik4891
8 Replies

4. Shell Programming and Scripting

Replacing a character with a number based on lines

Hi, I am in need of help for the two things which is to be done. First, I have a file that has around four columns. The first column is filled with letter "A". There are around 400 lines in the files as shown below. A 1 5.2 3.2 A 2 0.2 4.5 A 1 2.2 2.2 A 5 2.1 ... (2 Replies)
Discussion started by: begin_shell
2 Replies

5. UNIX for Dummies Questions & Answers

Command to split the files based on the number of lines in it

Hello Friends, Can anyone help me for the below requirement. I am having a file called Input.txt. My requirement is first check the count that is wc -l input.txt If the result of the wc -l Input.txt is less than 10 then don't split the Input.txt file. Where as if Input.txt >= 10 the split... (12 Replies)
Discussion started by: malaya kumar
12 Replies

6. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

7. Shell Programming and Scripting

Merge two non-consecutive lines based on line number or string

This is a variation of an earlier post found here: unixcom/shell-programming-scripting/159821-merge-two-non-consecutive-lines.html User Bartus11 was kind enough to solve that example. Previously, I needed help combining two lines that are non-consecutive in a file. Now I need to do the... (7 Replies)
Discussion started by: munkee
7 Replies

8. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

9. Shell Programming and Scripting

Script to split files based on number of lines

I am getting a few gzip files into a folder by doing ftp to another server. Once I get them I move them to another location .But before that I need to make sure each gzip is not more than 5000 lines and split it up . The files I get are anywhere from 500 lines to 10000 lines in them and is in gzip... (4 Replies)
Discussion started by: gubbu
4 Replies

10. Shell Programming and Scripting

How can I get an if statement to execute based on number of lines in a file?

I need to have an if statement in a script to run if there are certain processes running. Easiest way I can see to do this is to run a ps and grep the results based on what I am looking for: $ ps -ef | grep wtrs --- webtrend 5046 1 0 May 12 ? 0:28 /webtrends/versions/6.1/wtrs_ui... (6 Replies)
Discussion started by: LordJezo
6 Replies
Login or Register to Ask a Question