Concatenating lines in bash


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Concatenating lines in bash
# 1  
Old 05-06-2010
Concatenating lines in bash

Hi,

I'm attempting to join two lines in a file which are separated by a line break. The file contents are shown below:
Code:
event_id=0
id=0_20100505210853
IFOconfig=HLV
template=TaylorF2
Nlive=1000.0
Nruns=1.0
NIFO=3
injectionXML_path=/home/rorysmith/Projects/SpinStudy/HL-INJECTIONS_1-933374636-10000.xml
param_names=['logM', 'eta', 'time', 'phi', 'distMpc', 'long', 'lat', 'psi', 'iota']
results_dir=/home/rorysmith/WWW/SpinStudy
events_page=0/HLV
data_path=/home/rorysmith/Projects/SpinStudy/0/HLV
data_files="output.0.dat"
Nruns_actual=1
timestamp_runs=
timestamp_results=20100505210853
inspnest_cmd=
gitID_runs=
gitID_results=
NOISE_WINDOW=
Deta=
Mmin=
Mmax=
DT=0.2
sub_IFOconfig=['LV', 'H', 'HL', 'V', 'L', 'HV']
other_IFOconfig=['LV', 'H', 'HL', 'V', 'HLV', 'L', 'HV']
injF_file_list=[]
injT_file_list=[]
templateF_file_list=[]
injSNR=[('H1', 3.667618989944458) ('L1', 5.1920042037963867)
 ('V1', 1.9253799915313721)]
H_TYPE=LALLIGO
L_TYPE=LALLIGO
V_TYPE=LALVirgo
H_CHANNEL=LALLIGO
L_CHANNEL=LALLIGO
V_CHANNEL=LALVirgo
N_SEGS=400
SAMPLING_RATE=1024
NMCMC=100
INJ_NO=0

where the two lines to be joined are

Code:
injSNR=[('H1', 3.667618989944458) ('L1', 5.1920042037963867)
 ('V1', 1.9253799915313721)]

I've been following similar problems which people have resolved using sed or awk, however they seem to mainly focus on removing all line breaks.

What I'm ideally looking for is a way to specify which line break to remove, perhaps by line number if this is possible?...

Last edited by Franklin52; 05-06-2010 at 12:23 PM.. Reason: Please use code tags!
# 2  
Old 05-06-2010
you can do it by sed or awk..
try this..
Code:
awk '{if($0 ~ /^injSNR/){printf "%s ",$0;getline;print $0}else{print}}' filename

# 3  
Old 05-06-2010
Code:
line_no=30
sed "${line_no}N;s/\n//;" file

# 4  
Old 05-06-2010
Thanks vidyadhar85, this works really well!
# 5  
Old 05-06-2010
A sed one:

Code:
sed "/^injSNR=/{N;s/\n//;}" inputfile

# 6  
Old 05-06-2010
Quote:
Originally Posted by Supersymmetric
Thanks vidyadhar85, this works really well!
Okay, so if i write the output of the command

awk '{if($0 ~ /^injSNR/){printf "%s ",$0;getline;print $0}else{print}}'

to a file, viewing it with more displays the contents without the line break this is supposed to remove, however vim still sees the line break.
# 7  
Old 05-06-2010
Either you are looking at the wrong file or that "line break" consists of something other than a single linefeed character. If the latter is the problem, it would help if you posted the contents of the lines in question in an unambiguous format.

If the line in question is line 11, you could print out lines 10 through 12, thusly (note that the last character in the sed command is a lower case ell):
Code:
sed -n '10,12 l'

Regards,
Alister
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Concatenating two mutiline variables in a bash

Hi All, I am having a situation where am capturing results in two variables from an xml file. However, I am looking to print those two variables with pipe in between them and these variable are multi-line. This is how my 1st variable looks like: 20181225010 20190224010 20190224010... (8 Replies)
Discussion started by: svks1985
8 Replies

2. Shell Programming and Scripting

Concatenating strings and run it in bash

Hi, all, I tried to write a simple shell script as follow: #!/bin/bash # What want to do in bash is following # : pcd_viewer cloud_cluster_0.pcd cloud_cluster_1.pcd cloud_cluster_2.pcd cloud_cluster_3.pcd cloud_cluster_4.pcd STR = "pcd_viewer" for i in `seq 0 4` do STR... (1 Reply)
Discussion started by: bedeK
1 Replies

3. Shell Programming and Scripting

Concatenating the lines of a data

I have a data of 1 lac lines with the following format abcde,1,2,3,4, ,ee ,ff ,gg ,hh ,mm abcde,3,4,5,6, ,we ,qw ,as ,zx ,cf abcde,1,5,6,7, ,dd ,aa ,er .... .... (6 Replies)
Discussion started by: aravindj80
6 Replies

4. Shell Programming and Scripting

Concatenating more than two lines into one based on some columns

Hi, I need to concatenate some lines in a file based on the First 4 coloumns of a file .. (For Eg.) Consider a file ... I,01,000002,0666,00000.00,000,00,000,000, ,0 I,01,000002,0667,00000.00,000,00,000,000, ,0 I,01,000002,0666,00056.10 I,01,000002,0667,00056.10 I,01,000002,0666,00001... (6 Replies)
Discussion started by: Sri3001
6 Replies

5. Shell Programming and Scripting

bash -- concatenating values from variables

Hi This is a simple one but I got a lost in translation when doing. What I want to do, given both variables in the example below, to get one value at the time from both variables, for example: 1:a 2:b etc... I need to get this in bash scripting code: varas="1 2 3 4" varbs="a b c d"... (4 Replies)
Discussion started by: ranmanh
4 Replies

6. Shell Programming and Scripting

Concatenating lines ending with '+' using awk

Hi, I have an ASCII text file where some of the lines are ending with '+' character. I have to concatenate the next successive line with those lines having the trailing '+' char by removing that char. The below awk code has some problems to do this task: awk '{while(sub(/\+$/,"")) {... (12 Replies)
Discussion started by: royalibrahim
12 Replies

7. Shell Programming and Scripting

awk: switching lines and concatenating lines?

Hello, I have only recently begun with awk and need to write this: I have an input consisting of a couple of letters, a space and a number followed by various other characters: fiRcQ 9( ) klsRo 9( ) pause fiRcQ 9( ) pause klsRo continue 1 aPLnJ 62( ) fiRcQ continue 5 ... and so on I... (7 Replies)
Discussion started by: Borghal
7 Replies

8. Shell Programming and Scripting

Concatenating the lines with different pattern

Hi, I have put a similar question in one of the other threads through which I got the solution shown below but I have some more condition to add to it, hence have further queries on it. I appologies if I should be putting this with the old thread. I have a file which perform a grep on the... (1 Reply)
Discussion started by: simi28
1 Replies

9. Shell Programming and Scripting

Concatenating the two lines in a file

hi My requirement is i have a file with some records like this file name ::xyz a=1 b=100,200 ,300,400 ,500,600 c=700,800 d=900 i want to change my file a=1 b=100,200,300,400 c=700,800 d=900 if record starts with " , " that line should fallows the previous line.please give... (6 Replies)
Discussion started by: srivsn
6 Replies

10. Shell Programming and Scripting

Concatenating lines and formatting.

Hi, I have a html file which is unformatted and need to concatenate the lines between each "table" statement in order to run an awk statement on it. Here is the example of the raw file: <table border="0" cellspacing="0" cellpadding="0" class="playerDetails"> def ... (3 Replies)
Discussion started by: Tonka52
3 Replies
Login or Register to Ask a Question