Sponsored Content
Top Forums Shell Programming and Scripting NAWK: changing string-format with split Post 302559548 by regisl67 on Tuesday 27th of September 2011 11:13:57 AM
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..
 

10 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

5. 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

6. 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

7. 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

8. 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

9. 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

10. 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
PDF::API2::Basic::PDF::Filter(3pm)			User Contributed Perl Documentation			PDF::API2::Basic::PDF::Filter(3pm)

NAME
PDF::API2::Basic::PDF::Filter - Abstract superclass for PDF stream filters SYNOPSIS
$f = PDF::API2::Basic::PDF::Filter->new; $str = $f->outfilt($str, 1); print OUTFILE $str; while (read(INFILE, $dat, 4096)) { $store .= $f->infilt($dat, 0); } $store .= $f->infilt("", 1); DESCRIPTION
A Filter object contains state information for the process of outputting and inputting data through the filter. The precise state information stored is up to the particular filter and may range from nothing to whole objects created and destroyed. Each filter stores different state information for input and output and thus may handle one input filtering process and one output filtering process at the same time. METHODS
PDF::API2::Basic::PDF::Filter->new Creates a new filter object with empty state information ready for processing data both input and output. $dat = $f->infilt($str, $isend) Filters from output to input the data. Notice that $isend == 0 implies that there is more data to come and so following it $f may contain state information (usually due to the break-off point of $str not being tidy). Subsequent calls will incorporate this stored state information. $isend == 1 implies that there is no more data to follow. The final state of $f will be that the state information is empty. Error messages are most likely to occur here since if there is required state information to be stored following this data, then that would imply an error in the data. $str = $f->outfilt($dat, $isend) Filter stored data ready for output. Parallels "infilt". NAME
PDF::API2::Basic::PDF::ASCII85Decode - Ascii85 filter for PDF streams. Inherits from PDF::API2::Basic::PDF::Filter NAME
PDF::API2::Basic::PDF::RunLengthDecode - Run Length encoding filter for PDF streams. Inherits from PDF::API2::Basic::PDF::Filter NAME
PDF::API2::Basic::PDF::ASCIIHexDecode - Ascii Hex encoding (very inefficient) for PDF streams. Inherits from PDF::API2::Basic::PDF::Filter perl v5.14.2 2011-03-10 PDF::API2::Basic::PDF::Filter(3pm)
All times are GMT -4. The time now is 04:42 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy