AWK and padding values


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers AWK and padding values
# 1  
Old 11-28-2006
AWK and padding values

Hi,

I am trying to parse a file of fixed length transactions into a new file. Using awk I need to add the line number as a 5 digit long numeric value to each of the transactional rows.

i.e.
awk '{print <ROW NUM PADDED HERE>substr($0,1,8).........}' filename > newfile

I know that NR-1 gives me the row number (as the file has a header record), however if I add NR-1 to the awk command this will give me records that are varying length i.e. the transaction on row 10 will be 1 charachter longer than the records on rows 1-9, and the records on lines in excess of 100 will be longer still.

Therefore, as part of the awk statement I need to pad the row number with leading zeros until it is 5 numbers long (the row number will never exceed 99999). I have read that using typeset -Z may be the way to achieve this, however using the syntax below only one leading zero is added to the row number, i.e. 01,02,03......010,011......0100,0101 etc. again this creates rows of varying lengths.

awk '{print typeset -Z5 NR-1substr($0,1,8).........}' filename > newfile

Can anyone help me with the correct syntax?

Thanks.
# 2  
Old 11-28-2006
Code:
nawk '{printf("%.5d %s\n", NR-1, substr($0,1,8)).........}' filename > newfile

# 3  
Old 11-29-2006
Hi,

Thanks for that, it seems to work fine although it puts an unwanted space between the row number and the next value printed out, for example if ($0,1,8) is ABCDEFGH then the output to the newfile is:

00001 ABCDEFGH

rather than:
00001ABCDEFGH

Any ideas?

Thanks

Last edited by kshelluser; 11-29-2006 at 05:07 PM..
# 4  
Old 11-29-2006
Quote:
Originally Posted by kshelluser
Hi,

Thanks for that, it seems to work fine although it puts an unwanted space between the row number and the next value printed out, for example if ($0,1,8) is ABCDEFGH then the output to the newfile is:

00001 ABCDEFGH

rather than:
00001ABCDEFGH

Any ideas?

Thanks
well......... it's kind of obvious if you looked at the solution - just remove the 'space' in 'printf'....
Code:
nawk '{printf("%.5d%s\n", NR-1, substr($0,1,8)).........}' filename > newfile


Last edited by vgersh99; 11-29-2006 at 05:26 PM..
 
Login or Register to Ask a Question

Previous Thread | Next Thread

8 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk file to read values from Db2 table replacing hard coded values

Hi, I want to replace a chain of if-else statement in an old AWK file with values from Db2 table or CSV file. The part of code is below... if (start_new_rec=="true"){ exclude_user="false"; user=toupper($6); match(user, "XXXXX."); if (RSTART ==2 ) { ... (9 Replies)
Discussion started by: asandy1234
9 Replies

2. Shell Programming and Scripting

How to find the X highest values in a list depending on the values of another list with bash/awk?

Hi everyone, This is an exemple of inpout.txt file (a "," delimited text file which can be open as csv file): ID, Code, Value, Store SP|01, AABBCDE, 15, 3 SP|01, AABBCDE, 14, 2 SP|01, AABBCDF, 13, 2 SP|01, AABBCDE, 16, 3 SP|02, AABBCED, 15, 2 SP|01, AABBCDF, 12, 3 SP|01, AABBCDD,... (1 Reply)
Discussion started by: jeremy589
1 Replies

3. Shell Programming and Scripting

awk to substitute ip without zero left padding

Hello All, I have this script to awk IP to new file. #awk '/myip|yourip/ {sub(/...\....\....\..../, newip)}1' newip=$IP existing.txt > new.txt When existing.txt has myip=192.168.123.123 and $IP has 192.168.12.12, the awk script is not working. But while I add zero left padding to $IP i.e,... (3 Replies)
Discussion started by: Shaan_Shaan
3 Replies

4. Shell Programming and Scripting

bash padding

Hi all Is there a way to pad the output of a bash script see that code below for i in `sed -n '/Start Printer/,/End Printer/p' /u/ab/scripts/hosts.conf | awk '!/^#/ {print $2}' | egrep -v 'broke|primera' `; do pages=`snmpget -Ov -v1 -c public $i sysLocation.0 | awk '{print $2}'` ... (3 Replies)
Discussion started by: ab52
3 Replies

5. Shell Programming and Scripting

awk padding column

Hello, i am trying to pad a column A1 A2 A10 B2 B8 B12 to look like A01 A02 A10 B02 B08 B12 using awk (6 Replies)
Discussion started by: mykey242
6 Replies

6. Shell Programming and Scripting

AWK: read values from file1; search for values in file2

I have read another post about this issue and am wondering how to adapt it to my own, much simpler, issue. I have a file of user IDs like so: 333333 321321 546465 ...etc I need to take each number and use it to print records wherein the 5th field matches the user ID pulled from the... (2 Replies)
Discussion started by: Bubnoff
2 Replies

7. Shell Programming and Scripting

How to pick values from column based on key values by usin AWK

Dear Guyz:) I have 2 different input files like this. I would like to pick the values or letters from the inputfile2 based on inputfile1 keys (A,F,N,X,Z). I have done similar task by using awk but in that case the inputfiles are similar like in inputfile2 (all keys in 1st column and values in... (16 Replies)
Discussion started by: repinementer
16 Replies

8. UNIX for Dummies Questions & Answers

Padding

Hi Can anyone tell me how to pad zeroes on the left side to a numeric string in unix shell scripting Your answer is very much appreciated Thanks Vijay (2 Replies)
Discussion started by: vijaygopalsk
2 Replies
Login or Register to Ask a Question