Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

concatenate lines using shell scripting

Shell Programming and Scripting


Tags
shell script

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 09-06-2009
dtdt dtdt is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 24 January 2015, 10:15 PM EST
Posts: 33
Thanks: 1
Thanked 0 Times in 0 Posts
concatenate lines using shell scripting

i have a mega file in this format:

a,
b,
c,
d,
=
a2,
b2,
c2,
d2,
=
a3,
b3
=

i want to combine lines until = meet. the result should be :
a,b,c,d,
a2,b2,c3,d2
a3,b3

need your help. thanks
Sponsored Links
    #2  
Old Unix and Linux 09-06-2009
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 June 2017, 5:05 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,446
Thanks: 100
Thanked 1,509 Times in 1,119 Posts
You can of course do this via shell scripting, but using sed would be a faster and by far clearer way to do it.

Why do you want to do it with a shell script?

bakunin
Sponsored Links
    #3  
Old Unix and Linux 09-06-2009
dtdt dtdt is offline
Registered User
 
Join Date: Sep 2009
Last Activity: 24 January 2015, 10:15 PM EST
Posts: 33
Thanks: 1
Thanked 0 Times in 0 Posts
sorry, i didn't mean that. sed or awk are all good.

---------- Post updated at 01:11 PM ---------- Previous update was at 12:58 PM ----------

would you please show me how to do it in sed or awk? thx
    #4  
Old Unix and Linux 09-06-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 123 Times in 116 Posts

Code:
awk '/=/{$0="\n"}1' ORS="" file

Sponsored Links
    #5  
Old Unix and Linux 09-06-2009
protocomm protocomm is offline
Registered User
 
Join Date: Mar 2009
Last Activity: 27 December 2015, 12:35 PM EST
Posts: 310
Thanks: 35
Thanked 25 Times in 25 Posts
perhaps.....bur certainly better....


Code:
awk '{if($0 !~ /=/) ORS=""}{if($0 ~/=/) ORS="\n"} {print}' | sed -e 's/=//g' -e 's/,$//g' file

Sponsored Links
    #6  
Old Unix and Linux 09-06-2009
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 22 June 2017, 5:05 AM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,446
Thanks: 100
Thanked 1,509 Times in 1,119 Posts
in sed it is easy: we have two types of lines, the ones reading "=" and the others. When we encounter a "="-line, we want to print out what we have so far, minus the newlines. If we encounter one of the other lines we want to store its contents until we encounter a "="-line.

sed has a so-called "hold space", think of it as a variable, where you can store things until you need them. We append everything to this hold space until we encounter a "="-line, then we recall the hold space, filter out all embedded newlines and print it, then start over.

In the following script i have put in comment for your understanding, remove them, because sed doesn't allow inline commenting in scripts. Furthermore, you can put the whole script on one line, replacing linefeeds with semicolons:


Code:
sed -n '/^=/ {                   # if a line starts with "="
          s/.*//                 # delete this lines content
          x                      # exchange the pattern space (empty) and the hold space
          s/\n//g                # delete newlines
          p                      # then print what you have
     }
     /^=/ ! {                    # if a line doesn't start with "="
          H                      # append it to the hold space
     }' /your/file > newfile

for short:


Code:
sed -n '/^=/{s/.*//;x;s/\n//gp};/^=/!{H}' /your/file > newfile

I hope this helps.

bakunin
Sponsored Links
    #7  
Old Unix and Linux 09-06-2009
danmero danmero is offline Forum Advisor  
 
Join Date: Nov 2007
Last Activity: 31 July 2016, 9:42 AM EDT
Location: H3X
Posts: 2,163
Thanks: 11
Thanked 123 Times in 116 Posts
Quote:
Originally Posted by protocomm View Post
perhaps.....bur certainly better....
Did you check the OP requirements Linux , certainly not.
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
concatenate log file lines up to timestamp AlanC Shell Programming and Scripting 4 06-29-2009 11:38 AM
Concatenate lines in a single paragraph cgkmal Shell Programming and Scripting 4 05-17-2009 01:02 AM
sed / awk to concatenate lines until blank line dunstonrocks Shell Programming and Scripting 11 03-30-2009 11:11 PM
How to concatenate consecutive lines shivi707 UNIX Desktop Questions & Answers 1 01-12-2009 08:08 AM
need to concatenate two lines if the line doesnt end with quotes laxmi131 UNIX for Advanced & Expert Users 9 10-27-2008 07:22 AM



All times are GMT -4. The time now is 09:45 AM.