A small AWK problem

I have a file tmp.out with contents:
2008-08-09 05:11:01
2008-08-09 08:52:59
2008-08-11 12:08:34
2008-08-11 12:15:40

I want the output to be:
3|0|1|71|2008-08-09 05:11:01|2008-08-30 11:19:28
4|0|1|71|2008-08-09 08:52:59|2008-08-30 11:19:28
5|0|1|71|2008-08-11 12:08:34|2008-08-30 11:19:28
6|0|1|71|2008-08-11 12:15:40|2008-08-30 11:19:28

Where:

  • The columns are delimited by '|'
  • The first column is initialized at 3 and incremented by one
  • Columns 2, 3, and 4 are hard-coded '0|1|71'
  • The last column is the current date (in the format listed above)

I did the initial work to read in the tmp.out from another file:
cut -d'|' -f67 filename.txt | awk '{print substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2), $2}'

Thanks,

  • CB

nawk -f chic.awk tmp.out

chic.awk:

BEGIN {
  FS=OFS="|"

  "date '+%Y-%m-%d %H:%M:%S'" | getline now
  cnt=3
  fld234="0|1|71"
}

{ $1 = cnt++ OFS fld234 OFS $1; $NF=$NF OFS now; print}

Sweet... it works.

Thx,

  • CB

you might not need to preprocess the original filename.txt file and do it all in awk.
If you post a sample record/line from the filename.txt file, it could help.

Okay so originally I was running the below code to get uniq date column from an extract file:

cut -d'|' -f67 extract.dat | uniq -u | awk '{print substr($1,1,4)"-"substr($1,5,2)"-"substr($1,7,2), $2}' > date.out

However, the current date column that you have in chic.awk (variable 'now'), should come before the above date

2008-08-30 11:19:28|2008-08-09 05:11:01
2008-08-30 11:19:28|2008-08-09 08:52:59
2008-08-30 11:19:28|2008-08-11 12:08:34
2008-08-30 11:19:28|2008-08-11 12:15:40

Can you help me with this small modification?

Thx,

  • CB
BEGIN {
  FS=OFS="|"

  "date '+%Y-%m-%d %H:%M:%S'" | getline now
  cnt=3
  fld234="0|1|71"
}

$1 = cnt++ OFS fld234 OFS now OFS $1

What's inside the 'extract.dat'?

Here is a sample record:

3|3|SRC|4684844~4443337||JOHN|P|SMITH||222 N MAIN ST|||||MANHATTAN BEACH||CA||90211-4333|840||||||||||||||||||||||||||||||||||||||7736654235|8876653354||||JOHN.SMITH@ATT.NET||||||2008-08-09 05:11:01||456123789|20080815||||||||||||||||||||||||||||||

The 67th field is the date/time I need to work with.

  • CB