开发者

Having trouble with awk

开发者 https://www.devze.com 2023-04-12 05:24 出处:网络
I am trying to assign a variable to an awk statement. I am getting an error. Here is the code: f开发者_如何学Goor i in `checksums.txt` do

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

f开发者_如何学Goor i in `checksums.txt` do
md=`echo $i|awk -F'|' '{print $1}'`
file=`echo $i|awk -F'|' '{print $2}'`
done

Thanks


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.)


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 


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

精彩评论

暂无评论...
验证码 换一张
取 消