Merge lines until known character reached


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Merge lines until known character reached
# 1  
Old 08-30-2017
Merge lines until known character reached

Hello all,

Hopefully this should be an easy one for you. I have a file that contains a list of parameters and their values. Some of these values are 2D arrays split over several lines, I need to move these so the parameter name and it's full value is all on one line. The parameters are separated by semi-colons.

That is I have a file that looks like the following:

Code:
param_a = 1 2;
 param_b = 3 4 
 5 6;
 param_c = 5;
 param_d = 6
 7
 8
 9;

etc

What I need for output is

Code:
param_1 = 1 2;
 param_b = 3 4 5 6;
 param_c = 5;
 param_d = 6 7 8 9;

I have the following so far but it doesn't work as it ignores the lines that contain the semi-colon.

Code:
awk '/;/{if (NR!=1)print "";next}{printf "%s ",$0}END{print "";}' testfile >> testfileout


Last edited by vgersh99; 08-30-2017 at 10:56 AM.. Reason: code tags, please!
# 2  
Old 08-30-2017
something like this:
Code:
awk '{printf("%s%s", $0, (/;/)?ORS:"")}' myFile

# 3  
Old 08-30-2017
That does the job perfectly. Thanks. Any chance you could explain it? My awk is terrible

Also would I need to pipe the output to a new file or is there any way to make the changes directly in the original file?
# 4  
Old 08-30-2017
Code:
awk '{printf("%s%s", $0, (/;/)?ORS:" ")}' myFile

For each record/line, printf with 2 parameters (%s%s):
  1. First parameter is the line/record itself ($0)
  2. For the second parameter, if the line contains a ; , output ORS (by default EndOfLine); if not - output a space " "
To save result in the same files, there're many ways to skin that cat:
Code:
awk '{printf("%s%s", $0, (/;/)?ORS:"")}' myFile >| /tmp/$$ && mv /tmp/$$ myFile

OR
Code:
 {rm myFile; awk '{printf("%s%s", $0, (/;/)?ORS:"")}' >| myFile; } < myFile

# 5  
Old 08-30-2017
Thanks. really appreciated.
# 6  
Old 08-30-2017
Try also
Code:
awk '{while (!(/;/)) {getline X; $0=$0 X}} 1' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Merge Lines

Hello I have an input as below this is test we are( ) one end of description I am looking for output this is test we are () one end of description (2 Replies)
Discussion started by: Tomlight
2 Replies

2. UNIX for Dummies Questions & Answers

How to merge every n lines into one line?

I want to merge every 16 lines into one line, how can I do that? My file looks like below: 0 . 2 2 . 0 0 . 0 0 . 0 0 . 0 0 0 0 0 (2 Replies)
Discussion started by: ml4me
2 Replies

3. Shell Programming and Scripting

Merge lines

Hello I have a file with CAR 23 COLOR 12 CAR 44 COLOR 12 CAR 55 COLOR 20 SIZE BIG CAR 56 CAR 57 COLOR 11 How can merge the CAR and the COLOR + SIZE (if there are COLOR,SIZE) CAR 23 COLOR 12 CAR 44 COLOR 12 CAR 55 COLOR 20 SIZE BIG CAR 56 CAR 57 COLOR 11 Every line begin in... (4 Replies)
Discussion started by: sharong
4 Replies

4. Shell Programming and Scripting

Grep the word from pattern line and update in subsequent lines till next pattern line reached

Hi, I have got the below requirement. please suggest. I have a file like, Processing Item is: /data/ing/cfg2/abc.txt /data/ing/cfg3/bgc.txt Processing Item is: /data/cmd/for2/ght.txt /data/kernal/config.klgt.txt I want to process the above file to get the output file like, ... (5 Replies)
Discussion started by: rbalaj16
5 Replies

5. Shell Programming and Scripting

remove blank lines and merge lines in shell

Hi, I'm not a expert in shell programming, so i've come here to take help from u gurus. I'm trying to tailor a csv file that i got to make it work for the LOAD FROM command. I've a datatable csv of the below format - --in file format xx,xx,xx ,xx , , , , ,,xx, xxxx,, ,, xxx,... (11 Replies)
Discussion started by: dvah
11 Replies

6. UNIX for Dummies Questions & Answers

To merge a few lines to 1 line

Hi Experts, This is my input file. input.txt 0 /dev/fd 25 /var 1 /tmp 1 /var/run 1. If this file has single line, then leave it, print the single line else merge the 4 lines above into 1 line as below e.g (6 Replies)
Discussion started by: streddy
6 Replies

7. Shell Programming and Scripting

merge lines

Hi guys in input every 1st line 1st ID value located in 2nd line 1st occurrence . I need to print them down accordingly.. Thanx in advance input rs1040480_XXXXX.value rs1040481_YYYYY.value rs1040482_TXXXX.value 0.7408157 0.3410044 0.7408157 ... (7 Replies)
Discussion started by: stateperl
7 Replies

8. UNIX for Dummies Questions & Answers

want to merge two consecutive lines.

Hi All, I want to merge two consecutive lines. Currently the output is :--> crmplp1 cmis461 No Online cmis462 No Offline crmplp2 cmis462 No Online cmis463 No ... (6 Replies)
Discussion started by: pank29
6 Replies

9. Shell Programming and Scripting

Merge two lines

Hi I have two lines of data formatted as displayed below shop, price, remark, date a,#N/A,order to -fd, 20091011 and would like it to be shop:a price:#N/A remark:order to -fd date:20091011 How can I do it? Many thanks (2 Replies)
Discussion started by: lalelle
2 Replies

10. Shell Programming and Scripting

Merge lines into one

Source data file from oracle, terminated by ",". 'Cause some of fields have \r\n, then those lines were splitted into multiple lines in the expoted data file. Just vi this file, and found ^M. How to concatenate these line into one if it has a ^M at then end. thanks, (7 Replies)
Discussion started by: anypager
7 Replies
Login or Register to Ask a Question