NAWK: changing string-format with split


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting NAWK: changing string-format with split
# 1  
Old 09-27-2011
Question NAWK: changing string-format with split

Hi all,
I try to make a awk-script, which counts lines, summarized by pdf and xml.
So far it works, but for sorting reasons, I'd like to change the format from the field $1 from dd-mm-yyyy to yyyy-mm-dd.

This works find, but: split() and sprintf() prints its output (for no reason, the results looks like this:

Code:
22-09-2011 09:15:00 Doinggthings 49490388905_49490994.PDF
2011-09-22 09:15:00 Doinggthings 49490388905_49490994.PDF
22-09-2011 09:15:00 Doinggthings 49445688905_49499494.XML
2011-09-22 09:15:00 Doinggthings 49445688905_49499494.XML
23-09-2011 11:20:00 Doinggthings 49490312305_94689494.PDF
2011-09-23 11:20:00 Doinggthings 49490312305_94689494.PDF
23-09-2011 11:20:00 Doinggthings 49490388905_49378494.XML
2011-09-23 11:20:00 Doinggthings 49490388905_49378494.XML
     Datum  Total Files   Total PDFs   Total XMLs
2011-09-22          2          1          1
2011-09-23          2          1          1

Inputfile:
Code:
22-09-2011 09:15:00 Doinggthings 49490388905_49490994.PDF
22-09-2011 09:15:00 Doinggthings 49445688905_49499494.XML
23-09-2011 11:20:00 Doinggthings 49490312305_94689494.PDF
23-09-2011 11:20:00 Doinggthings 49490388905_49378494.XML

My actual script:
Code:
#!/usr/bin/nawk -f

BEGIN {
        FS=" "
        IGNORECASE = 1 }

split($1, d, "-")
$1 = sprintf("%s-%s-%s", d[3],d[2],d[1])

$1 != "" && $NF ~ /\.PDF/ {a[$1]++;b[$1]++}
$1 != "" && $NF ~ /\.XML/ {a[$1]++;c[$1]++}

END {
        printf("%10s %12s %12s %12s\n", "Datum", "Total Files", "Total PDFs", "Total XMLs" )
        for (i in a)
                printf("%s %10.0f %10.0f %10.0f\n", i, a[i], b[i], c[i] )}
                #printf("%s %10.0f %10.0f %10.0f\n", i, a[i,t], b[i,p], c[i,x] )}

I have to use nawk instead of awk, because awk dosn't support split().
I run this on Solaris 10.
Does nawk behave differently?

Has anybody a good idea?

Thanks a lot.
Regis

Last edited by pludi; 09-27-2011 at 07:11 PM..
# 2  
Old 09-27-2011
You need to put your split code in a {} pair:

Code:
#!/usr/bin/nawk -f
 
BEGIN {
        FS=" "
        IGNORECASE = 1 }
{
   split($1, d, "-")
   $1 = sprintf("%s-%s-%s", d[3],d[2],d[1])
}
 
$1 != "" && $NF ~ /\.PDF/ {a[$1]++;b[$1]++}
$1 != "" && $NF ~ /\.XML/ {a[$1]++;c[$1]++}
 
END {
        printf("%10s %12s %12s %12s\n", "Datum", "Total Files", "Total PDFs", "Total XMLs" )
        for (i in a)
                printf("%s %10.0f %10.0f %10.0f\n", i, a[i], b[i], c[i] )}
                #printf("%s %10.0f %10.0f %10.0f\n", i, a[i,t], b[i,p], c[i,x] )}

# 3  
Old 09-28-2011
Uh?! It was so simple ... and works perfect. Thank you very much for your help! Régis
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Date format conversion how to change this from using nawk to awk

Hi, I have a file where I need to change the date format on the nth field from DD-MM-YYYY to YYYY-MM-DD so I can accurately sort the record by dates From regex - Use sed or awk to fix date format - Stack Overflow, I found an example using nawk. Test run as below: $: cat xyz.txt A ... (2 Replies)
Discussion started by: newbie_01
2 Replies

2. Shell Programming and Scripting

Using awk or nawk to convert epoch time to date format

Looking for some help and usually when I do a search this site comes up. Hopefully someone can give me a little direction as to how to use one of these two commands to achieve what I'm trying to do. What am I trying to do? I need to take the time value in epoch format returned from the... (5 Replies)
Discussion started by: minigts
5 Replies

3. Shell Programming and Scripting

Nawk Format

Hi! I have a file which I want to search daily for any line that contains the work 'Reason' and I want to take that line and put the data in a certain format using awk or nawk....I do not have gawk on my machine so it would have to be awk or nawk, or sed would work as well. Here are some examples... (9 Replies)
Discussion started by: ther2000
9 Replies

4. Shell Programming and Scripting

how to parse the file in xml format using awk/nawk

Hi All, I have an xml file with the below format. <a>111</a><b>222</b><c>333<c><d><e>123</e><f>234</f><d><e>456</e><f>789</f> output needed is 111,222,333,123,234 111,222,333,456,789 nawk 'BEGIN{FS="<|>"} {print a,b,c,e,f a="" ... (7 Replies)
Discussion started by: natalie23
7 Replies

5. UNIX for Dummies Questions & Answers

Changing from Excel date format to MySQL date format

I have a list of dates in the following format: mm/dd/yyyy and want to change these to the MySQL standard format: yyyy-mm-dd. The dates in the original file may or may not be zero padded, so April is sometimes "04" and other times simply "4". This is what I use to change the format: sed -i '' -e... (2 Replies)
Discussion started by: figaro
2 Replies

6. Shell Programming and Scripting

Format - Inventory Row data into Column - Awk - Nawk

Hi All, I have the following file that has computer data for various pcs in my network... Snap of the file is as follows ******************************************************************************* Serial 123456 Computer IP Address lo0:... (1 Reply)
Discussion started by: aavam
1 Replies

7. Shell Programming and Scripting

awk/nawk question to format a file

Hi, I am new to awk/nawk, needs help. I want to merge the rows having emplid attribute same into a single row in the following file. In actual this kind of file will have around 50k rows. Here is my input file id|emplid|firstname|dep|lastname 1|001234|test|1001|1 2|002345|test|1032|2... (7 Replies)
Discussion started by: kumar04
7 Replies

8. Shell Programming and Scripting

changing month in Mmm format to mm FORMAT

i have an variable mydate=2008Nov07 i want o/p as in variable mymonth=11 (i.e nov comes on 11 number month) i want some command to do this for any month without using any loop. plz help me (1 Reply)
Discussion started by: RahulJoshi
1 Replies

9. UNIX for Dummies Questions & Answers

Split BIG report using nawk

I have the following nawk script: nawk -F: '{ if($0 ~ "^Report No") {fl=1; i=0;} if(fl==1){data=$0; i++} if($0 ~ "^BE NO:") { fname = "reprot_"$2".lis"; gsub(" ","",fname); for(j=0;j<i;j++) print data > fname; fl=0; } else if(fl==0) print $0 > fname; }' filename When I try to apply... (1 Reply)
Discussion started by: raychu65
1 Replies

10. UNIX for Dummies Questions & Answers

Nawk Split

Hello. I have a input file in this format: line1data :: line1data :: line1data line2data :: line2data :: line2data I would like to split each lines data element into an array: array1=line1data array1=line1data array1=line1data array2=line2data array2=line2data array2=line2data I... (3 Replies)
Discussion started by: steveramsey
3 Replies
Login or Register to Ask a Question