简体   繁体   中英

Using AWK to sort lines and columns together

I'm doing an assignment where I've been asked to find files between a certain size and sha1sum them. I've specifically been asked to provide the output in the following format:

Filename Sha1 File Size

all on one line. I'm not able to create additional files.

I've tried the following:

for listed in $(find /my/output -type f -size -20000c):
do
sha1sum "$listed" | awk '{print $1}'
ls -l "$listed" | awk '{print $9 $5}'
done

which gives me the required output fields, but not in the requested format, ie

sha1sum
filename filesize

Could anyone suggest a manner in which I'd be able to get all of this on a single line?

Thank you :)

With single pipeline:

find /my/path-type f -size -20000c -printf "%s " -exec sha1sum {} \; | awk '{ print $3,$2,$1 }'

An exemplary output (as a result of my local test) in the needed format FILENAME SHA1 FILESIZE :

./GSE11111/GSE11111_RAW.tar 9ed615fcbcb0b771fcba1f2d2e0aef6d3f0f8a11 25446400
./artwork_tmp 3a43f1be6648cde0b30fecabc3fa795a6ae6d30a 40010166

If you use the stat command to avoid needing to parse the output of ls , you can simply echo all the values you need:

while IFS= read -r -d '' listed
do
  echo "$listed" $(sha1sum "$listed") $(stat -c "%s" "$listed")
done < <(find /my/output -type f -size -20000c -print0)

Check your version of stat , though, the above is GNU. On OS X, eg, would be

stat -f "%z" $listed

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM