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.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I have the following in a text file:

0400903071220312  20120322 20:21
1TRANTELSTRAFLEXCAB22032012CMP201323930000812201108875802100A003485363          12122011AUS          182644             000C28122011        0000                     000

How can I pull just the 3071 out of the first line?

share|improve this question

closed as not a real question by Gilles, Kevin, Renan, Ulrich Dangel, Stéphane Gimenez Aug 16 '12 at 14:33

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

It depends on why 3071 is special. Is it because of its position, or because it is exactly the value 3071, or because of what comes before or after it, etc? – Shawn J. Goff Apr 18 '12 at 18:28

You don't say how you want to determine what's displayed, but -o shows only the matched expression, so grep -o 3071 file would just display the 3071.

share|improve this answer
I vote for echo 3071 at that point – Michael Mrozek Apr 25 '12 at 2:16

To get the data from this position in a text file, you could use a tiny awk-script

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

or work with head and cut

head -1 yourfilename | cut -c7-10
share|improve this answer

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