To display the selected part in text file of unix

0400903071220312  20120322 20:21
1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363          12122011AUS          182644             000C2
8122011        0000                     000
1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363          12122011AUS          182644             000C2
8122011        0000                     0000803201205259005         -182644

I have the text file as above. I want to display '3071' from first line. Please assist with code. Help me out.:wall:

awk '{print substr($0, 7, 4); exit}' file

Thank you scott, the code works fine :slight_smile:

---------- Post updated at 05:21 AM ---------- Previous update was at 05:04 AM ----------

Hi scott, In mentioned code what is $0 refers to ?
awk '{print substr($0, 7, 4); exit}' file

$0 means the entire line
awk reads the file line by line

sed 's/.\{6\}\(.\{4\}\).*/\1/g;q' infile

awk reads the file line by line.ok. Does the 0 in $0 is refer to first line of the file ?

awk splits a text file into "records". by default this is a line (but can be changed).
each line (or record) is split into fields. by default this is done at spaces.

$0 then refers to the entire line at the moment. $1 is the first word, $2 the second, and so on, up to $NF (NF meaning number of fields)

The exit in the action block makes it quit before it ever gets to any other lines.

1 Like