Appending lines in a file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Appending lines in a file
# 1  
Old 11-28-2013
Appending lines in a file

Hello,
I have a file like:
Code:
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
str4,"HEX"H,(534530),info

How can I append lines until it reaches next str1, output like that:
Code:
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,,,,,str4,"HEX"H,(534530),info

They are supposed to be four lines, each with 4 fields.
str1 is always there.
str2, str3 and str4 can be missing.
Is it possible to put commas for the empty spaces as it is marked in red above.

Thanks!

Last edited by radoulov; 04-03-2014 at 12:22 PM..
# 2  
Old 11-28-2013
Code:
awk -F, 'BEGIN {
  p="str1,str2,str3,str4"
  ef = ",,,,,"
  n = split(p, t)
  }
$1 == t[1] && NR > 1 {
  for (i = 0; ++i <= n; ) 
    printf "%s%s", (t[i] in r ? r[t[i]] : ef), (i < n ? OFS : ORS)
  split(x, r)    
  }
{ r[$1] = $0 }
END {
  for (i = 0; ++i <= n; ) 
    printf "%s%s", (t[i] in r ? r[t[i]] : ef), (i < n ? OFS : ORS)
  }' OFS=,  infile

This User Gave Thanks to radoulov For This Post:
# 3  
Old 11-28-2013
Quote:
Originally Posted by radoulov
Code:
awk -F, 'BEGIN {
  p="str1,str2,str3,str4"
  ef = ",,,,,"
  n = split(p, t)
  }
$1 == t[1] && NR > 1 {
  for (i = 0; ++i <= n; ) 
    printf "%s%s", (t[i] in r ? r[t[i]] : ef), (i < n ? OFS : ORS)
  split(x, r)    
  }
{ r[$1] = $0 }
END {
  for (i = 0; ++i <= n; ) 
    printf "%s%s", (t[i] in r ? r[t[i]] : ef), (i < n ? OFS : ORS)
  }' OFS=,  infile

Thank you very much Radoulov, it works perfectly!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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 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... (8 Replies)
Discussion started by: dev.devil.1983
8 Replies

2. 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

3. 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

4. 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

5. UNIX for Dummies Questions & Answers

Help needed in Appending multiple lines

Hello, There is a log file A.log where new lines are getting added every minute. When ever any new lines are getting added in to A , the same lines needs to be appended to B.log I need to append these newly added lines to end of another file B through a shell script. I tried CAT but its... (2 Replies)
Discussion started by: twisterboy
2 Replies

6. Shell Programming and Scripting

Appending number of lines in the file to begining of the file

I am having 6 files named file1,file2....file6 and i need to append number of lines in each file to begining of the file. For example, If file 1 contains a b c d then after adding new line file1 should contain 4 a b c d Thanks in advance. (2 Replies)
Discussion started by: akhay_ms
2 Replies

7. Shell Programming and Scripting

Comparing two files and appending only missing lines.

Hi All, I am a newbie to Shell scripting. Please help me with the Following problem, 1. I have two files with the same name in different locations in the same machine. Eg: /root/testfolder/a ---- location 1 /tmp/testfolder/a ----- location 2 2. I want to compare the files in... (5 Replies)
Discussion started by: Karthick333031
5 Replies

8. 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

9. Shell Programming and Scripting

Appending line/lines with sed

Hi folks, I need to append line or bulk of lines into a file. For example,I have the following section in opmn.xml file: <process-type id="OC4J_RTEadmin_NIR" module-id="OC4J"> <module-data> <category id="start-parameters"> <data... (28 Replies)
Discussion started by: nir_s
28 Replies

10. Shell Programming and Scripting

Appending Consecutive lines

Hi, I have a file containing a single field on every row. What I need is to append one on to the end of another, e.g. The input file looks like this: nnnnn mmmmmm nnnnn mmmmmm I need it to look like this: nnnnn mmmmmm nnnnn mmmmmm Any ideas would be much appreciated,... (8 Replies)
Discussion started by: pondlife
8 Replies
Login or Register to Ask a Question