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!

locate record in text file based on string pattern at offset 2

Status
Not open for further replies.

Calator

Programmer
Feb 12, 2001
262
AU
We have a text file with different records formats, indentified by a 'record type' code, found in character positions 19-20 (teh code is two characters).
In a ksh Unix script, I need to extract and process record type '13' from this file. There will be only one such record on file.
Can you indicate syntax for "sed" or other command that can be used to achieve that?

for eg we use following command to obtain the 'year' from first record on file:
year="`sed -n -e '1p' ${file_name}.head | cut -c34-37`"

I would need something similar, but replacing "first record" by "record with '13' in char positions 19-20
 
sed -n '/^.\{18\}13/p'

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Thanks PHV, can you let me know how does '18' in your solution related to my problem?
 
You wanted '13' in character positions 19-20

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
In other words, ^.\{18\} will match 18 of the previous expression, in this case a "." which matches any character, starting from the beginning of the line.

Annihilannic.
 
Guys, thanks for your help, it sorted out my problem or locating the requeired record.
I have a further question, on extracting the right information (a substring) from the record:

Following code returns the correct information:
bal_ref="`sed -n '/^.\{18\}13/p' ${source_file} | cut -c63-80`"

However as I need to extract several items, I though it would be more elegant to split the code into:
- extracting the whole record from the file
- extracting the rrequired substrings from the file

So I wrote as the follwing:
bal_record="`sed -n '/^.\{18\}13/p' ${source_file}`"
bal_ref=`echo $bal_record |cut -c63-80`

The problem is that the code above does not appear to extract from the correct record offset(positions 63-80), instead it extracts positions 68-86

Any idea why and how to fix?
 
If there is white space in the record the expansion of the $bal_record variable and subsequent passing of parameters to echo may be squashing it. Try echo "$bal_record" instead, which will force it to be treated as one parameter and preserve white space.

Annihilannic.
 
bal_ref=`echo "$bal_record" |cut -c63-80`
did not work, it returned empty string
Any ideas? Thanks!
 
Can you include some sample input data and what you expect as output?

Also, are you sure the input doesn't contain tabs?

Annihilannic.
 
1085-111234555558 130002636968ABABABAB ABABA ABABBABABAB ABABABAB ABABABA 085-111512345958ABABA AAA AA AAA00000000

SAMPLE ABOVE - HAD TO OVERTYPE SOME OF THE STUFF. nO TABS I THINK. THANKS A LOT
 
The result I get in bal_ref is "ABABABAB ABABABA ".

What operating system and version are you using here (the output of uname -i would be useful)?

Annihilannic.
 
$ uname -i
SUNW,SPARC-Enterprise
$ version
Machine hardware: sun4u
OS version: 5.10
Processor type: sparc
Hardware: SUNW,SPARC-Enterprise
Thank you for your effort!
 
Sorry if I misled you, I had some code commented out earlier in the script, That was likely causing the problem. I'll check it out tomorrow and let you know result
 
yes Annihilannic, the double quotes solved the problem. Thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top