Take the 2-minute tour ×
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.

Looking through the man pages for sed, awk and grep, I'm unable to find a way to search for a string that is exactly n characters long.

Given the following text file, I want to extract just 6982a9948422

ID                  IMAGE               COMMAND                
CREATED             STATUS              PORTS
6982a9948422        ubuntu:12.04        apt-get install ping   
1 minute ago        Exit 0

The value will always be in the first column and is always 12 alphanumeric characters.

Here is what I am trying so far.

 cat /tmp/test | awk {'print $1'} | sed 's/.*\([0-9],[A-Z],[a-z]\{12\}\).*//g'

What would be a way to extract just 6982a9948422 from the text above ?

share|improve this question

7 Answers 7

up vote 4 down vote accepted
awk 'length($1) == 12 { print $1 }' file

The program is pretty self documenting and avoids the regex hammer.

awk -v f=1 '$f ~ /^[[:alnum:]]{12}$/ { print $f }' file

Or swing away with the above.

share|improve this answer
    
The problem with this is that it will pick up non-alphanumerics, too. –  Joseph R. Jan 26 at 17:09

This will search for, and print, all alphanumeric strings that start at the beginning of a line and are exactly 12 characters long.

grep -o -w -E '^[[:alnum:]]{12}'
share|improve this answer
    
This works on (GNU grep) 2.10 [ubuntu] but not on (BSD grep) 2.5.1-FreeBSD [MacOsX] –  spuder Jan 26 at 5:30
1  
I tried to make it POSIX compliant. It works on FreeBSD 9.1. I don't have access to a Mac OS X system, sorry. –  Mark Plotnick Jan 26 at 6:12
    
Nothing wrong with the answer, I just wanted to clarify for any future readers who might be confused why it doesn't work for them. –  spuder Jan 26 at 6:28

You can use grep with the PCRE facility. It's available in most of the newer versions of grep.

$ grep -oP "^[[:alnum:]]{12}" test.txt
6982a9948422

This will give you all the matches that are 12 long and include characters that are valid in words, [a-zA-Z0-9].

share|improve this answer
    
This works on (GNU grep) 2.10 [ubuntu] but not on (BSD grep) 2.5.1-FreeBSD [MacOsX] –  spuder Jan 26 at 5:31
    
@spuder - please update your Q then, you need to be explicit if you have locked versions of OS/tools, otherwise with the generic tags you've selected the A'ers are left to guess. –  slm Jan 26 at 6:18
1  
Nothing wrong with the answer, I just wanted to clarify for any future readers who might be confused why it doesn't work for them. –  spuder Jan 26 at 6:27
    
@spuder - OK, next time just make note that this is just a heads up or something, otherwise that comment might be perceived as this A doesn't solve the OP's problem. Mainly b/c it's you the OP making mention of this. 8-). Is your Q resolved then or did you want additional feedback? –  slm Jan 26 at 13:17

Another solution, using end-of-word delimiter, not apparent in any of the solutions above:

egrep '^[[:alnum:]]{12}\>' filename
share|improve this answer
    
pubs.opengroup.org/onlinepubs/007904875/basedefs/… Because it isn't standardized, thus may not work everywhere. which seems to be a untold part of the question from reading his comments. –  llua Jan 26 at 15:01

I would modify your own solution. Instead of deleting any $1 that doesn't match the required regexp, use grep to filter them out:

awk '{print $1}' /tmp/test | grep -iE '^[a-z0-9]{12}$'

Another alternative in pure sed:

sed 's/^\([a-zA-Z09]\{12\}\)[ \t]\+.*/\1/' /tmp/test
share|improve this answer

AWK Solution

awk '{match( $1, /[0-9A-Za-z]{12}/, arr) }; { printf arr[0] }' inputFile

OR

awk '$1 ~ /[0-9A-Za-z]{12}/{print $1}' inputFile
share|improve this answer

Others have given you answers for your specific test case where the string in question is at the beginning of the line. These solutions will print all strings that consist of 12 consecutive word characters:

perl -lne 'print for /\b(\w{12})\b/g;' file
grep -oP '\b(\w{12})\b' file
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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