Showing posts with label shell. Show all posts
Showing posts with label shell. Show all posts

Apr 8, 2012

Manipulating CSV files with Unix Shell


The quick and dirty way to manipulate CSVs from the Shell.

Say you want to create a new CSV subset file with just two specific fields from the original CSV.  The one liner code is:



awk -F\, '{printf "%s,%s\n",$1,$3}' original.csv > new.csv


How about sorting on the second field?


cat new.csv | awk -F\, '{printf "%s,%s\n",$1,$2}'  | sort -t, -k 2 > newsorted.csv

Dec 6, 2011

Writing a Shell Script


Paired Bash Control Structures

if, while, until, for and case
Paired? Yes, starts and ends with a tag.

-f
checks if it is a regular file

-d
check if the file is a directory

-e
check if the file exists

-f
check if the file is a regular file

-g
check if the file has SGID permissions ???

-r
check if the file is readable

-s
check if the file's size is not 0

-u
check if the file has SUID permissions

-w
check if the file is write-able

-x
check if the file is executable

if [ -f /etc/foo ]; then
If-then control structure.

Finding and listing files with a string of interest in a directory and sub-directories.
find . -type f -exec grep "stringofinterest" {} \; -print


References:
1. Tips for Linux: codecoffee.com