Combining lines in one line

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers Combining lines in one line
# 8  
Old 04-05-2018
This minor extension of Ravindersingh13's proposal prints even the requested | separator:
Code:
awk '{printf("%s%s%s",/^2/?"|":"", /^1/ && FNR>1?ORS:"",$0)} END{print ""}' file

EDIT: or even
Code:
awk '{printf("%s%s",/^2/?"|":/^1/ && FNR>1?ORS:"",$0)} END{print ""}' file

EDIT: or even
Code:
awk '{printf("%s%s",/^1/?DL:"|",$0); DL=ORS} END{print ""}' file

# 9  
Old 04-05-2018
Hi Rudic

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

in this can you please explain the working of below part.
Code:
/^1/

--> this will print lines starting with 1. only this part I understand
Code:
1h;

Code:
1!

Code:
{x; s/\n//g; p; }
/^2/

Code:
{s/^/|/; H; };

Code:
{x; s/\n//g; p;

whoever I googled and found below things but still scratching my head to understand.
Code:
(h)function copies the contents of the pattern space into a holding area 
(g) function copies the contents of the holding area into the pattern space, 
destroying the previous contents of the pattern space
(H) function appends the contents of the pattern space to the contents of the holding area.
(x)function interchanges the contents of the pattern space and the holding area.

also I will be grateful to your if suggest me how should I also learnt or understand so that I can too build similar 1 line coding.

---------- Post updated at 04:52 PM ---------- Previous update was at 04:48 PM ----------

HI Ravinder,

in your syntax
Code:
awk '{printf("%s%s",$0~/^1/ && FNR>1?ORS:"",$0)} END{print ""}'

what is the working of
Code:
$0~

over net I found it means
Code:
represents your home folder

but this doesn't fit in this case I guess.

Last edited by RudiC; 04-05-2018 at 08:58 AM..
# 10  
Old 04-05-2018
Another one-liner with sed (all versions)
Code:
sed -e ':L' -e '$!N;/\n1,/{P;D;}' -e 's/\n/|/;tL' file

Better readable as multi-liner
Code:
sed '
  :L
  $!N
  /\n1,/{
    P;D
  }
  s/\n/|/
  tL
' file

This User Gave Thanks to MadeInGermany For This Post:
# 11  
Old 04-05-2018
thx everyone
if guys help me understand this .
I am scratching my head since morning to understand Rudic's one liner
but fails.
# 12  
Old 04-05-2018
Quote:
Originally Posted by scriptor
Hi Rudic

you suggestion works
Thank you.
Quote:
. . . in this can you please explain the working of below part.
Explaining sed's intricate operation in depth exceeds my language capabilities as well probably space provided in here; on top, there's many texts on the topic in them there internet sites... once you're finished reading sed's man and / or info pages as the principle sources of information.
In short, sed has a pattern space and a hold space; on the former all commands operate upon, the latter is only copied and / or appended to / from, or exchanged. The commands can be influenced by (ranges of) addresses, which themselves can be regex (important: man regex!) matches like /pattern/ (/^1/ matches a char "1" in the first place of a line) or line numbers (1 is the first line in an input stream). Don't mix up the two! The most powerful sed command is s(ubstitute): s/\n//g globally substitutes the regex pattern \n (escape sequence interpreted as a <new line> char) with the empty string, effectively removing it.

Quote:
. . . also I will be grateful to your if suggest me how should I also learnt or understand so that I can too build similar 1 line coding . . .
L1 - reading - exercise - reading - exercise - goto L1
# 13  
Old 04-05-2018
thx a lot Rudic for valuable suggestion.

however if you can only explain your syntax in details I will very thankful to you
Code:
sed -n '/^1/ {1h; 1! {x; s/\n//g; p; }}; /^2/ {s/^/|/; H; }; $ {x; s/\n//g; p; } ' file

# 14  
Old 04-05-2018
How about you try to explain it as far as you get, and I / we will jump in and fill in the gaps and / or correct misperceptions. It might be worthwhile to use a paper slip and sketch pattern and hold space and their contents?
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