Need help on appending all the lines in a file after a pattern is found


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help on appending all the lines in a file after a pattern is found
# 1  
Old 10-09-2012
Bug Need help on appending all the lines in a file after a pattern is found

Hi Friends,

I am working on a file which has content as follows
Code:
Wed,Database,ABC_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes
Wed,Logging,out,user,109000151,active,for,8,minutes
Wed,Logging,out,user,302002654,active,for,0,minutes
Wed,Logging,out,user,302002654,active,for,60,minutes
Wed,Database,XYZ_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes
Wed,Logging,out,user,109000151,active,for,8,minutes
Wed,Logging,out,user,302002654,active,for,0,minutes
Wed,Logging,out,user,302002654,active,for,60,minutes
Wed,Database,DBC_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes
Wed,Logging,out,user,109000151,active,for,8,minutes
Wed,Logging,out,user,302002654,active,for,0,minutes
Wed,Logging,out,user,302002654,active,for,60,minutes

my requirement is that i want to have the 'cube name' i.e ABC_cube,XYZ_cube at the end of the successive lines as follows
Code:
Wed,Database,ABC_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes,ABC_cube
Wed,Logging,out,user,109000151,active,for,8,minutes,ABC_cube
Wed,Logging,out,user,302002654,active,for,0,minutes,ABC_cube
Wed,Logging,out,user,302002654,active,for,60,minutes,ABC_cube
Wed,Database,XYZ_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes,XYZ_cube
Wed,Logging,out,user,109000151,active,for,8,minutes,XYZ_cube
Wed,Logging,out,user,302002654,active,for,0,minutes,XYZ_cube
Wed,Logging,out,user,302002654,active,for,60,minutes,XYZ_cube
Wed,Database,DBC_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes,DBC_cube
Wed,Logging,out,user,109000151,active,for,8,minutes,DBC_cube
Wed,Logging,out,user,302002654,active,for,0,minutes,DBC_cube
Wed,Logging,out,user,302002654,active,for,60,minutes,DBC_cube

Thanks in advance Smilie

Last edited by Franklin52; 10-09-2012 at 10:50 AM.. Reason: Please use code tags for data and code samples
# 2  
Old 10-09-2012
Code:
awk -F, '{if($0 ~ /Database/){s=$3}else{$(NF+1)=s}}1' OFS="," file

# 3  
Old 10-09-2012
With some assumptions:
Code:
awk -F, 'NF==4{c=$3}NF!=4 && c{$0=$0 FS c}1' file

# 4  
Old 10-09-2012
Code:
$ awk -F, -v OFS=, '/cube/{a=$3} /minutes/{$NF=$NF","a}1' a.txt
Wed,Database,ABC_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes,ABC_cube
Wed,Logging,out,user,109000151,active,for,8,minutes,ABC_cube
Wed,Logging,out,user,302002654,active,for,0,minutes,ABC_cube
Wed,Logging,out,user,302002654,active,for,60,minutes,ABC_cube
Wed,Database,XYZ_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes,XYZ_cube
Wed,Logging,out,user,109000151,active,for,8,minutes,XYZ_cube
Wed,Logging,out,user,302002654,active,for,0,minutes,XYZ_cube
Wed,Logging,out,user,302002654,active,for,60,minutes,XYZ_cube
Wed,Database,DBC_cube,loaded
Wed,Logging,out,user,302002654,active,for,0,minutes,DBC_cube
Wed,Logging,out,user,109000151,active,for,8,minutes,DBC_cube
Wed,Logging,out,user,302002654,active,for,0,minutes,DBC_cube
Wed,Logging,out,user,302002654,active,for,60,minutes,DBC_cube

please use the code tag
https://www.unix.com/how-post-unix-li...code-tags.html

Last edited by itkamaraj; 10-09-2012 at 10:13 AM.. Reason: missed the comma
# 5  
Old 10-09-2012
thanks friends,

i am basically new to scripting .. while trying to run above script i encountered the following error
Code:
awk: syntax error near line 1
awk: bailing out near line 1


Last edited by Franklin52; 10-10-2012 at 04:25 AM.. Reason: Please use code tags for data and code samples
# 6  
Old 10-09-2012
try with nawk
# 7  
Old 10-09-2012
the following eror is appearing now:

