Showing posts with label awk. Show all posts
Showing posts with label awk. Show all posts

Tuesday, March 27, 2012

AWK documentation

http://sunsite.ualberta.ca/Documentation/Gnu/gawk-3.1.0/html_chapter/gawk.html

Day of the Week in GAWK

gawk '
BEGIN {
  for (d = 1; d <= 31; d++) {
    t = mktime("2008 08 " d " 0 0 0")
    print d, strftime("%A", t)
  }
}'


The output is:

1 Friday
2 Saturday
3 Sunday
4 Monday
5 Tuesday
6 Wednesday
7 Thursday
8 Friday
9 Saturday
10 Sunday
11 Monday
12 Tuesday
13 Wednesday
14 Thursday
15 Friday
16 Saturday
17 Sunday
18 Monday
19 Tuesday
20 Wednesday
21 Thursday
22 Friday
23 Saturday
24 Sunday
25 Monday
26 Tuesday
27 Wednesday
28 Thursday
29 Friday
30 Saturday
31 Sunday

Compare to:
$ cal 8 2008
    August 2008
Su Mo Tu We Th Fr Sa
                1  2
 3  4  5  6  7  8  9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30
31

Wednesday, February 8, 2012

AWK and the here-doc string


awk '{print $1}' <<!
jack be
nimble jack be
quick.
!
jas@hardy:~/awk$ ./here.sh
jack
nimble
quick.

AWK: convert lines in a file to columns


#!/bin/bash

awk '

/Name:/, /Zip:/ { printf "%s ", $0 }
/Zip:/ { print "" }

' <<EOT
Name: John Doe
Age: 32
Zip: 60324
Name: Jane Doe
Age: 34
Zip: 54930
Name: Skippy
Age:134
Zip:23456
EOT

Friday, June 24, 2011

AWK and Java CLASSPATH

From http://unix-simple.blogspot.com/2008/12/dynamically-building-java-classpaths.html

for line in $java_dir/*.jar
do
  CLASSPATH="$CLASSPATH:$line"
done

Lets put AWK to iterate:

CLASSPATH=$CLASSPATH:$(ls *.jar | awk 'BEGIN { ORS = ":" } { print }')

Maybe a shellish way of do it:

CLASSPATH=$CLASSPATH:`echo *.jar | sed 's/ /:/g'`

Friday, August 27, 2010

Counting lines in a file (61960627 lines)

sed -n '$=' file.txt

real    1m9.237s
user    1m8.602s
sys     0m0.631s

perl -ne 'END { print $NR }' file.txt

real    0m13.876s
user    0m13.245s
sys     0m0.630s

awk 'END { print NR }' file.txt

real    0m8.866s
user    0m8.257s
sys     0m0.608s

wc -l file.txt

real    0m2.550s
user    0m1.677s
sys     0m0.873s

wc file.txt

real    3m4.875s
user    3m3.970s
sys     0m0.895s

Tuesday, April 13, 2010

Last Sunday of the Month in UNIX shell

cal | grep '^[23]' | tail -1 | cut -d' ' -f1

Several months later I don't remember why the grep part?

cal | tail -1 | cut -d' ' -f1


AWK version:



#!/bin/bash


cal | awk '
    { last = $1 }
END { print last }'


##END##

Friday, January 22, 2010

awk RE range

awk '/MESSAGE: 2010-01-20/,/MESSAGE: 2010-01-21/ { print }' info.log > info.txt