awk padding column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk padding column
# 1  
Old 05-02-2011
awk padding column

Hello, i am trying to pad a column
Code:
A1
A2
A10
B2
B8
B12

to look like
Code:
A01
A02
A10
B02
B08
B12

using awk
Code:
 awk '{sprintf ("%c" $1 "%03d",int (0))}' file.txt

getting
awk: (FILENAME=file.txt FNR=1) fatal: not enough arguments to satisfy format string

Last edited by vgersh99; 05-02-2011 at 12:45 PM.. Reason: fixed code tags.
# 2  
Old 05-02-2011
Code:
nawk '$1=sprintf("%s%02d",substr($1,1,1),substr($1,2))' file.txt

# 3  
Old 05-02-2011
nawk is not an option for me Smilie
# 4  
Old 05-02-2011
Perhaps this will help...

Code:
$ echo A1 | awk '{printf ("%1c%02d",substr($1,1,1),substr($1,2,2))}'
A01

$ echo A12 | awk '{printf ("%1c%02d",substr($1,1,1),substr($1,2,2))}'
A12

This User Gave Thanks to joeyg For This Post:
# 5  
Old 05-02-2011
Quote:
Originally Posted by mykey242
nawk is not an option for me Smilie
is 'awk' an option then?
# 6  
Old 05-02-2011
Yes the awk method worked. Thank you very much.
I am getting more in depth with awk at my job, any good study guides to suggest. currently only finding targeted related scenarios in my searches.
# 7  
Old 05-02-2011
books

See
https://www.unix.com/unix-dummies-que...scripting.html

I have a couple of these books on a shelf behind my desk, for handy reference.
This User Gave Thanks to joeyg For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk script to append suffix to column when column has duplicated values

Please help me to get required output for both scenario 1 and scenario 2 and need separate code for both scenario 1 and scenario 2 Scenario 1 i need to do below changes only when column1 is CR and column3 has duplicates rows/values. This inputfile can contain 100 of this duplicated rows of... (1 Reply)
Discussion started by: as7951
1 Replies

2. Shell Programming and Scripting

awk to Sum columns when other column has duplicates and append one column value to another with Care

Hi Experts, Please bear with me, i need help I am learning AWk and stuck up in one issue. First point : I want to sum up column value for column 7, 9, 11,13 and column15 if rows in column 5 are duplicates.No action to be taken for rows where value in column 5 is unique. Second point : For... (1 Reply)
Discussion started by: as7951
1 Replies

3. Shell Programming and Scripting

Problems with awk (fatal error) and paste (two variables into one column-by-column)

Hello, I have a script extracting columns of useful numbers from a data file, and manipulating the numbers with awk commands. I have problems with my script... 1. There are two lines assigning numbers to $BaseForAveraging. If I use the commented line (the first one) and let the second one... (9 Replies)
Discussion started by: vgbraymond
9 Replies

4. Shell Programming and Scripting

awk Print New Column For Every Two Lines and Match On Multiple Column Values to print another column

Hi, My input files is like this axis1 0 1 10 axis2 0 1 5 axis1 1 2 -4 axis2 2 3 -3 axis1 3 4 5 axis2 3 4 -1 axis1 4 5 -6 axis2 4 5 1 Now, these are my following tasks 1. Print a first column for every two rows that has the same value followed by a string. 2. Match on the... (3 Replies)
Discussion started by: jacobs.smith
3 Replies

5. Shell Programming and Scripting

Sort, sed, and zero padding date column csv bash scripting

Hello people, I am having problem to sort, sed and zero padding of column in csv file. 7th column only. Input of csv file: 1,2,3,4,5,6,4/1/2010 12:00 AM,8 1,2,3,4,5,6,3/11/2010 9:39 AM,8 1,2,3,4,5,6,5/12/2011 3:43 PM,8 1,2,3,4,5,6,12/20/2009 7:23 PM,8 Output:... (5 Replies)
Discussion started by: sean1357
5 Replies

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

7. Shell Programming and Scripting

Padding space with each column

Hi How do I a add space with each column . Each column has fixed size like 1 st column = 10 2nd column = 20 In each column data can be of variable length.So rest of the length should be space Frank Student Sue Admin (3 Replies)
Discussion started by: Chinky23
3 Replies

8. Shell Programming and Scripting

AWK script to create max value of 3rd column, grouping by first column

Hi, I need an awk script (or whatever shell-construct) that would take data like below and get the max value of 3 column, when grouping by the 1st column. clientname,day-of-month,max-users ----------------------------------- client1,20120610,5 client2,20120610,2 client3,20120610,7... (3 Replies)
Discussion started by: ckmehta
3 Replies

9. Shell Programming and Scripting

for each different entry in column 1 extract maximum values from column 2 in unix/awk

Hello, I have 2 columns (1st column has multiple entries but the corresponding values in the column 2 may be the same or different.) however I want to extract unique values for each entry in column 1 by assigning the max value from column 2 SDF4 -0.211654 SDF4 0.978068 ... (1 Reply)
Discussion started by: Diya123
1 Replies

10. UNIX for Dummies Questions & Answers

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... (3 Replies)
Discussion started by: kshelluser
3 Replies
Login or Register to Ask a Question