rocarvaj
6/2/2015 - 5:41 PM

Bash/cli tips!

Bash/cli tips!

Print from line n on

tail -n +2 file.csv

From the man page:

-n, --lines=N
     output the last N lines, instead of the last 10
...

If the first character of N (the number of bytes or lines) is a '+', print beginning with the Nth item from the start of each file, other- wise, print the last N items in the file.

file loops

while IFS='' read -r line || [[ -n "$line" ]]; do
echo $line
done < "my_file.txt"

file loop, split by comma

while IFS=, field1 field2 field3; do
echo $field2
done < "my_file.txt"

Rename multiple files

Use rename (aka prename) which is a Perl script which may be on your system already. Do it in two steps:

find -name "* *" -type d | rename 's/ /_/g'    # do the directories first
find -name "* *" -type f | rename 's/ /_/g'

Edit:

Based on Jürgen's answer and able to handle multiple layers of files and directories in a single bound using the "Revision 1.5 1998/12/18 16:16:31 rmb1" version of /usr/bin/rename (a Perl script):

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

Text file difference

Get lines from fileB that are not in fileA

comm <(sort fileA) <(sort fileB) -3

Ref: http://unix.stackexchange.com/a/28185