Eric Anderson
Eric Anderson

Reputation: 366

Having trouble with awk

I am trying to assign a variable to an awk statement. I am getting an error. Here is the code:

for i in `checksums.txt` do
md=`echo $i|awk -F'|' '{print $1}'`
file=`echo $i|awk -F'|' '{print $2}'`
done

Thanks

Upvotes: 0

Views: 87

Answers (3)

Dimitre Radoulov
Dimitre Radoulov

Reputation: 27990

You don't need external programs for this:

while IFS=\| read m f; do
  printf 'md is %s, filename is %s\n' "$m" "$f"
done < checksums.txt 

Edited as per new requirement. Given the file is already sorted, you could use uniq (assuming GNU uniq and md hash length of 33 characters):

uniq -Dw33  checksums.txt

If GNU uniq is not available, you can use awk (this version doesn't require a sorted input):

awk 'END {
  for (M in m)
    if (m[M] > 1) 
      print M, "==>", f[M]  
  }
{ 
  m[$1]++
  f[$1] = f[$1] ? f[$1] FS $2 : $2
  }' checksums.txt 

Upvotes: 1

phs
phs

Reputation: 11051

while read line
do
  set -- `echo $line | tr '|' ' '`
  echo md is $1, file is $2
done < checksums.txt

Upvotes: 0

Mat
Mat

Reputation: 206679

for i in `checksums.txt` do

This will try to execute checksums.txt, which is very probably not what you want. If you want the contents of that file do:

for i in $(<checksums.txt) ; do
  md=$(echo $i|awk -F'|' '{print $1}')
  file=$(echo $i|awk -F'|' '{print $2}')
  # ...
done

(This is not optimal, and will not do what you want if the file has lines with spaces in them, but at least it should get you started.)

Upvotes: 1

Related Questions