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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Picking a Substring in a .ksh script

Status
Not open for further replies.

tbohon

Programmer
Apr 20, 2000
293
0
0
US
I have a file of records that look like this:

03/02/2005|oru_in|BAD MEDREC ID|medrec='12345',enc='111'

My script needs to separate these out into two separate files and the only difference is in the length of the medrec value (12345 above) ... one group has values of length 10, the other has values of length 7.

I can parse the line out based on the field separator '|' but can't figure out how to check the length of the medrec value (again, 12345 in the example above).

This will be operating on the input file and creating the two output files all at the same time although I could create intermediate working files if necessary.

Ideas?

Tnx.

Tom

"My mind is like a steel whatchamacallit ...
 
Similiar to bash:
Code:
str="03/02/2005|oru_in|BAD MEDREC ID|medrec='12345',enc='111'"
medrec=${str##*rec=\'} medrec=${medrec%%\'*}

echo medrec: $medrec, length: ${#medrec}

Code:
awk -F'[ \t"\x27]*[=|,][ \t"\x27]*' '/medrec=/ {
  printf "Medrec: %s (length %d)\n", $5, length($5)
}'

. Mac for productivity
.. Linux for developement
... Windows for solitaire
 
I hope I got it right - maybe not the nicest code but works:

Code:
#!/bin/ksh
# where $1 is the input file, of course
sed 's/.*medrec=.\(.*\).,.*/\1/' $1| awk '{ if ((length($0)) == 7) print ($0)}' >> 7-file
sed 's/.*medrec=.\(.*\).,.*/\1/' $1| awk '{ if ((length($0)) == 10) print ($0)}' >> 10-file

Input file looked like this:
Code:
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'

laters
zaxxon
 
Try...
Code:
awk -F\' '{print > "outfile" length($2)}' infile
Tested...
Code:
==> outfile10 <==
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567890',enc='111'

==> outfile7 <==
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567',enc='111'
03/02/2005|oru_in|BAD MEDREC ID|medrec='1234567',enc='111'
 
Or...
Code:
awk 'match($0, "medrec=[^,]*") {print > "outfile" RLENGTH-9}' infile
 
Thanks, all ... greatly appreciate it!

Tom

"My mind is like a steel whatchamacallit ...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top