awk or grep ??

Hi All,

I need to creat one cutdown file from one full file. The full file may looks as below:

UHL1 08007 DDLIST 000
O301107121212A & L TRANSFER IN ALCB ...................
A301107121212720000IBACS LIAISON ...................
O060807138202B O C THE DIRECT DEBIT DEPT...........
A060605138202404356IBOC MANCHESTER ACC......................
O301106202001ZEUS ADMINISTRATION SERVICES SU BRODIE ....
A301106202001400250IFIRST ADVICE ...................
O060807214209COMMERCIAL UNION LIFE ASSURANCE FRANCIS JACKSON ..................
A060605214209400250ICGU CU CUDDNCS ..............
A060605214209400250ICGU CU CUI-IS DD ..........
O120607232323A & L TRANSFER IN Karina Armstrong ....
A120607232323720000IBACS LIAISON ...................
O130707241010AULD PROPERTIES LTD RAYMOND AULD .......
UTL1064600

I need to extract only lines which starts from "O" to my cutdown file from full file.
Also from this selected lines (which has "O") I need only few position fields.
Position for these fields from full file are (from 8-13),(from 14-30) and (from 36-40).
And each cutdown file record should start with "DTL1".

Finally my cutdown file should look like below:
DTL121212A & L TRANSFER IN ALCB........
DTL1138202B O C THE DIRECT DEBIT DEPT......
.
. etc.,

I know how to select the lines with "O" using grep. But I dont know how to insert DTL1 to each line and also how to select position for this.

I know this can be implemented in awk easily, but Iam not much exposure to awk.

Pls help me achieve this...

Hi,

Am also very new to awk.
But the below command can work out a part of ur query
awk '/^O/ { print "DTL1" $1 }' <filename>
The above command gives the o/p as below:
DTL1O301107121212A
DTL1O060807138202B
DTL1O301106202001ZEUS
DTL1O060807214209COMMERCIAL
DTL1O120607232323A
DTL1O130707241010AULD

As ur file doesnot have any coloumn separator Am not aware of how to print the positional paramaeters.

if there is a space inbetween two strings in a file , then awk consider it as two different arguments.Hence "O301107121212A" is the first argument and "&" "L" "TRANSFER" "IN" "ALCB" are the 2nd 3rd etc arguments.

Regards,
Jisha

There is something wrong with your specification for the fields you want outputted.
Your sample output is variable length but you specify fixed field lengths.

Hope this is what you are looking for...

grep "^O" file | cut -c 8-13,14-30,36-40 | while read line ; do echo DTL1$line ;done

o/p as per your data:
DTL1121212A & L TRANSFER IN ....
DTL1138202B O C THE DIRECT DEPT
DTL1202001ZEUS ADMINISTRATIRVICE
DTL1214209COMMERCIAL UNION ASSUR
DTL1232323A & L TRANSFER INna Ar
DTL1241010AULD PROPERTIES LYMOND

-ilan

Thats perfect ilan.
Thanks
Jisha

And here is the same solution using sed

sed -n -e 's/\(^O.\{6\}\)\(.\{23\}\)\(.\{5\}\)\(.\{5\}\)\(.*$\)/DTLI\2\4/p' file
sed -n -e "s/^O.\{6\}\(.\{23\}\).\{5\}\(.\{5\}\).*/DTL1\1\2/p" input.txt

Edit: fpmurphy beat me to it :frowning:

Thanks a lot to all for sharing your great time on this...

With Regards / Mysore Ganapati;)

Or:

awk '/^O/{print "DTL1"substr($0,9)}' file

Regards

Further to the above I need to append two blank spaces to each of the line. Iam wondering, how this can be done in the same line "grep "^O" file | cut -c 8-13,14-30,36-40 | while read line ; do echo DTL1$line ;done"

any more light on this...

grep "^O" file | cut -c 8-13,14-30,36-40 | while read line ; do echo "DTL1$line  " ;done

is this what you need?

grep "^O" file | cut -c 8-13,14-30,36-40 | while read line ; do echo -e "DTL1$line\n\n" ;done

There is one problem here

Output record line should of fixed size (32 charecters including 2 spaces at the end of each output line), eventhough selected position has null values.

grep "^O" samp_file | cut -c 8-13,14-30,36-40 | while read line ; do echo -e "DTL1$line " ;done >> result.txt

Eventhough above command giving expected results, for the below case it is failing:

If the samp_file is as below:

A s583938DD Road Bangalor 86534
O dfss839999UR Road Bangalor 73344
A s483erwSD Road Mysorer 86534
O dfss259867UP Road Kolar 344
O dgfs446832JK SUR Hubli

The result file result.txt is producing as below:

DTL1839999UR Road Bangalor73344
DTL1259867UP Road Kolar 344
DTL1446832JK SUR Hubli

It is ignoring the space and output is not exact length (32 charecters). Hence output should be as below (fixed length, eventhough it is blank, space should be there):

DTL1839999UR Road Bangalor73344
DTL1259867UP Road Kolar 344
DTL1446832JK SUR Hubli

Any help on this would be much appreciated.
Ganapati

I think my question is hard to understand, I'll try to simplify now:

The output from the below command Iam redirecting into outputfile called "result.txt".

grep "^O" samp_file.txt | cut -c 8-13,14-30,36-40 | while read line ; do echo -e "DTL1$line " ;done >> result.txt

I need each line (record) in the result.txt should be of same length.
Now the problem is the produced output lines are not of same length -if "Cut" command finds blank spaces in "samp_file.txt".

Please use below "samp_file.txt", and requirement is each line in "result.txt" should be of same length.:confused:

A s583938DD Road Bangalor 86534
O dfss839999UR Road Bangalor 73344
A s483erwSD Road Mysorer 86534
O dfss259867UP Road Kolar 344
O dgfs446832JK SUR Hubli

With Regards / Mysore Ganapati