| |
|
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.
|
 |

01-07-09, 07:23
|
|
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
|
|

01-07-09, 10:55
|
|
Registered User
|
|
Join Date: Apr 2004
Location: Boston, MA
Posts: 325
|
|
Code:
nawk -v RS=';' '$1=$1' myFile
__________________
vlad
+-----------------------+
| #include <disclaimer.h> |
+-----------------------+
|
|

01-07-09, 11:29
|
|
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
|
|

01-07-09, 11:51
|
|
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> |
+-----------------------+
|
|

01-07-09, 12:02
|
|
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
|
|

01-07-09, 12:10
|
|
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> |
+-----------------------+
|
|

01-08-09, 05:53
|
|
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
|
|

01-08-09, 11:12
|
|
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> |
+-----------------------+
|
|

01-08-09, 11:43
|
|
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
|
|

01-08-09, 11:43
|
|
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
|
|

01-08-09, 11:58
|
|
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> |
+-----------------------+
|
|

01-08-09, 12:02
|
|
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.
|
|

01-08-09, 12:09
|
|
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
|
|

01-08-09, 12:13
|
|
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> |
+-----------------------+
|
|

01-08-09, 14:24
|
|
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
|
|
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|