Bash-Fu

Perform actions on files containing a certain string

A quick snippet to find from the local files that contain a certain string and move them to another folder using grep and xargs:

grep -Rl . -e "mystring" | xargs -0 -d '\n' -I '{}' mv '{}'  destination/path

Explanations:

  • for grep, -R is for recursive, -l is for the location only (skip content), and -e is the pattern
  • for xargs, -d '\n' is to switch the delimiter to new line, -I '{}' to use '{}' as placeholder for the subsequent commands.

Extract columns from all CSVs

Given a folder with lots of CSV files with similar structure, here’s a one-liner to extract a column using Ruby. This isn’t the most performant, but it should be sufficient for many cases.

ls | xargs -I '{}' cat {} | ruby -rcsv -e 'lines=CSV.new(ARGF.read, headers:true); lines.each{|line| puts line["href"] }'

Convert all XLSX files in a folder to CSV

We’d need to install xlsx2csv pip package with pip3 install xlsx2csv.

ls *.xlsx | xargs -I {} basename {} .xlsx | xargs -I {} xlsx2csv {}.xlsx {}.csv

This will create a CSV file with the same name as the input XLSX file in the current folder.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.