Visit Our UNIX and Linux User Community


join lines on line break in files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting join lines on line break in files
# 1  
Old 07-31-2009
join lines on line break in files

i had a file where lines appear to be broken when they shouldn't
eg
Line 1. kerl abc sdskd sdsjkdlsd sdsdksd \
Line 2. ksdkks sdnjs djsdjsd

i can do a shift join to combine the lines but i there are plenty of files with this issue
Line 1. kerl abc sdskd sdsjkdlsd sdsdksd ksdkks sdnjs djsdjsd
i need a script that files as input and fix the line break wherever it appears
Please Advice
# 2  
Old 07-31-2009
try..
Code:
-bash-3.2$ sed '{:q;N;s/\\\n//g;t q}' file
kerl abc sdskd sdsjkdlsd sdsdksd ksdkks sdnjs djsdjsd
-bash-3.2$

# 3  
Old 07-31-2009
thanks but i am getting the error
sed: The label {:q;N;s/\\\n//g;t q} is greater than eight characters.

also
the file is having \ in between the words also, there i have not to join
eg
abc sdhsdkjhdks \
sdjladjlksadlsasa
jsdksd ghsjdjsdjid
dsdsdkljs\ shkdkshddkksd

output

abc sdhsdkjhdks sdjladjlksadlsasa
jsdksd ghsjdjsdjid
dsdsdkljs\ shkdkshddkksd
# 4  
Old 07-31-2009
try...
Code:
-bash-3.2$ sed -e ':a;N;$!ba;s/\\\n//g' -e 's/\\//g' file
abc sdhsdkjhdks sdjladjlksadlsasa
jsdksd ghsjdjsdjid
dsdsdkljs shkdkshddkksd
-bash-3.2$

# 5  
Old 07-31-2009
With non GNU sed, commands must ne on separate lines :
Code:
sed -e ':a
N
$!ba
s/\\\n//g
' inputfile

Jean-Pierre.
# 6  
Old 07-31-2009
Thanks , worked super. can you kindly elborate on the awk script how it was doneSmilie
# 7  
Old 07-31-2009
A solution with awk :
Code:
awk '/\\$/ {printf "%s",substr($0, 1, length-1);next} 1' inputfile

Jean-Pierre.

Previous Thread | Next Thread
Test Your Knowledge in Computers #142
Difficulty: Easy
Version 4 Unix was rewritten in C in 1977.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Break line content into multiple lines using delimiter

I need to break the line after every 3rd semi colon(;) using Unix shell scripting Input.txt ABC;DEF;JHY;LKU;QWE;BVF;RGHY; Output.txt ABC;DEF;JHY; LKU;QWE;BVF; RGHY; (1 Reply)
Discussion started by: meet_calramz
1 Replies

2. Shell Programming and Scripting

Enumerate lines until each line break using awk

Hi, I have the following data: This this DT 0.99955 0 4 is be VBZ 1 5 7 sentence sentence NN 0.916667 8 16 one one NN 0.545078 17 20 . . Fp 1 20 21 This this DT 0.99955 22 26 is be VBZ 1 27 29 the the DT 1 30 33 second 2 JJ 0.930556 34 40 sentence sentence NN 0.916667 41 49... (1 Reply)
Discussion started by: owwow14
1 Replies

3. Shell Programming and Scripting

Join two lines into one, but the second line only the last two columns

Hi guys, I hope you are doing well! I have a file and I need to join two lines into one, but the second line I need only the last two columns. ================= "eHealth Trend Report","logoRpt" "LAN/WAN Group 123" "Divide by Time" "switch1_a-RH-Serial0" "BW: 1.02 M" ... (4 Replies)
Discussion started by: antoniorajr
4 Replies

4. Shell Programming and Scripting

Join common patterns in multiple lines into one line

Hi I have a file like 1 2 1 2 3 1 5 6 11 12 10 2 7 5 17 12 I would like to have an output as 1 2 3 5 6 10 7 11 12 17 any help would be highly appreciated Thanks (4 Replies)
Discussion started by: Harrisham
4 Replies

5. Shell Programming and Scripting

Join lines from two files based on match

I have two files. File1 >gi|11320906|gb|AF197889.1|_Buchnera_aphidicola ATGAAATTTAAGATAAAAAATAGTATTTT >gi|11320898|gb|AF197885.1|_Buchnera_aphidicola ATGAAATTTAATATAAACAATAAAA >gi|11320894|gb|AF197883.1|_Buchnera_aphidicola ATGAAATTTAATATAAACAATAAAATTTTT File2 AF197885 Uroleucon aeneum... (2 Replies)
Discussion started by: pathunkathunk
2 Replies

6. Shell Programming and Scripting

Join fields from files with duplicate lines

I have two files, file1.txt: 1 abc 2 def 2 dgh 3 ijk 4 lmn file2.txt 1 opq 2 rst 3 uvw My desired output is: 1 abc opq 2 def rst 2 dgh rst 3 ijk uvw (2 Replies)
Discussion started by: xan.amini
2 Replies

7. Shell Programming and Scripting

join based on line number when one file is missing lines

I have a file that contains 87 lines, each with a set of coordinates (x & y). This file looks like: 1 200.3 -0.3 2 201.7 -0.32 ... 87 200.2 -0.314 I have another file which contains data that was taken at certain of these 87 positions. i.e.: 37 125 42 175 86 142 where the first... (1 Reply)
Discussion started by: jackiev
1 Replies

8. Shell Programming and Scripting

Break lines up into single lines after each space in every line

It sounds a bit confusing but what I have is a text file like the example below (without the Line1, Line2, Line3 etc. of course) and I want to move every group of characters into a new line after each space. Example of text file; line1 .digg-widget-theme2 ul { background: rgb(0, 0, 0) none... (7 Replies)
Discussion started by: lewk
7 Replies

9. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies

10. Shell Programming and Scripting

Break one line to many lines using awk

Break one line to many lines using awk The below code works but i want to implement without combining field 2 and 3 and then splitting i would like to do this in one command instead of writing multiple commands and creating multiple lines. nawk -F"|" '{print $1,$2SUBSEP$3}' OFS="|" file >... (16 Replies)
Discussion started by: pinnacle
16 Replies

Featured Tech Videos