Code:
nawk: syntax error at source line 1
 context is
         >>> OFS=, <<< 
nawk: bailing out at source line 1


Last edited by Franklin52; 10-10-2012 at 04:26 AM.. Reason: Please use code tags for data and code samples
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Sed/awk join lines once pattern found

Hi all OS - RHEL6.4 I have input file -f1.txt I need to search line which starts with \Start and read next line till it gets blank line and join them all. I need to trim any trailing spaces for each line.So output.txt should be.. \Start\now\fine stepwatch this space for toolsends... (7 Replies)
Discussion started by: krsnadasa
7 Replies

2. Shell Programming and Scripting

Appending lines in a file

Hello, I have a file like: str1,"HEX"H,(39),info str2,"HEX"H,(854548),info str3,"HEX"H,'BGTOUR',info str4,"HEX"H,(534322),info str1,"HEX"H,,info str3,"HEX"H,'Landing',info str4,"HEX"H,'BG',info str1,"HEX"H,,info str3,"HEX"H,'Ay',info str1,"HEX"H,(27),info str2,"HEX"H,(854548),info... (2 Replies)
Discussion started by: apenkov
2 Replies

3. Shell Programming and Scripting

If first pattern is found, look for second pattern. If second pattern not found, delete line

I had a spot of trouble coming up with a title, hopefully you'll understand once you read my problem... :) I have the output of an ldapsearch that looks like this: dn: cn=sam,ou=company,o=com uidNumber: 7174 gidNumber: 49563 homeDirectory: /home/sam loginshell: /bin/bash uid: sam... (2 Replies)
Discussion started by: samgoober
2 Replies

4. Shell Programming and Scripting

Copy/print all lines between pattern is found in .log files

Hi, I have a folder with multiple (< 33) .log files. And I have to copy the lines between two patterns from all the .log files to a new file. (script file with a loop?) Thanks in advance. 1.log ... .. xx1> begin ... .. .. >>> Total: 2 Alarms .. .. (17 Replies)
Discussion started by: AK47
17 Replies

5. Shell Programming and Scripting

awk to print all lines after a pattern is found

Is there a way with aw to print all lines after a string is found There is a file like this ....... ........ 2012/19/11 :11.58 PM some data lne no date 2012/19/11 :11.59 PM some other data 2012/20/11 :12.00 AM some other data some line without dates some more lines without dates... (8 Replies)
Discussion started by: swayam123
8 Replies

6. UNIX for Dummies Questions & Answers

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (6 Replies)
Discussion started by: mkandula1983
6 Replies

7. Shell Programming and Scripting

Grouping or appending the lines in a file through Unix

Hi, I am looking for a way to Group the Lines in a file.. Basically My file structure is something like this A 1 100 abc def A 1 200 abc def A 1 300 abc def A 2 100 pqr def A 2 200 pqr def A 2 300 pqr def A 1 100 abc def A 1 200 xyz def A 1 300 xyz def I need it as... (1 Reply)
Discussion started by: mkandula1983
1 Replies

8. Shell Programming and Scripting

appending lines to a file dynamically

Hi all, I have a file with some number of lines. I need to add certain number of lines from another file which may vary according to the user's input and to it. eg code: I/P file 1 apps/file/xyz apps/file/abc apps/file/def file 2 progs/file/xyz ... (2 Replies)
Discussion started by: Ananthdoss
2 Replies

9. Shell Programming and Scripting

Delete a block of text delimited by blank lines when pattern is found

I have a file which contains blocks of text - each block is a multi-lines text delimited by blank lines eg. <blank line> several lines of text ... pattern found on this line several more lines of text ... <blank line> How do you delete the block of text (including the blank lines) when... (17 Replies)
Discussion started by: gleu
17 Replies

10. Shell Programming and Scripting

need help appending lines/combining lines within a file...

Is there a way to combine two lines onto a single line...append the following line onto the previous line? I have the following file that contains some blank lines and some lines I would like to append to the previous line... current file: checking dsk c19t2d6 checking dsk c19t2d7 ... (2 Replies)
Discussion started by: mr_manny
2 Replies
Login or Register to Ask a Question