Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Data file is fixed length file, and I want to read the file record by record and do transformations to the subsequent records based on the prior records (and write the results into another file).

Example:

CTD1234abcdTRN0001
AA 5678defg
BB 8910erty
CTD5678qwerTRN5678
AA 9876bvcn
BB 8765zxcv

Now I want TRN0001 from the CTD record to be written to the subsequent AA and BB records, similarly for the following CTD records. My output should look like:

CTD1234abcdTRN0001
AA 5678defgTRN0001
BB 8910ertyTRN0001
CTD5678qwerTRN5678
AA 9876bvcnTRN5678
BB 8765zxcvTRN5678

Example 2:

AAABBBB11115678xxxx
BBBCCCC22221234YYYY
MTD0001abcdTRN12345 abcdedfg
AA 0002bcde
BB 0003defg
CCCDDDD33331234zzzz
MTD0003qwerTRN56789 defghigk
AA 9876bvcn
BB 8765zxcv
XXXBBBB11115678aaaa
YYYCCCC22221234bbbb 

should become

AAABBBB11115678xxxx
BBBCCCC22221234YYYY
MTD0001abcdTRN12345 abcdedfg
AA 0002bcdeTRN12345
BB 0003defgTRN12345
CCCDDDD33331234zzzz
MTD0003qwerTRN56789 defghigk
AA 9876bvcnTRN56789
BB 8765zxcvTRN56789
XXXBBBB11115678aaaa
YYYCCCC22221234bbbb 

Can you please help me on this, how can you achieve this using UNIX shell scripting?

share|improve this question
1  
Can you be more precise about the structure of the file? Are there exactly two records of each type per line. Is the substring in question fixed in length, e.g the last 7 characters of the CTD field? –  Adrian Frühwirth Apr 7 '13 at 12:20

2 Answers 2

up vote 2 down vote accepted
$ cat testdata
CTD1234abcdTRN0001
AA 5678defg
BB 8910erty
CTD5678qwerTRN5678
AA 9876bvcn
BB 8765zxcv
CTDxxxxxxxxTRNyyyy
AA foobarfo
BB foobarfo
AAABBBB11115678xxxx
BBBCCCC22221234YYYY
MTD0001abcdTRN12345 abcdedfg
AA 0002bcde
BB 0003defg
CCCDDDD33331234zzzz
MTD0003qwerTRN56789 defghigk
AA 9876bvcn
BB 8765zxcv
XXXBBBB11115678aaaa
YYYCCCC22221234bbbb

$ awk '/^(C|M)TD/ { s=match($0, /[[:space:]]/); postfix=substr($0, 12, length($0)-s); print; next } /^(AA|BB)[[:space:]]/ { print $0 postfix; next } 1' testdata
CTD1234abcdTRN0001
AA 5678defgTRN0001
BB 8910ertyTRN0001
CTD5678qwerTRN5678
AA 9876bvcnTRN5678
BB 8765zxcvTRN5678
CTDxxxxxxxxTRNyyyy
AA foobarfoTRNyyyy
BB foobarfoTRNyyyy
AAABBBB11115678xxxx
BBBCCCC22221234YYYY
MTD0001abcdTRN12345 abcdedfg
AA 0002bcdeTRN12345
BB 0003defgTRN12345
CCCDDDD33331234zzzz
MTD0003qwerTRN56789 defghigk
AA 9876bvcnTRN56789
BB 8765zxcvTRN56789
XXXBBBB11115678aaaa
YYYCCCC22221234bbbb
share|improve this answer
    
there may be multiple CTD line in a file –  user36697 Apr 7 '13 at 13:05
    
And you want the first CTD line's TRNxxxx to carry into all subsequent AA and BB records (and ignore any following CTD lines)? –  Adrian Frühwirth Apr 7 '13 at 13:09
    
The above output I need and there may be multiple CTD line in file... –  user36697 Apr 7 '13 at 13:09
    
You fail to explain how the output should look with more than two CTD lines in a file. I updated my answer with what I assume you are asking for. –  Adrian Frühwirth Apr 7 '13 at 13:14
    
Oh I'm sorry my output should look like: CTD1234abcdTRN0001 AA 5678defgTRN0001 BB 8910ertyTRN0001 CTD5678qwerTRN5678 AA 9876bvcnTRN5678 BB 8765zxcvTRN5678 CTDxxxxxxxxTRNyyyy AA foobarfoTRNyyyy BB foobarfoTRNyyyy –  user36697 Apr 7 '13 at 13:20

That the Unix shell is a (quite limited) programming language is a bonus for its job of organizing other program's work. It is not to be used for everything.

Such text-processing jobs are better done with scripting languages specifically created for this purpose, like Perl or Python.

share|improve this answer
    
so, can you pls. let me know in Perl? –  user36697 Apr 7 '13 at 12:50
3  
@user36697 you should try to solve it yourself and ask for the specific problem you encountered. –  Ulrich Dangel Apr 7 '13 at 13:55

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.