0

I have a text file with a lot of mda5 hashes. I need to get only the hash of my file as output. I've tried cat mda5hashes.txt | grep manual12.pdf, but I have this promt:

manual12          917NJvfNj6uY237fjzmso38djr7s

How can I get only the hash as prompt, and the number of line in which is located?

Joshua Salazar
  • 205
  • 4
  • 14

3 Answers3

3

Awk can do this very simply Assuming that manual12 occures at the begining of the line

awk '/^manual12/{print NR,$2}' mda5hashes.txt

will give the output as

1      917NJvfNj6uY237fjzmso38djr7s

What it does?

awk uses a format pattern{action}

That is when the line matches the pattern, the action is performed.

Here the action is to print the second $2 column delimited by space(by default)

When pattern matches manual12 in the line the hash part is printed

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
0

grep -n will print line numbers with the match. You can translate the first colon produced by grep and all other whitespace to a single space with sed 's/:/ /;s/\s\s*/ /g'. You then want the first and third section, which can be achieved with cut -d ' ' -f 2 --complement

Chaining these all together, you would get something like:

cat mda5hashes.txt | grep -n manual12 | sed 's/:/ /;s/\s\s*/ /g' | cut -d ' ' -f 2 --complement

I'm sure somebody could replace the sed + cut combo with a perl or awk one-liner, though.

iobender
  • 3,346
  • 1
  • 17
  • 21
  • **awk '/manual12/{ print $2 }' mda5hashes.txt** explanation: search for line containing 'manual12' then print second field of that line(record). – Arif Burhan Mar 24 '16 at 03:26
  • **awk '/manual12/{ print NR,$2 }' mda5hashes.txt** explanation: search for line containing 'manual12' then print the line number and second field of that line(record). – Arif Burhan Mar 24 '16 at 03:32
0

This looks OK

cat -n | grep manual | awk '{$2=""}1'

or

nl | grep manual | awk '{$2=""}1'
Vytenis Bivainis
  • 2,308
  • 21
  • 28