Match only a-fA-F0-9 in a file

Hi,

I have a big file of hashes, but some lines are complete garbage and do not match with hexadecimal caracters.

I would like to use sed to select only the lines thar are composed by a-fA-F0-9 and print them into an outside file.

I tried

sed '/.[a-f0-9A-F]\{32\}/p' myFile > myNewFile

but it didn't work. Do you have any idea ?

Thank you :slight_smile:

Since you did not post any example of input or output, but rather just the not working regular expression, this is only a guess:
Drop the . (dot) in front of it. If that does not fix it, please, tell us in what way is not working, and show us some input and output.

Hello ganon551,

Please use code tags for commands/codes/Input you have shown in your posts as per forum rules. Also please do show us the sample input with expected output which will help us to understand your requirement in better manner, based on some assumptions following may help you in same, not sure is there any specific reason for using sed by you, why don't you give a try to simple grep too.
Let's say we have following Input_file:

cat Input_file
TestSingha-fA-F0-9test2
a-fA-F0-91233445667
test12334567
R. Singh is a bad boy.
Iron man is my hero.
Love you UNIX.com
a-fA-F0-9
  
 

Following will be the command then for same.

grep "a-fA-F0-9" Input_file > Output_file

You will see the output in Output_file as follows.

cat Output_file
TestSingha-fA-F0-9test2
a-fA-F0-91233445667
a-fA-F0-9

Thanks,
R. Singh

Whoops you're right sorry.

I tried without the dot and then added it.

So I have things like this as input :

$7.PxCsOF/tXBo7HR/0Tpxixk2i3eFs/
$7.qGSerhC8121ktlsaYhjFw2o53mkH/
45fd7bbdb5be9b735e8e85792e215177
45fd84989fb0897547529a123942a289
45fd91a6031d64f59c0bae8efb3ce4ca
45fd9ee96f4f19f31c9418bbfc65afac
45fdd39340740ca502d0f669bd5d604a
nlGwAUMrLpD3u5qUPh5Lz0xwenuxO5YU
o1j5n72bXKQToeCTLLv2lGV7QqBJM1vg
og4Q7tsiYxAEiqplF6vTABSnP4fqlfmO

And I want to output this :

45fd7bbdb5be9b735e8e85792e215177
45fd84989fb0897547529a123942a289
45fd91a6031d64f59c0bae8efb3ce4ca
45fd9ee96f4f19f31c9418bbfc65afac
45fdd39340740ca502d0f669bd5d604a

Onlye the lines that are composed with hexadecimal caracters.

Thank you for your reply.

sed -n '/[a-f0-9A-F]\{32\}/p' input
45fd7bbdb5be9b735e8e85792e215177
45fd84989fb0897547529a123942a289
45fd91a6031d64f59c0bae8efb3ce4ca
45fd9ee96f4f19f31c9418bbfc65afac
45fdd39340740ca502d0f669bd5d604a

I forgot the -n option you're right. It's working now.

Thank you for your fast replies guys :slight_smile:
And have a good new year !

Hello ganon551,

You could try an awk solution too for same.

awk --re-interval '{match($0,/[a-fA-F0-9]{32}/);if(substr($0,RSTART,RLENGTH)){print}}' Input_file

Output will be as follows.

45fd7bbdb5be9b735e8e85792e215177
45fd84989fb0897547529a123942a289
45fd91a6031d64f59c0bae8efb3ce4ca
45fd9ee96f4f19f31c9418bbfc65afac
45fdd39340740ca502d0f669bd5d604a
 

Thanks,
R. Singh

Yes, initially, I did not notice it, neither, when you posted the command embedded in the post text. Now that you corrected it, adding code tags, it looks obvious. Posting the input and desired output helps to test as well.

Heading that way. :wink:

Some sed s accept the xdigit character class.

What Aia suggested works with the sample input, and may be exactly what is wanted (a string of 32 or more hex digits appearing anywhere on an input line without regard to what other characters may be present on the line). But, what was requested in post #4 (Onlye(sic) the lines that are composed with hexadecimal characters.) would be something more like:

sed -n '/^[a-f0-9A-F]\{1,\}$/p' input

which prints lines that contain one or more hex digits that do not contain any characters that are not hex digits.

If you only want lines that contain exactly 32 hex digits and nothing else, that would be:

sed -n '/^[a-f0-9A-F]\{32\}$/p' input