need help with AWK - urgent - Thx


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting need help with AWK - urgent - Thx
# 1  
Old 03-06-2009
need help with AWK - urgent - Thx

Hi,

I have the following code (I inherited and am new to awk):

nawk -F, '{OFS=","; print $1,$2,"0000"$3" ",$4" PH",$6," PAYROLL HOURS USD ",$5,$7,$8}' /tmp/payfile > /tmp/paywork1


Q: How do I modify this so that if length of $3 = 6 then only pad 00 and if its length = 5 then pad 000 and if its length = 4 then pad 0000 (like original code)

preferrably: an if statement to check on length of $3 and KEEP original line of code because of possible error occur with formatting of input $5, $7, and $8

Thanks!

Last edited by mtn17; 03-06-2009 at 06:08 PM..
# 2  
Old 03-06-2009
as suggest in another forum - check your awk's man pages for 'printf' formatting options.
Code:
nawk -F, -v OFS=',' 'length($3) < 8 {$3=sprintf("%08d", $3)} {print $1,$2,$3" ",$4" PH",$6," PAYROLL HOURS USD ",$5,$7,$8}' /tmp/payfile > /tmp/paywork1

# 3  
Old 03-06-2009
Thanks. I did try to look at printf function but some reasons, the inputs $5,$7, and $8 got messed up when they are being used below this line of code. However, the code that you just described seem to work. I am so happy :-)
# 4  
Old 03-06-2009
Quote:
Originally Posted by mtn17
Thanks. I did try to look at printf function but some reasons, the inputs $5,$7, and $8 got messed up when they are being used below this line of code. However, the code that you just described seem to work. I am so happy :-)
I don't follow, but ok....
# 5  
Old 03-06-2009
I did try using this equivalent code:
nawk -F, '{printf "%s,%s,%08d ,%s PH,%s, PAYROLL HOURS USD %d,%d,%d\n",$1,$2,$3,$4,$6,$5,$7,$8}' /tmp/payfile > /tmp/paywork1

--input $3 worked in terms of padded 0s to be 8 in length BUT subsequent format for $5,$7,$8 seem to be off.
# 6  
Old 03-06-2009
Quote:
Originally Posted by mtn17
I did try using this equivalent code:
nawk -F, '{printf "%s,%s,%08d ,%s PH,%s, PAYROLL HOURS USD %d,%d,%d\n",$1,$2,$3,$4,$6,$5,$7,$8}' /tmp/payfile > /tmp/paywork1

--input $3 worked in terms of padded 0s to be 8 in length BUT subsequent format for $5,$7,$8 seem to be off.
you're printing them as INTERGERS (%d) whereas in the original 'print' they;re explicitely printed as STRINGS.

Try changing the '%d' to '%s' for $5, $7, $8
# 7  
Old 03-09-2009
Thanks vgersh99 for your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Solaris

Plz help me with linked filesystems...thx

SunOS 5.10 Generic_142900-15 sun4u sparc SUNW,SPARC-Enterprise I need sys-admin help from someone. I have 2 file systems that are both mounted /CV and /eabatch. under /eabatch/in/data is a hardlink to to /CV/in/data lrwxrwxrwx 1 batchop batchtaa 11 Mar 6 2009 data -> /CV/in/data... (2 Replies)
Discussion started by: Harleyrci
2 Replies

2. Shell Programming and Scripting

awk or sed help urgent

Hello, I have a file with the follwoing pattern: Input file: =========== tcp://xxx:123 8179 YY 1798 YY tcp://abc:2345 not found tcp://swt:4945 7356 QQ tcp://pqr:456 8178 PP 9485 PP 4485 PP (8 Replies)
Discussion started by: uandme2k2
8 Replies

3. Shell Programming and Scripting

Urgent awk help !!

Hi All,I need to convert following field from a file L2578978CLC/576/116804 => L2578978CLC/00000576/00168304 i have to append Zeros in the third and fourth number after slash / in the above string (total length of number should be 8). means L2578978CLC/576/116804 should be converted to... (3 Replies)
Discussion started by: unknown123
3 Replies

4. Shell Programming and Scripting

urgent help awk script

Hi I have a scenario where i have a file name as abcd_To_hfgh.20090456778_1.dat I will get the filename as parameter and i need a string in between second _ and first . i.e i need hfgh in this case. Please help me with the script. This may not be awk script even if it can be... (2 Replies)
Discussion started by: dsdev_123
2 Replies

5. Shell Programming and Scripting

XML Awk : Urgent

Hi All, I am a beginner to Unix. So would really appreciate if people out here can help me out. I have a XML file which has a element <NoteData> in which two values DBHA and DAKO. I need to search these in all the XML files in a directory and if found in the XML file then replace the... (3 Replies)
Discussion started by: karansachdeva
3 Replies

6. UNIX for Dummies Questions & Answers

awk help urgent

Hi i am trying to read a line from a file and add the values in a new file eg Input file a1|a2|a3|a4|a5|a6 b1|b2|b3|b4|b5|b6 c1|c2|c3|c4|c5|c6 expected output File one a1|a2|a3 b1|b2|b3 c1|c2|c3 (3 Replies)
Discussion started by: rysh
3 Replies

7. Shell Programming and Scripting

Urgent!!!!! AWK

Thank everyone advanced. Would you please tell me how to use the shell variable in the awk body? I try follows but they all do not work, pls kindly help me: 1. str=`cat file2` awk 'BEGIN{print("'"$str"'"}' file1 > file3 2. str=`cat file2` awk 'BEGIN{print(ENVIRON}' file1 > file3 3.... (3 Replies)
Discussion started by: summer_cherry
3 Replies

8. Shell Programming and Scripting

Help me for AWK URGENT

i have a record like 1,23423,4545,6767,89898,3434,121212,123123,322 2,23233,3434,4545,56566,rxrx,e344343,343434,3434 1,23223,336,78787,78787,654,6767677,6877989,7878 i want to check the $6 field if its start with any letters, i want to move this record to some other file and keep rest of... (2 Replies)
Discussion started by: readycpbala
2 Replies

9. Shell Programming and Scripting

help me do a small program!! THX

Write a shell script (to run on the Bourne shell) that runs an infinite loop to check every 5 seconds to report on who logs into and who logs out of a UNIX system. Without loss of practical significance of this little utility, we can ignore multiple logins from the same user during a time period of... (1 Reply)
Discussion started by: lydragon
1 Replies
Login or Register to Ask a Question