Append next line to previous line when one pattern not found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Append next line to previous line when one pattern not found
# 1  
Old 09-07-2011
Append next line to previous line when one pattern not found

Hi,
I need help for below scenario.I have a flat file which is having records seperated by delimiters which will represent each record for oracle table.My Control file will consider each line as one record for that table. Some of the lines are aligned in two/three lines so that records are rejected by scripts.
I need help to align the data as expected.

Input will be like below:
Code:
PC111111^|test1^|....   ->Line1-Its inserted corretly
PC222222^|test2^|
asdgasdgaj^|                ->Line2 seperated into two records so its considered as bad record.

Please help me with command that will append next line with previous line if PCXXXXXX pattern is not found as first word in each line.

I have tried with
Code:
sed -e :a -e '$!N;s/\n[^PC]//;ta' -e 'P;D'

But its not appending if the next line starts with "P","^" and "C".Smilie

Any inputs are greatly appreciated.Smilie

Last edited by Franklin52; 09-08-2011 at 08:07 AM.. Reason: Please use code tags for code and data samples, thank you
# 2  
Old 09-07-2011
try this:
Code:
awk '/^PC/{a[NR]=$0}!/^PC/{a[NR-1]=a[NR-1]$0}END{for(i=0;i<NR;i++){print a[i]}}' file

# 3  
Old 09-08-2011
Thanks Kato. Its working fine.
# 4  
Old 09-12-2011
Alternatively, as you initially thought, sed proves more concise:
Code:
 sed '/^PC/{N;/\nPC/!{s/\n//};P;D}' file


Last edited by kato; 09-12-2011 at 07:18 PM.. Reason: forgot to handle last line or odd lines
# 5  
Old 09-12-2011
Another concise one-liner using Perl -

Code:
$
$ cat -n f20
     1  PC111111^|test1^|....   ->Line1-Its inserted corretly
     2  PC222222^|test2^|
     3  asdgasdgaj^|
     4  gjeucowkje^|
     5  wwwdixoeum^|
     6  PC333333^|test2^|
     7  PC444444^|test2^|
     8  fjaldkdjgk^|
     9  PC555555^|test2^|
$
$ perl -0pe 's/\n([^PC])/$1/g' f20
PC111111^|test1^|....   ->Line1-Its inserted corretly
PC222222^|test2^|asdgasdgaj^|gjeucowkje^|wwwdixoeum^|
PC333333^|test2^|
PC444444^|test2^|fjaldkdjgk^|
PC555555^|test2^|
$
$

tyler_durden
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Remove new line starting with a numeric value and append it to the previous line

Hi, i have a file with multiple entries. After some tests with sed i managed to get the file output as follows: lsn=X-LINK-IN0,apc=661:0,state=avail,avail/links=1/1, 00,2110597,2094790,0,81,529,75649011,56435363, lsn=TM1ITP1-AM1ITP1-LS,apc=500:0,state=avail,avail/links=1/1,... (5 Replies)
Discussion started by: nms
5 Replies

2. Shell Programming and Scripting

Issue while append to previous line

Hi, I have data as below. 36578019,005-923887317,UNMDL,20151230,2C3CCAAG4GH135448,L,TX,20160108,62,"030916 PPT TX AFF RPRT VALID AFF IN PDP WLL FWD TO RYAN ON 031116 CB1619 ",, 36580219,611-923785453,FC,20151209,ZACCJABT9FPC19274,L,TX,20160108,83,,,... (4 Replies)
Discussion started by: JSKOBS
4 Replies

3. Shell Programming and Scripting

How to print previous line of multiple pattern matched line?

Hello, I have below format log file, Comparing csv_converted_files/2201/9747.1012H67126.5077292103609547345.csv and csv_converted_files/22019/97447.1012H67126.5077292103609547345.csv Comparing csv_converted_files/2559/9447.1012H67126.5077292103609547345.csv and... (6 Replies)
Discussion started by: arvindshukla81
6 Replies

4. UNIX for Dummies Questions & Answers

How to remove fields space and append next line to previous line.?

awk 'BEGIN{FS = "«"} NR == 1 {p = $0; next} NF > 1 {print p; p = $0} NF <= 1 {p = (p " " $0)} END {print p}' input.txt > output.txt This is what the input data file looks like with broken lines Code: 29863 «890000000 «543209911 «CHNGOHG «000000001 «055 ... (4 Replies)
Discussion started by: cumeh1624
4 Replies

5. Shell Programming and Scripting

Append next line to previous lines when NF is less than 0

Hi All, This is very urgent, I've a data file with 1.7 millions rows in the file and the delimiter is cedilla and I need to format the data in such a way that if the NF in the next row is less than 1, it will append that value to previous line. Any help will be appricated. Thanks,... (17 Replies)
Discussion started by: cumeh1624
17 Replies

6. Shell Programming and Scripting

Append text on particular line after pattern found

hi, i have /etc/inittab, I want to add another line after that when i find a pattern "l6:6:wait:/etc/rc.d/rc 6". original l6:6:wait:/etc/rc.d/rc 6 after-change l6:6:wait:/etc/rc.d/rc 6 /sbin/if-pp-to-cng (3 Replies)
Discussion started by: learnbash
3 Replies

7. UNIX for Dummies Questions & Answers

Append a string on the next line after a pattern string is found

Right now, my code is: s/Secondary Ins./Secondary Ins.\ 1/g It's adding a 1 as soon as it finds Secondary Ins. Primary Ins.: MEDICARE B DMERC Secondary Ins. 1: CONTINENTAL LIFE INS What I really want to achieve is having a 1 added on the next line that contain "Secondary Ins." It... (4 Replies)
Discussion started by: newbeee
4 Replies

8. Shell Programming and Scripting

Append each line to next previous line in a file

Hi all, Please help me in providing sample code to append the following 4 lines in one row. Input : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 RXOCF-430 HY1711 1 EXTERNAL ALARM DOOR ALARM Output should be : A1/EXT "BAPBSC10/07B/00" 523 090530 0115 ... (8 Replies)
Discussion started by: sudhakaryadav
8 Replies

9. UNIX for Advanced & Expert Users

append the line with the previous if it not start with 1=

How to append the line with the previous if it not start with 1=. 1=ttt, 2=xxxxxx, 3=4545 44545, 4=66666, 1=ttt, 2=xxxxxx, 3=34434 3545, 4=66666, 5=ffffff 6=uuuuuuu, 7=ooooooo 1=ttt, 2=xxxxxx, 3=311343545, 4=66666 1=ttt, 2=xxxxxx, 5=XAXAXA, 7=FDFD (3 Replies)
Discussion started by: palsevlohit_123
3 Replies

10. Shell Programming and Scripting

Append line that does not contain pipe to it previous line

Hi All, I have a file which contains data as below When we see no pipe character in the line. append those lines to the previous line with pipe character till we get the next line with pipe character with ~(concat with ~) Input file looks like: 1080530944|001|john.l.bonner|Acknowledge|CN... (11 Replies)
Discussion started by: ainuddin
11 Replies
Login or Register to Ask a Question

Featured Tech Videos