Combining lines in one line

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Combining lines in one line
# 15  
Old 04-05-2018
Quote:
Originally Posted by scriptor
Hi Rudic
...
Code:
/^1/

--> this will print lines starting with 1. only this part I understand
Not print; it selects lines that start with a 1 (I suggest to also demand a following comma)
Code:
/^1,/

for running the following command, in this case a complete { code block }.
As you found out, it uses the hold space with the commands h H x
The hold space is a bit cumbersome because line 1 and the last line $ need special treatment. (Indeed my solution without the hold space does not need to handle these border cases.)
A bit odd is /^2/{ ... }, that selects lines that start with a 2; shouldn't it be not /^1,/ i.e. /^1,/!{ ... } ?
# 16  
Old 04-05-2018
RudiC's idea (with hold space), optimized, as multi-liner with comments
Code:
# sed -n : no default print
sed -n '
# if line starts with 1, then
  /^1,/ {
# exchange with hold space
    x
# substitute embedded newlines with | and print if successful (in line 1 will not print)
    s/\n/|/gp
# jump to :L
    bL
  }
# else append to hold space
  H
  :L
# if last line then
  $ {
# exchange with hold space (or copy from hold space with g)
    x
# substitute embedded newlines with | and print
    s/\n/|/g; p
  }
' file

And my idea (pattern space only), as multi-liner with comments
Code:
sed '
  :L
# append next line
# (Unix sed: N in the last line exits without default print, needs $b;N or $!N)
  $!N
# if the next line begins with 1, then
  /\n1,/ {
# print and delete the current line; D jumps to next cycle
    P;D
  }
# substitute embedded newline with |
  s/\n/|/
# if successful then jump to :L
  tL
# default print happens here
' file


Last edited by MadeInGermany; 04-05-2018 at 04:31 PM..
# 17  
Old 04-06-2018
Hi Rudic

from your syntax
Code:
sed -n '/^1/ {1h; 1! {x; s/\n//g; p; }}; /^2/ {s/^/|/; H; }; $ {x; s/\n//g; p; } ' file

