Sed/Nawk help


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Sed/Nawk help
# 1  
Old 12-05-2011
Sed/Nawk help

Hi all,

I have the following piece of code which reformats a file but the part that isn't working converts a date from dd mmm ccyy format to 2 fields containing dd/mm/ccyy,ccyy-mm-dd hh:mm:ss.000000

This is for a DB2 load file. The problem I have is that the input dd values (the dd[3] variable in the beginning of the nawk) are only single character for the first 9 days of the month and I need them to be 2 character (filled with a leading 0) in the output file.

Code:
sed "s/\"//g;s/Jan/01/g;s/Feb/02/g;s/Mar/03/g;s/Apr/04/g;s/May/05/g;s/Jun/06/g;s/Jul/07/g;s/Aug/08/g;s/Sep/09/g;s/Oct/10/g;s/Nov/
11/g;s/Dec/12/g" $INPUT  |  nawk -v out=$OUTPUT ' split($2,dd," ") {d=dd[1]"/"dd[2]"/"dd[3]","dd[3]"-"dd[2]"-"dd[1]" "dd[4]".0000
00"}


BEGIN {
  FS=","
  OFS=","
}
{
  if ($1 == "Transaction ID") {}
  else if ( $9 != "") {
    split($9, aa, "-")
          if ( $12 == "" ) {
            v="\" \""
          }  else       {
        v=$12
                }
    print $1,d,$3,"\""$4"\"",$5,$6,$7,"\""$8"\"",aa[1],aa[2],aa[3],aa[4],aa[5],$9,$10,$11,v,$13,"",0,"","" > out
  } else {
      if ( $12 == "" ) {
            v="\" \""
          }  else       {
        v=$12
                }
    print $1,d,"\""$3"\"","\""$4"\"",$5,$6,$7,$8,"0","00000000","0000000000000000","0","\" \"","\" \"",$10,$11,v,$13,"","0","",""
 > out
  }
 }

# 2  
Old 12-05-2011
I'm not going to try to decipher all of that.... but nawk was sprintf...

Code:
d=sprintf("%02d/%02d/%02d ...", dd[1], dd[2], dd[3], ...);

# 3  
Old 12-05-2011
Thanks for the reply. That's one to bear in mind but I've just managed to get this working by adding
Code:
s/\,\([0-9] \)/\,0\1 /g

into the end of the sed as that's the only place in the file I get a single digit preceded by a comma and followed by a space.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nawk Problem - nawk out of space in tostring on

Hi.. i am running nawk scripts on solaris system to get records of file1 not in file2 and find duplicate records in a while with the following scripts -compare nawk 'NR==FNR{a++;next;} !a {print"line"FNR $0}' file1 file2duplicate - nawk '{a++}END{for(i in a){if(a-1)print i,a}}' file1in the middle... (12 Replies)
Discussion started by: Abhiraj Singh
12 Replies

2. Shell Programming and Scripting

Need help parsing data with sed and/or nawk

Good day all. I have the following entries of data in a file in a column, however, I need this data written on a single line with several parameters in a different order. Current format: Treatment ,parmeter1=value ,parmeter2=value ,parmeter3=value ,parmeter4=value... (7 Replies)
Discussion started by: BRH
7 Replies

3. Shell Programming and Scripting

Help in parsing xml file (sed/nawk)

I have a large xml file as shown below: <input> <blah> <blah> <atr="blah blah value = ""> <blah> <blah> </input> ..2nd chunk... ..3rd chunk... ...4th chunk... All lines between <input> and </input> is one 'order' and this 'order' is repeated... (14 Replies)
Discussion started by: shekhar2010us
14 Replies

4. Shell Programming and Scripting

Nesting - two nawk into one nawk

hi people; this is my two awk code: nawk '/cell+-/{r=(NF==8) ? $4FS$5FS$6 : NF==7 ? $4FS$5 : $4 ;c=split(r,rr);for (i=1;i<=c;i++){if(rr != "111111"){printf($3" %d ""\n",(i+3))}}printf("")}' /home/gc_sw/str.txt > /home/gc_sw/predwn.txt nawk -F'*' '{gsub(/ *$/,"")}$0=$1$($NF-2)'... (2 Replies)
Discussion started by: gc_sw
2 Replies

5. Shell Programming and Scripting

How to specify 'not case sensitive' in regex (nawk, sed, patern expencions)?

Is it possible to make the search in regular exprecion or in matching parts of sed, nawk and others to IGNORE the case of the search string? I mean, like if used 'grep' with -i option: > grep -i "abc" file I would like to be able to do the same, say, by nawk: > nawk '/abc/ {print $0}'... (4 Replies)
Discussion started by: alex_5161
4 Replies

6. Shell Programming and Scripting

how to access values of awk/nawk variables outside the awk/nawk block?

i'm new to shell scripting and have a problem please help me in the script i have a nawk block which has a variable count nawk{ . . . count=count+1 print count } now i want to access the value of the count variable outside the awk block,like.. s=`expr count / m` (m is... (5 Replies)
Discussion started by: saniya
5 Replies

7. Shell Programming and Scripting

Sed,nawk for inputfile and out put file same

Hi, I am using sed and nawk command for replace/add/remove on file records. But when I am using the inputfile name and directing output to the same inpu name file becomes empty. I don't want to use to copy it to another file and move it back to original file. Example: sed 's/./,&/17'... (2 Replies)
Discussion started by: svenkatareddy
2 Replies

8. Shell Programming and Scripting

Need help in sed or nawk replace RE

Dear Friends, I am trying to replace the following pattern , But I cant understand how to express the RE in this case . Please help me with a sed command to replace . (Address = 918h : Initial = 0000h : RD /WR (Address = 91Ah : Initial =... (2 Replies)
Discussion started by: user_prady
2 Replies

9. Shell Programming and Scripting

awk,nawk,sed, delimiter |~|

RECORD=NEW|~|VENDORN=LUCENT|~|VENDORM=CBX500_REAR|~|NETWORK=ATM|~|SUBNETWORK=N/A|~|SITE=CIL|~|REGION=KN|~|COUNTRY=PS|~|SWITCH=SWITCH1|~|E THERNET=N/A|~|LOOPBACK=N/A|~|SHELF=N/A|~|SLOT=14|~|SUBSLOT=N/A|~|STSCHAN=N/A|~|PORT=S14|~|DS1SLOT=N/A|~|LINE=N/A|~|LPORTID=N/A|~|CARDDESC=N/A|~|CARDTYPE=BAC2RT0... (7 Replies)
Discussion started by: knijjar
7 Replies

10. UNIX for Advanced & Expert Users

nawk use

I found a command who prints x lines before and after a line who contain a searched string in a text file. The command is : ------------------- nawk 'c-->0;$0~s{if(b)for(c=b+1;c>1;c--)print r;print;c=a}b{r=$0}' b=2 a=4 s="string" file1 ...where "b" and "a" are the number of lines to print... (2 Replies)
Discussion started by: ctap
2 Replies
Login or Register to Ask a Question