help extracting text from file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting help extracting text from file
# 1  
Old 07-21-2012
help extracting text from file

Hello I have a large file with lines beginning with 552, 553, 554, below is a small sample, I need to extract the data you can see below highlighted in bold from this file on the same location on every line and output it to a new file.

Thank you in advance for any help

Code:
55201KL 000787350120720  KL32   21547XA                            MU   LKW-2P  S000000V            0000000000                 
55301000006110012071800089123000000000001300000I                                                                               
554011207200000000001300 1207230000000000000 1207240000000001300 1207250000000000012 1207260000000000077 1208130000000001300   
554011208140000000000808 1208150000000000700 1208160000000001200 1208170000000001300 0000000000000000000 0000000000000000000
5520150 000000164120720  6L1W    6519AX                            GS           E000000V1738621     0000000000                 
55301000000361312071800540451000000000000300000I                                                                               
554011207200000000000613 1207230000000000500 1207240000000000002 1207250000000005600 1207260000000000078 1207270000000000008   
554011207300000000000770 1207310000000000100 1208010000000000060 1208020000000000500 1208030000000000700 0000000000000000000

I would like it to look like this if possible

Code:
KL32   21547XA  MU                                                                                       
120720 1300 120723 0000 120724 1300 120725 0012 120726 0077 120813 1300   
120814 0808 120815 0700 120816 1200 120817 1300 000000 0000 000000 0000
                                                                                                                         
6L1W    6519AX GS                                                                                             
120720 0613 120723 0500 120724 0002 120725 5600 120726 0078 120727 0008  
120730 0000 120731 0100 120801 0060 120802 0500 120803 0700 000000 0000


Last edited by firefox2k2; 07-21-2012 at 04:29 AM..
# 2  
Old 07-21-2012
Code:
awk '
/^552/{print $3, $4, $5}
/^554/{
printf("%s %s ",substr($1,6,6),substr($1,length($1)-3))
for(i=2;i<=NF;i++)
 printf("%s %s ",substr($i,1,6),substr($i,length($i)-3))
print ""
}' inputfile

# 3  
Old 07-23-2012
Quote:
Originally Posted by elixir_sinari
Code:
awk '
/^552/{print $3, $4, $5}
/^554/{
printf("%s %s ",substr($1,6,6),substr($1,length($1)-3))
for(i=2;i<=NF;i++)
 printf("%s %s ",substr($i,1,6),substr($i,length($i)-3))
print ""
}' inputfile


Hi thanks for the help, tried this but get a syntax error when running
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Extracting and copying text from one file to another

Helooo, So I have a .fasta file (a text file with sequence data) which looks like this, with just over 3 million lines of data. >TCONS_00000001 gene=XLOC_000001 AATTGTGGTGAAATGACTTCTGTTAACGGAGACATCGATGATTGTTGTTACTATTTGTTCTCAGGATTCA... (8 Replies)
Discussion started by: 4galaxy7
8 Replies

2. UNIX for Dummies Questions & Answers

Extracting lines from a text file based on another text file with line numbers

Hi, I am trying to extract lines from a text file given a text file containing line numbers to be extracted from the first file. How do I go about doing this? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

3. UNIX for Dummies Questions & Answers

Extracting the last column of a text file

I would like to extract the last column of a text file but different rows of the text file have different numbers of columns. How do I go about doing that? Thanks! (1 Reply)
Discussion started by: evelibertine
1 Replies

4. Shell Programming and Scripting

extracting part of a text file

Hi guys So I have a very large log file where each event is logged along with the time that it occurred. So for e.g. The contents of the file look like: ... 12:00:07 event 0 happened. 12:01:01 event 1 happened. 12:01:05 event 2 happened. 12:01:30 event 3 happened. 12:02:01 event 4... (10 Replies)
Discussion started by: alinaqvi90
10 Replies

5. Shell Programming and Scripting

Extracting specific characters from a text file

I'm extremely new to scripting and linux in general, so please bear with me. The class I'm taking gives virtually no instruction at all, and so I'm trying to learn everything off the web. Anyway, I'm trying to extract characters that follow after a specific pattern ( '<B><FONT FACE="Arial">' ) but... (3 Replies)
Discussion started by: livos23
3 Replies

6. UNIX for Dummies Questions & Answers

extracting text and reusing the text to rename file

Hi, I have some ps files where I want to ectract/copy a certain number from and use that number to rename the ps file. eg: 'file.ps' contains following text: 14 (09 01 932688 0)t the text can be variable, the only fixed element is the '14 ('. The problem is that the fixed element can appear... (7 Replies)
Discussion started by: JohnDS
7 Replies

7. Shell Programming and Scripting

Extracting specific text from a file

Dear All, I have to extract a a few lines from a log file and I know the starting String and end string(WHich is same ). Is there any simplere way using sed - awk. e.g. from the following file -------------------------------------- Some text Date: 21 Oct 2008 Text to be extracted... (8 Replies)
Discussion started by: rahulkav
8 Replies

8. Shell Programming and Scripting

Extracting a line in a text file

If my file looks like this…. 10 20 30 and I want to take each line individually and put it in a variable so it can be read later in it's on individual test statement, how can I do that? I guess what I'm asking is how can I extract each line individually. Thanks (5 Replies)
Discussion started by: terryporter51
5 Replies

9. Shell Programming and Scripting

extracting unique lines from text file

I have a file with 14million lines and I would like to extract all the unique lines from the file into another text file. For example: Contents of file1 happy sad smile happy funny sad I want to run a command against file one that only returns the unique lines (ie 1 line for happy... (3 Replies)
Discussion started by: soliberus
3 Replies

10. Shell Programming and Scripting

Extracting data from text file based on configuration set in config file

Hi , a:) i have configuration file with pattren <Range start no>,<Range end no>,<type of records to be extracted from the data file>,<name of the file to store output> eg: myfile.confg 9899000000,9899999999,DATA,b.dat 9899000000,9899999999,SMS,a.dat b:) Stucture of my data file is... (3 Replies)
Discussion started by: suparnbector
3 Replies
Login or Register to Ask a Question