selective positions from a datafile

Hi dear friends,

Im writing a shell script which has to select the strings based on the position.
but the problem is there is no field seperator.
Normally a datafile contains 2000 records (lines) and each line is of size 500 charecters.
I want to select the fields from all the lines which matches the charecter positions.

ex: from position(011:025) position(061:095) position(201:250) position(311:318) position(411:425) position(471:500).

any idea will be much appreciated.

Thanks in advance.
Ganapati

Yes its possible to do but it would be better if you could post some sample input data and output you require.

use simple cut command

cut -c1-4,6-8 filename

it will print field 1 to 4 and 6to 8 from file.

aah I see. I thought OP was meant to select data based on characters from each line, without knowing the columns or having a field separator, therefor I asked for the sample input, it proved to be very dumb though :slight_smile:

:slight_smile: Thanks Dhruva and shereenmotor,

Sample datafile is:
ANP200606299e903984cls8898989ewe89CK20080820..................,

Required file would be based on charecter position as mentioned earlier.
Other than this charecter position there is no other logic applicable.

Many Thanks and Regards,
Ganapati

You can decompoose the input file in records with the fold program.
The following command create split your file in fixed length (500) lines :

fold -w 500 filename

You can pipe the result of the command to another command for field extracting :

fold -w 500 filename | \
cut -c 11-25,61-95,201-250,311-318,411-425,471-500

There is no separator betwen the extracted fields.
If you want someone, ';' for example, you can do :

fold -w 500 filename | \
awk -v OFS=';' '
   { print substr($0,  11, 15),
           substr($0,  61, 35),
           substr($0, 201, 50),
           substr($0, 311,  8),
           substr($0, 411, 15),
           substr($0, 471, 30) } '

For a space separator, remove the -v OFS= option.

Another way to split the input file is to use the dd program :

dd obs=500 cbs=500 conv=unblock if=filename 2>/dev/null

Jean-Pierre.

Could any one explain me the code to do the above task?

dd obs=500 cbs=500 conv=unblock if=filename 2>/dev/null

I dont know anything about dd obs. !!!!!

Thanks in advance
Ganapati

the dd program read the input file (if=filename), convert data blocks of 500 characters (cbs=500) from fixed to variable (conv=unblock); the specification of the output block size, may be omited (obs=500).

the stderr is redirected to null because dd display stats.

Jean-Pierre.

Thanks,
But I've a doubt, will this works in solaris?
Lokesha

Yeah it should be there also, try:

type dd

it'll tell you the path of dd as well as check man dd.

Thanks for your clarification Shereenmotor :slight_smile: