Help with extracting words from fixed length files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with extracting words from fixed length files
# 1  
Old 10-05-2010
Help with extracting words from fixed length files

I am very new to scripting and need to write a script that will extract the account number from a line that begins with HDR. For example, the file is as follows
Code:
HDR2010072600300405505100726     00300405505           
LBJ FREEWAY                                      DALLAS    
TELEGRAPH                                             751 HI
AR727612419      201007262010082520100823ABC
TELE EXGT401000300405505
DHL

I need to extract 00300405505

Any ideas?

Last edited by Franklin52; 10-06-2010 at 03:33 AM.. Reason: Please use code tags!
# 2  
Old 10-05-2010
Code:
awk '/^HDR/{print substr($NF,9)}' file

# 3  
Old 10-05-2010
Code:
$ ruby -ane 'puts $F.last if $_[/^HDR/]' file

# 4  
Old 10-05-2010
one more question...say the file actually looks like this...
Code:
HDR2010072600300405505100726 00300405505 customer name etc
LBJ FREEWAY DALLAS 
TELEGRAPH 751 HI
AR727612419 201007262010082520100823ABC
TELE EXGT401000300405505
DHL
HDR2010072600300405505100726 00300405505 customer name etc
LBJ FREEWAY DALLAS 
TELEGRAPH 751 HI
AR727612419 201007262010082520100823ABC
TELE EXGT401000300405505
DHL

There are multiple HDR lines and the customer name comes after the account that i need to extract?

Thanks

Last edited by Franklin52; 10-06-2010 at 03:33 AM.. Reason: Please use code tags!
# 5  
Old 10-05-2010
Quote:
Originally Posted by bds052189
There are multiple HDR lines and the customer name comes after the account that i need to extract?
Please use [code] tags when you post code or data sample !
Code:
awk '/^HDR/{print substr($(NF-1),9)}' file

# 6  
Old 10-05-2010
Code:
$ ruby -ane 'puts $F[1] if $_[/^HDR/]' file

# 7  
Old 10-05-2010
@Danmero:
i think substr character number start has to be 3 .

input contains:
Code:
HDR2010072600300405505100726 00300405505

last field NF is: 00300405505
start from 3
code & output is:
Code:
awk '/^HDR/{print substr($NF,3)}' file
300405505

If it is 9 , output will be
Code:
505


Last edited by dragon.1431; 10-05-2010 at 01:08 PM.. Reason: output added
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting fixed length number from a text file

Hi, I have a text file with sample records as CASE ID: 20170218881083 Original presentment record for ARN not found for Re-presentment I want to extract the 23 digit number from this file. I thought of using grep but initially couldn't extract the required number. However, after... (16 Replies)
Discussion started by: dsid
16 Replies

2. UNIX for Dummies Questions & Answers

Fixed length file extracting values in columns

How do I extract values in a few columns in a row of a fixed length file? If there are 8 columns and I need to extract values of 2nd,4th and 6 th columns, how do i do that? I used cut command, this I used only for one column. How do I do it more than one column? The below command will give... (1 Reply)
Discussion started by: princetd001
1 Replies

3. Shell Programming and Scripting

changing a variable length text to a fixed length

Hi, Can anyone help with a effective solution ? I need to change a variable length text field (between 1 - 18 characters) to a fixed length text of 18 characters with the unused portion, at the end, filled with spaces. The text field is actually field 10 of a .csv file however I could cut... (7 Replies)
Discussion started by: dc18
7 Replies

4. Shell Programming and Scripting

Make variable length record a fixed length

Very, very new to unix scripting and have a unique situation. I have a file of records that contain 3 records types: (H)eader Records (D)etail Records (T)railer Records The Detail records are 82 bytes in length which is perfect. The Header and Trailer records sometimes are 82 bytes in... (3 Replies)
Discussion started by: jclanc8
3 Replies

5. UNIX for Dummies Questions & Answers

What the command to find out the record length of a fixed length file?

I want to find out the record length of a fixed length file? I forgot the command. Any body know? (9 Replies)
Discussion started by: tranq01
9 Replies

6. Shell Programming and Scripting

Awk - Working with fixed length files

OK I am somewhat new to UNIX programming please see what you can do to help. I have a flat file that is a fixed length file containing different records based on the 1st character of each line. The 1st number at the beginning of the line is the record number, in this case it's record #1. I... (3 Replies)
Discussion started by: ambroze
3 Replies

7. Shell Programming and Scripting

Join two fixed length Files in Unix

Hi, Can we join two fixed length files in Unix using JOIN command? Is there any other command to accomplish the same? Thanks, G.Harikrishnan (6 Replies)
Discussion started by: gharikrishnan
6 Replies

8. Shell Programming and Scripting

Awk with fixed length files

Hi Unix Champs, I want to awk on a fixed length file. Instead if the file was a delimited file, then I could have used -F and then could have easily done manipulation on the fields. How do i do the same in case of fixed length file? Thanks in Advance. Regards. (7 Replies)
Discussion started by: c2b2
7 Replies

9. Shell Programming and Scripting

sort on fixed length files

Hi How to sort a fixed length file on a given char range and just display the duplicates. I did search for man sort to find any option but could find any.,something similar to cut -c 1-5,25-35. I have alternate way of doing this by using combination of cut,awk. but this creates extra temp... (6 Replies)
Discussion started by: sach_in
6 Replies

10. Shell Programming and Scripting

creating a fixed length output from a variable length input

Is there a command that sets a variable length? I have a input of a variable length field but my output for that field needs to be set to 32 char. Is there such a command? I am on a sun box running ksh Thanks (2 Replies)
Discussion started by: r1500
2 Replies
Login or Register to Ask a Question