Archive

Archive for March, 2014

“Generating summary data with awk

March 31, 2014 Leave a comment

Problem: A collegue asked me to do this. Generating a summary based on the week numbers. File input

$ less file1.txt
2013-03-01, Week 1, 20
2013-03-05, Week 1, 30
2013-03-04, Week 1, 20
2013-03-17, Week 1, 30
2013-03-19, Week 1, 40
2013-03-21, Week 1, 50
2013-03-01, Week 2, 20
2013-03-05, Week 2, 30
2013-03-04, Week 2, 20
2013-03-17, Week 2, 30
2013-03-19, Week 2, 40

File output

2013-03-01, 20, Week 1
2013-03-04, 20, Week 1
2013-03-05, 30, Week 1
2013-03-17, 30, Week 1
2013-03-19, 40, Week 1
2013-03-21, 50, Week 1
Total,190, Week 1
2013-03-01, 20, Week 2
2013-03-04, 20, Week 2
2013-03-05, 30, Week 2
2013-03-17, 30, Week 2
2013-03-19, 40, Week 2
Total,140, Week 2

Solution: Using awk.

$ awk -v FS="," -v OFS="," '{sum[$2]+= $3; print $1, $3, $2}
END {for (var in sum){print "Total", sum[var], var}}' file1.txt | sort -t, -k3
2013-03-01, 20, Week 1
2013-03-04, 20, Week 1
2013-03-05, 30, Week 1
2013-03-17, 30, Week 1
2013-03-19, 40, Week 1
2013-03-21, 50, Week 1
Total,190, Week 1
2013-03-01, 20, Week 2
2013-03-04, 20, Week 2
2013-03-05, 30, Week 2
2013-03-17, 30, Week 2
2013-03-19, 40, Week 2
Total,140, Week 2

Source: http://www.unix.com/shell-programming-scripting/245209-generating-summary-data-use-awk.html

Categories: awk Tags:

JLSN – incron, inotifywait

March 30, 2014 Leave a comment

Problem:
Needed to run a script every time a file was placed in a certain folder. So you want the script triggered when a file is created or deleted.

Solution:
To run in a shell install inotify-tools, you will get inotifywait. But if you want a daemon that runs and calls a script or performs an action then install incron. YMMV.

Read more about incron:
http://zipizap.wordpress.com/2013/11/15/incron/
http://www.howtoforge.com/triggering-commands-on-file-or-directory-changes-with-incron

Delete specific line numbers in a file – awk, sed

March 14, 2014 Leave a comment

Problem:
You want to delete certain lines in a file. Say 3,6,9,12 etc.

$ less file
first
second
third
fourth
fifth
sixth
seventh
eigth
nineth
tenth

Solution:
1. Using awk.

$ awk 'NR%3' file
first
second
fourth
fifth
seventh
eigth
tenth

My understanding of how it works.

$ man awk

NR The total number of input records seen so far.

$ awk 'NR' file 

Will print all the rows as NR returns a value which equates to true and thus gets printed.
If you want to only print the x row

$ awk 'NR == 2' file
second
$ awk 'NR == 5' file
fifth

The below example prints nothing. It is like saying print but the result is false so nothing gets printed.

$ awk '0' file

So what happens with NR%3 is that anything that is not 0 gets printed.
1%3 result is 1 it gets printed.
2%3 result is 2 it gets printed.
3%3 result is 0 it does not get printed.
4%3 result is 1 it gets printed.
and so on.

2. Using sed.

$ sed 'n;n;d' file
first
second
fourth
fifth
seventh
eigth
tenth

How I think it works.

$ sed 'n;n;d' file

n – move to the next line
n – move to the next line
d – delete that line.

I think that due to d repeating the next cycle, since we still have lines in the file, it starts over and repeats the same till EOF.

$ info sed

`n’
If auto-print is not disabled, print the pattern space, then,
regardless, replace the pattern space with the next line of input.
If there is no more input then `sed’ exits without processing any
more commands.

`d’
Delete the pattern space; immediately start next cycle.

NB: File must not contain blank lines.

Source:
http://www.unix.com/shell-programming-scripting/245088-how-delete-line-number-3-6-9-12-15-so.html

Categories: awk, Interesting, sed Tags: