Capture first N Bytes from first line in a file

Hi Guyz,

I need to capture first N Bytes from the first line of my file.

Eg. If i have following data in File1

414d51204541495052475731202020204a910846230e420c Hello 3621363663212 Help Required

Then, i want the value of first 48 Bytes to be stored in a variable.
That is, variable MyValue should contain :-

414d51204541495052475731202020204a910846230e420c 

Help Anyone.
:slight_smile:

something like this :

Pos=48

Myval=`awk -v va=$Pos '{ print substr($0,1,va) }'  file_name.txt`

echo "$Myval"

couple of ways:

#  cut -c1-48 infile
414d51204541495052475731202020204a910846230e420c

#  awk 'NR==1{print substr($0,1,48)}' infile
414d51204541495052475731202020204a910846230e420c

#  sed 's/^\(.\{48\}\).*/\1/;q' infile
414d51204541495052475731202020204a910846230e420c

#  dd if=infile ibs=48 count=1 of=outfile
1+0 records in
0+1 records out

:slight_smile: HTH

Tytalus,

can you please let me know how the below one is working ?

#  dd if=infile ibs=48 count=1 of=outfile

if : input file
of : output file
ibs : tell how many bytes to read at a time, something like block size
count: read those blocks for maximum of this times,

so

# dd if=infile ibs=N count=M of=outfile
Will work if N * M = 48

Thank You ,

Forgot to check the man page of "dd" :slight_smile: