Shell snippets 🔗

Posted by Médéric Ribreux 🗓 In kb/snippets/

#shell #snippets


Shell tools like Awk, Sed, Grep, etc. are great tools for data manipulation, especialy for CSV files! But I don't use them every day so I always have to re-read man pages and find how to use them. The tasks I do with awk or sed are often similars. So why don't put them somewhere and try to adapt them when I've found a better answer ?

Be careful: those snippets do not pretend to be THE solution to all of your problems...

Add a column to a bunch of CSV files

At work today, I had to add a new empty column to a lot of CSV files. Time to use sedagain:

find ./ -name '*.csv' -exec sed -i -e '1 s/$/,newcolumn/' -e '2,$ s/$/,/' {} \;


Concatenate n columns in one (awk+sed)

Here is a CSV file:


You want to extract only the first column and a concatenation of all of the others (like: "Borsdfsdf,sdfsdfsdf") and delete each null column.

Here is the awk (+sed) snippet to do it:

sed -e 's/,/;/' -e 's/,,//g' -e 's/,$//g' ./file.csv | awk -F ";" '{print "\""$1"\",\""$2"\""}'

Multiline sed

Here, I want to add a newline for strings where the previous line is not an empty newline. From this:

this is a line:
blah blah
blah blah

To this:

this is a line:

blah blah
blah blah

Sed is not multiline by default so you have to re-read the manual.

But, here is a snippet to do the job:

sed -E -e '{N;s/^([^\n]+)\n---([^\n]+)/\1\n\n---\2/;ty;P;D;:y}'