below is what I still not understand
Code:
{1h; 1!  ---->

not able to understand this part.
what does
Code:
1h

and
Code:
1!

means here and its working
below is what I understand.
Code:
 
 /^2/ {s/^/|/; H; ----> this part select line starting with 2 and appending pipe"|" at the starting.
H is putting this in holding area

Code:
 
 s/\n//g ---> this part replace newline char with empty line

now my confusion is, if below is the file
Code:
 
 cat v
a\nb\nc\nd 
a b c d 
\n
xyx 
 xyz

then when why here sed command is not replacing newline char in this case when I
Code:
sed 's/\n//g' v

may be my question seem silly to you ... but only this way I can clear my doubt. as I do not hv no other means

Last edited by scriptor; 04-06-2018 at 04:04 AM.. Reason: typo error
# 18  
Old 04-06-2018
Quote:
Originally Posted by scriptor
. . .
what does
Code:
1h  -  as said before, you have addresses and commands; here: on first line, copy pattern space to hold space. Multiple addresses possible.

and
Code:
1!  -  ! means negation; some intricacy of sed: perform the command following on any but first line.

. . .
Code:
/^2/ {s/^/|/; H; ----> this part select line starting with 2 and appending pipe"|" at the starting.
H is putting this in holding area     ---  YES

. . . now my confusion is, if below is the file
Code:
 
 cat v
a\nb\nc\nd  - these don't seem to be <new line> chars but combinations / sequences of literal "\" char and "n" char.
a b c d     -  <new line> is the ascii char 0x0D, see ascii table.
\n
xyx 
 xyz

. . .
This User Gave Thanks to RudiC For This Post:
# 19  
Old 04-06-2018
A \n an embedded newline character (not two consecutive characters \ and n).
sed loops over the lines in the input file, so the sed code only has one line in its "pattern space".
The \n character is only created after an append command like N or H or G or a.
Normally an RE can handle only one line, and ^ and $ mark the beginning and end of the line.
For example a line
Code:
line1

The RE can see
Code:
^line1$

After an append there is a \n between the two lines, like this
Code:
line1\nline2

An RE can see it like
Code:
^line1\nline2$


Last edited by MadeInGermany; 04-06-2018 at 04:41 AM..
This User Gave Thanks to MadeInGermany For This Post:
# 20  
Old 04-09-2018
Post#10 shows a very interesting approach by MadeInGermany, both in method as well as esp. in script length. Here's a revised version of mine stripped down to minimum length:
Code:
sed -n '/^1/bL; {s/^/|/;H;}; ${:L;x;s/\n//gp;}' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Log4j combining lines to single line

Hi, Our log4j file contents look like this: 2018-11-20T00:06:58,888 INFO ql.Driver: Executing command(queryId=hive_20181120000656_49af4ad0-1d37-4312-872c-a247ed80c181): CREATE TABLE RESULTS.E7014485_ALL_HMS_CAP1 AS SELECT name,dept from employee Where employee='Jeff'... (4 Replies)
Discussion started by: wahi80
4 Replies

2. Shell Programming and Scripting

Combining lines into a single line

i have a file (where the column values are separated by ' and the text can be enclosed in ~) which contains data in form of 4461,2,~Basic: 2 Years/Unlimited Miles Drivetrain: Gas Engine 2 Years/Unlimited Miles Duramax Engine 3 Years/Unlimited... (2 Replies)
Discussion started by: rahulchandak
2 Replies

3. Shell Programming and Scripting

Combining two lines into one, UNIX

Hi All, I have a file which has the following sample lines -- <Member name="Canada" Currency="CAD" -- <Member name="UK" Currency="GBP" -- <Member name="Switzerland" Currency="CHF" -- <Member name="Germany" Currency="EUR" -- (11 Replies)
Discussion started by: dev.devil.1983
11 Replies

4. Shell Programming and Scripting

Combining multiple block of lines in one comma separated line

Hi Everyone, On my Linux box I have a text file having block of few lines and this block lines separated by one blank line. I would like to format and print these lines in such a way that this entire block of lines will come as single comma separated line & again next block of lines in next... (7 Replies)
Discussion started by: gr8_usk
7 Replies

5. Shell Programming and Scripting

Combining lines in to one line

Hi Friends, I have a file1.txt 1001 jkilo yrhfm 200056 jhdf rjhwjkrh 3+u8jk5h3 uru ehjk 1002 jkfhk hfjkd 2748395 fdjksfh hefjkh 3hdfk ejkh kjhjke In the above if you see the firt charcter of each line mentioned in red has a pattern . I need to create another file where , the... (6 Replies)
Discussion started by: i150371485
6 Replies

6. Shell Programming and Scripting

Reading two lines in a while loop and combining the lines

Dear all, I have a file like this: imput scaffold_0 1 scaffold_0 10000 scaffold_0 20000 scaffold_0 25000 scaffold_1 1 scaffold_1 10000 scaffold_1 20000 scaffold_1 23283 and I want the output like this: scaffold_0 1 scaffold_0 10000 scaffold_0 10000 scaffold_0 20000... (6 Replies)
Discussion started by: valente
6 Replies

7. Programming

PERL:Combining multiple lines to single line

Hi All I need a small help for the below format in making a small script in Perl or Shell. I have a file in which a single line entries are broken into three line entries. Eg: I have a pen and notebook. All i want is to capture in a single line in a separate file. eg: I have a pen and... (4 Replies)
Discussion started by: Kalaiela
4 Replies

8. Shell Programming and Scripting

Combining 2 lines in a file into 1 line

Hi all, I have a file with lot of lines with repeating pattern. ( TABLE_NAME line followed by Total line). I would like combine these two lines into one line seperated by cama and create a new file. Is there a simple way to do this. Current Format ( just a sample 4 lines ) TABLE_NAME:... (10 Replies)
Discussion started by: MKNENI
10 Replies

9. Shell Programming and Scripting

Combining lines between two specific lines

Hi, I have a requirement like following: I have input file like: Question: 1 ----Multiple choice--- What is your favourite colour? Options: a) red b) blue c) none of these Question: 2 ---Multiple choice----- In which month did you join your first job? Options: a) Jan b) Feb c)... (11 Replies)
Discussion started by: ppatra
11 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