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

Extracting 7-8 bytes string from file with variable lenght records

Status
Not open for further replies.

amkipnis

Programmer
Apr 15, 2003
21
US
Hello PHV,
I have been using your solution for a while and it worked OK until I got to different problem. Here is a list:
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Next Directory/Pending/P0I5002_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Next Directory/PendAppr/P0I5003_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Next Directory/Review/P0I5004_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Direct Lending/Pending/P0I5006_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Direct Lending/PendAppr/P0I5007_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Direct Lending/Review/P0I5008_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Canada/PendAppr/P0I5011_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Pricing Matrices/Canada/Review/P0I5012_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/HHoldDirect/Pending/P0J01A10_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/HHoldDirect/PendAppr/P0J01A11_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/HHoldDirect/Review/P0J01A12_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/US/PendAppr/P0J01A3_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/US/Review/P0J01A4_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/Canada/Approved/P0J01A5_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/Canada/Pending/P0J01A6_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/Canada/PendAppr/P0J01A7_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/SpecialRules/Canada/Review/P0J01A8_03-25-2004.roi
/Folder1/Next Directory/Operational/Pricing/Control/Rescission/PendFundSwChg/Daily/P0K7201PFSC_03-25-2004.roi

I have to extract 8 bytes long vs. 7 bytes long pattern in some cases.

After executing slightly modified your original command:
sed -n 's!.*/\(P......*\)_.*!\1.txt!p'

I receive the following result:

P0I5002.txt
P0I5003.txt
P0I5004.txt
P0I5006.txt
P0I5007.txt
P0I5008.txt
P0I5011.txt
P0I5012.txt
P0J01A10.txt
P0J01A11.txt
P0J01A12.txt
P0J01A3.txt
P0J01A4.txt
P0J01A5.txt
P0J01A6.txt
P0J01A7.txt
P0J01A8.txt
P0K7201PFSC.txt

As you can see I can got extra 4 alpha characters in the last record.
How can I modify above command to extract P0K7201.txt instead?

I appreciate for your help!
Thanks again.
Alex.
 
Have you tried this ?
sed -n 's!.*/\(P......[0-9]*\).*!\1.txt!p'

Hope This Help, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884
 
Thank you for your help, PHV. I have tried it and it worked great!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top