Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Awk to Sed alternative 1

Status
Not open for further replies.

dickiebird

Programmer
Feb 14, 2002
758
GB
Guys....
Already posted on Unix scripting - to no avail.
I have an awk solution (has to be awk on aix3.2.5 - no nawk/gawk on this old box)which occasionally fails when the text is > 3000 bytes.(Awk limitation circa 1963 ???)
Could someone show the sed to do the same job for me ?

awk -v GBPT="$GBP" -v NEWT="$NEW" '
BEGIN {
RS="^C"
FS="^C"
ORS="\n"
OFS="\n"
}
{
if (index($0,"Sterling"))
print $0 >> GBPT
else
print $0 >> NEWT

}' ${IN_FILE}

TIA
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Dickie Bird
put this cmd in a file called what-you-want
----------------begin
/\[Ss]terling\>/{
w sterling-file
n
}
/..*/ w other-file
------------------end
then on cmd line type:
sed -nf what-you-want inputfile
according to the sed version you have, it could be write
shorter. this is a general version. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
should be:
/\<[Ss]terling\>/{ ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
Dank Jamisar - ich werde es morgen versuchen
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Jamisar
No luck, I'm afraid - It errors :
sed: 0602-404 Function split_gbp cannot be parsed.
Here's my problem in detail:
The RS and FS are set to ^C so that the index statement knows what record to work on. I have several lines making up a record, terminated by ^C.
The index statement will then write the entire ^C terminated record to either of the two files, depending on the existence (or not) of the test &quot;Sterling&quot; line in the record.

Here's two sample records:
This writes to GBP file
{1:F01DDDDGB22AXXX6150963837}{2:O8981756020325FFFFESDAAXXX23531279480203251556N}{4:
:20:0208125945002058
:51C:/3409
:23:1234568/24
:30:020605
:26A:pH01128855/8859
:33B:USD500.
:73:/NAGCOM/USD2.50
:36:1.4
::34B:GBP509.93
:16A:2
:32A:GBP509.93
:72:Sterling
-}{5:{MAC:4FD745A0}{CHK:219D0ED7D5DD}}^C

This is to write to NEW file(no Sterling line):
{1:F01DDDDGB22AXXX6150963837}{2:O8981756020325FFFFESDAAXXX23531279480203251556N}{4:
:20:0208125945002058
:51C:/3409
:23:1234568/24
:30:020605
:26A:pD01128855/8859
:33B:USD500.
:73:/NAGCOM/USD2.50
:36:1
::34B:USD500.00
:16A:2
:32A:USD500.00
:72:USDollar
-}{5:{MAC:4FD745A0}{CHK:219D0ED7D5DD}}^C

Any clearer ?
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
i see. you have 2 problems
a) ths control C is a really stupid choice
b) the key you are looking for is not at begin, nor at the end of the record.
but put this in mysed:
-----------------
/^{/,/Sterling/{
N
w fileSTERLING
n
}
/^{/,/USDollar/{
N
w fileDOLLARS
n
}
--------------
then:

sed -nf mysed inputfile
works on my solaris. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
this assume the pattern you are looking for
are exactly on: the last but one (vorletzte) line. explanation:

/^{/,/aaa/{ # start a grouped cmd on the
# lines between an initial {
# and the matching pattern
N # read the next inputline
w filen # write the whole buffer to file
n # start new cyclus
} # end command grouping

the first time sed writes to a file, will truncate or create it, the second append to it. ------------ jamisar
Einfachheit ist das Resultat der Reife. (Friedrich Schiller)
Simplicity is the fruit of maturity.
 
How about
Code:
BEGIN {fn=&quot;NEWT&quot;}
/Sterling/{fn=&quot;GBT&quot;}
{
  if ($0 ~ &quot;^C&quot;) {
    for (i=0;i<n;i++) print a[i] >> fn
    n = 0
    print $0 >> fn
    fn=&quot;NEWT&quot;
  }
  else {
    a[n++] = $0
  }
}
CaKiwi
 
Hi Cakiwi
Nearly - the GBPT file has only records for Sterling
but the NEWT file has 'em all still - and I don't have as many lines as on the original - aren't the assoc. arrays intolerant of duplicates ?
Also - Am I right to assume that the first lines (
BEGIN {fn=&quot;NEWT&quot;}
/Sterling/{fn=&quot;GBPT&quot;} ) is a kind of 'case' ie if '/Sterling/' is found, then fn=GBPT ?
And the rest :
if ($0 ~ &quot;^C&quot;) # Found our end of record marker ?
{
for (i=0;i<n;i++) #Output all in the array
print a >> fn
n = 0 #reset count
print $0 >> fn #print line with ^C in it
fn=&quot;NEWT&quot; # reset fn to default name
}
else
{
a[n++] = $0 # Otherwise load up array
}
}
Any more thoughts ???
;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Hi DB,

I made a file out of the 2 records you posted above and running this awk program created a GBP with the first record and a NEWT file with the other. Make sure you remove any old output files before you run since we are using >> to output to them so the data will be added to the end of any existing file. Alternatively, use > instead of >> to create new files. Here is a slightly simplified program using > and your filename variables:
Code:
BEGIN {n = 0;fn = NEWT}
/Sterling/{fn = GBPT}
{ a[n++] = $0 }
$0 ~ &quot;^C&quot; {
    for (i=0;i<n;i++) print a[i] > fn
    n  = 0
    fn = NEWT
}
CaKiwi
 
Hi again DB,

There shouldn't be a problem with the array because I'm indexing with an incrementing value of i for each line being saved. The rest of your comments are correct except I would call /Sterling/{...} more like an if statement than a case statement. Here if a more &quot;procedural&quot; version of the same program.
Code:
BEGIN {n = 0;fn = NEWT}
{
  if ($0 ~ /Sterling/) fn = GBPT
  a[n++] = $0
  if ($0 ~ &quot;^B&quot;) {
    for (i=0;i<n;i++) print a[i] > fn
    n  = 0
    fn = NEWT
  }
}
CaKiwi
 
Thanks for the help guys !

Jamisar - You're right, a better unique item in last line of record is &quot;-}{5:{&quot; - so I'm using that ! (Never got the sed to work though)

CaKiwi - Thanks for the Awk solution - now to test it with > 3000 byte records !

;-) Dickie Bird
db@dickiebird.freeserve.co.uk
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top