If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Unix Shell Scripts > merge lines based on specific last pattern of a line

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 01-07-09, 07:23
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
merge lines based on specific last pattern of a line

pls advise how to merge the following lines (by sed command)

1;
2
3
4
5;
6
7;

to becomes
1
2345
67
Reply With Quote
  #2 (permalink)  
Old 01-07-09, 10:55
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
nawk -v RS=';' '$1=$1' myFile
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #3 (permalink)  
Old 01-07-09, 11:29
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
thx vgersh99 ... how about

1;
2
3
4
;5;
6
7;

expected result:1) 2) no data changed except lines merging 2) only last ; of each merged line is replaced
=>
1
234;5
67
Reply With Quote
  #4 (permalink)  
Old 01-07-09, 11:51
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
nawk '{a=(a=="")? $0 : a OFS $0}; /;$/ { print substr(a, 1, length(a)-1);a=""}' myFile
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #5 (permalink)  
Old 01-07-09, 12:02
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
woo... but donno why my result contains space between fields ... pls advise
os : sun unix (soloris)

mine:
1
2 3 4 ;5
6 7
Reply With Quote
  #6 (permalink)  
Old 01-07-09, 12:10
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
nawk '{a=(a=="")? $0 : a OFS $0}; /;$/ { gsub(OFS, "", a);print substr(a, 1, length(a)-1);a=""}' myFile
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #7 (permalink)  
Old 01-08-09, 05:53
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
sorry vgersh99, i mislead you .. my problem is merge the broken line due to content in text file:
e.g
1st data line <EOL>
2nd data
line <EOL>
3rd
data
line <EOL>
4th data line <EOL>
=>
1st data line <EOL>
2nd data line <EOL>
3rd data line <EOL>
4th data line <EOL>

pls advise
Reply With Quote
  #8 (permalink)  
Old 01-08-09, 11:12
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Given the previous solution, what have you tried and where exactly are you stuck?
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #9 (permalink)  
Old 01-08-09, 11:43
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
$cat tmp3
1st data line <EOL>
2nd data
line <EOL>
3rd
data
line <EOL>
4th data line <EOL>

command:
nawk '{a=(a=="")? $0 : a OFS $0}; /;$/ { gsub(OFS, "", a);print substr(a, 1, length(a)-1);a=""}' tmp3
=>
no result

command:
nawk '{a=(a=="")? $0 : a OFS $0}; />$/ { gsub(OFS, "", a);print substr(a, 1, length(a)-1);a=""}' tmp3
=>
1stdataline<EOL
2nddataline<EOL
3rddataline<EOL
4thdataline<EOL
Reply With Quote
  #10 (permalink)  
Old 01-08-09, 11:43
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
This might work :
Code:
cat your_file | gawk '{
                split( $0, a, "" )
                for( v in a ) {
                        if ( a[v] >= 0 && a[v] <=9 ) printf a[v]
                        if ( a[v] == ";" ) print "\n"
                }
        }' | grep "."
But it assumes you only want to print out the chars 0-9.

Mike
Reply With Quote
  #11 (permalink)  
Old 01-08-09, 11:58
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
Code:
nawk -v sep='<EOL>' '{a=(a=="")? $0 : a OFS $0}; $NF ~ (sep "$") { print substr(a, 1, length(a) - length(sep));a=""}' myFile
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #12 (permalink)  
Old 01-08-09, 12:02
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
oh but my unix version has no gawk command... btw, can sed work ? checked from sed command, it can allow us to merge line selectively but donno why to code.
Reply With Quote
  #13 (permalink)  
Old 01-08-09, 12:09
ymho ymho is offline
Registered User
 
Join Date: Jul 2006
Posts: 109
Quote:
Originally Posted by vgersh99
Code:
nawk -v sep='<EOL>' '{a=(a=="")? $0 : a OFS $0}; $NF ~ (sep "$") { print substr(a, 1, length(a) - length(sep));a=""}' myFile
revised as following in order to keep <EOL>

nawk -v sep='<EOL>' '{a=(a=="")? $0 : a OFS $0}; $NF ~ (sep "$") { print a;a=""}' tmp3
=>
1st data line <EOL>
2nd data line <EOL>
3rd data line <EOL>
4th data line <EOL>
=>
extra spaces found b/w "2nd data " & "3rd..." line
Reply With Quote
  #14 (permalink)  
Old 01-08-09, 12:13
vgersh99 vgersh99 is offline
Registered User
 
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
not sure what you're implying and/or what to change.
If you're looking to get rid of the extra spaces, look into the previous solution (hint: use 'gsub').

In the future, please use the vB Codes when posting data or code samples.
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
Reply With Quote
  #15 (permalink)  
Old 01-08-09, 14:24
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
gawk and awk are very similar, both are usually available with gawk has more features. I've rewritten the code extensively to use awk :
Code:
cat your_file | awk '{
                split( $0, a, "" )
                for( v in a ) {
                        if ( a[v] >= 0 && a[v] <=9 ) printf a[v]
                        if ( a[v] == ";" ) print "\n"
                }
        }' | grep "."
Mike
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On