Operating System - Linux
1753846 Members
7521 Online
108807 Solutions
New Discussion юеВ

Re: how to get a CRC or a quick hash of a single line of text?

 
SOLVED
Go to solution
TwoProc
Honored Contributor

how to get a CRC or a quick hash of a single line of text?

I want to get a hash of a whole line of text. A CRC would probably do even better for this application that I'm working on.

I've never tried to get one before - short of writing one in C oh so many moons ago, but I figure that there's gotta be some out there already. Anyone can give me some advice on this?

Thanks!
We are the people our parents warned us about --Jimmy Buffett
8 REPLIES 8
James R. Ferguson
Acclaimed Contributor
Solution

Re: how to get a CRC or a quick hash of a single line of text?

Hi:

# echo "this is a line of text"|cksum
2829970435


# echo "this is a line of text"|md5sum
edb95cc51670abff69d0af7fa1c8122b

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to get a CRC or a quick hash of a single line of text?

Hi:

# echo "this is a line of text"|cksum
2829970435

# echo "this is a line of text"|md5sum
edb95cc51670abff69d0af7fa1c8122b

# echo "this is a line of text"|sum -r|cut -f1 -d" "
23527

See the manpages for 'sum(1)' in particular too.

Regards!

...JRF...
James R. Ferguson
Acclaimed Contributor

Re: how to get a CRC or a quick hash of a single line of text?

Hi (again):

/* NO POINTS PLEASE */

Sorry for the double post. I got an error posting and decided my reply had hit the black hole. Hence, I added a closing thought and submitted again.

Regards!

...JRF...
Sandman!
Honored Contributor

Re: how to get a CRC or a quick hash of a single line of text?

Yet another way of generating a hash value for every single line of a file:

# awk '{printf("%-15d\n", rand(srand(NR))*(10^9))}' file

~cheers
TwoProc
Honored Contributor

Re: how to get a CRC or a quick hash of a single line of text?

Thanks JRF!

That's exactly what I needed.

Much appreciated,
John
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: how to get a CRC or a quick hash of a single line of text?

JRF, 10 points for not being the only one who has double posted by accident, and fessing up. Most of the time (but not all), when I've double posted myself, it's been because it took soooo long to post, I thought it was stuck, and in the meantime I thought of something to add, so I added it and posted, unfortunately, twice.

So, you get 10 points for being there like I've been there, because I thought I was the only one who had posts that look like that. Yeah, I know you asked for no points, but since it is my thread and I get to decide, I decided to push 10 points on empathy.
:-) Nygeah :-)
We are the people our parents warned us about --Jimmy Buffett
TwoProc
Honored Contributor

Re: how to get a CRC or a quick hash of a single line of text?

Sandman,

Thanks for the nice awk script. Can I ask you how to add a few more things to that? I was going to shell script all of this, but since you've nailed down a piece of it in awk, I wonder if you'd mind helping me a bit more?

Give me a) line number, b) CRC type sum code, and c) the text itself. Separated by "^" symbol.

I hate to ask, but if you're strong on awk, it would get me rolling pretty quickly on my little project.

I think it would resemble:

awk '{printf("%d^%-15d^$*\n",somelinecountervariable rand(srand(NR))*(10^9))}' /tmp/junk

The problem that I'd have is identifying the linecounter variable. I'm assuming there's a builtin in awk for this, instead of having to count with a loop??? Also, I'm supposing tha t$* prints the contents of the line, or would I use the (NR) maybe?

Thanks if you can help!
We are the people our parents warned us about --Jimmy Buffett
Sandman!
Honored Contributor

Re: how to get a CRC or a quick hash of a single line of text?

>Give me a) line number, b) CRC type sum code, and c) the text itself. >Separated by "^" symbol.

NR stands for the line (aka record) number and $0 stands for the entire line.

# awk '{printf("%d^%-13d^%s\n", NR, rand(srand(NR))*(10^9), $0)}' file

~hope it helps