1

im a list numbers in file and i would search 1 specific number in string

Es. 1 54 67 78 32
    11 4 67 89 90
    13 67 87 32 21

My search is 1

With awk or grep commands out this numbers

1
11
13
21
grep -w 1
awk '/12/'

but not single 1

This is correct operations? And for search for specific 2 numbers in 1 line?

  • 2
    I have a hard time understand what you want... grep -w 1 outputs just the first 1 for your example string. – RoVo Mar 1 at 12:06
  • 1
    Do consider updating your question, adding exactly what commands you've tried and what the output was, along with the expected results. – Kusalananda Mar 1 at 12:16
1

So you want any number containing the specified number? Add number patterns to the 1, e.g.:

grep -ow '[0-9]*1[0-9]*' infile

Output:

1
11
13
21

Edit

As noted by Stéphane, this only works for positive integers. If you want a more general solution, something like what is suggested here would be better. Below is a perlre grep version with the recommended regular expression:

grep -oP '[+-]?(0|[1-9]\d*)(\.\d*)?([eE][+-]?\d+)?' infile

Finding all numbers with a 1 in them is now a matter of passing the output to grep 1.

  • 1
    Might be worth clarifying that for grep -w a word is a sequence of alphanumerical characters or underscores. On an input like -21 12.13e-1, it would return 21, 12, 13 and 1. – Stéphane Chazelas Mar 1 at 12:52
0

I have done by below 2 method

h=`awk '{print NF}' filename| sort | uniq| sort -nr | sed -n '1p'`
praveen@praveen:~$ for ((i=1;i<=$h;i++))
 do
 awk -v i="$i" '$i ~ /1/{print $i}' filename
done

out

1
11
13
21

Second method

sed -r "s/\s+/\n/g" filename | awk '/1/{print $0}'

1
11
13
21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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