Thursday 6 August 2009

Stripping out parts of a filename from an ls

command:
   ls * | cut -f1 -d '_'

in dir with these files:
  6250_DBAGZ_Asset_CF.20090610
   1149_DBAGZ.20090716
   7321_DBAGZ_Asset_CF.20090716
   HYB_DBAGZ_Assets.20090716

output:
   6250
   1149
   7321
   HYB

breakdown:
  cut
  -f1 >>>>> field 1
  -d '_' >>>>>> use delimeter '_' Default is TAB not space

Advanced real word usage:
Searches through nginx log access file for user agent (field 9) - Prints number of user agents by type. Useful for spotting scrapers:
  cat /log/nginx_access/FILE | cut -f 9 | sort | uniq -c | sort -nr | less -S

No comments:

Post a Comment