Renumber column using Awk


 
Thread Tools Search this Thread
Special Forums UNIX Desktop Questions & Answers Renumber column using Awk
# 1  
Old 05-20-2010
Error Renumber column using Awk

I have a data set similar to that below and I need to add a column $2 (which numbers the lines from 1- end)

Text Input Output x y z
Text Input Output x y z
Text Input Output x y z
Text Input Output x y z

I also need to have various column widths so $1 is 6 spaces $2 is 5, $3 is 5, $4 is 7. I have used awk '{print FNR "\t" $2}' and now have a tab delineated file (which is numbered). Can anyone help adjusting the output? I do not understand how to use the printf commands to specify column width.

Thank you for any assistance you are willing to share
# 2  
Old 05-20-2010
printf uses a format string.
Code:
awk '{ printf("%6d\t%5s\t%5s\t%7s\n", $1, $2, $3, $4) }' filewithnumbercolumn > newfile

This User Gave Thanks to jim mcnamara 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

Remove line containing string and renumber

Hello, I have some files in a directory and a short list of strings. I want to loop through the files and remove lines containing the string and renumber. There are some issues. The first is the strings that can contain troublesome characters like single quotes and parenthesis. Here is one... (12 Replies)
Discussion started by: LMHmedchem
12 Replies

5. Shell Programming and Scripting

awk script to renumber

I have input file like MODEL 1 ATOM 1 N GLY 121 27.310 57.180 59.640 1.00 0.00 ATOM 2 H1 GLY 121 27.320 57.560 60.610 1.00 0.00 MODEL 2 ATOM 1 N GLY 121 28.430 56.080 59.750 1.00 0.00 ATOM 2 H1 GLY 121 ... (2 Replies)
Discussion started by: dsp80
2 Replies

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

7. Shell Programming and Scripting

awk or sed: change the color of a column w/o screwing up column spacing

Hey folks. I wrote a little awk script that summarizes /proc/net/dev info and then pipes it to the nix column command to set up column spacing appropriately. Here's some example output: Iface RxMBytes RxPackets RxErrs RxDrop TxMBytes TxPackets TxErrs TxDrop bond0 9 83830... (3 Replies)
Discussion started by: ryran
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. Shell Programming and Scripting

Renumber Residues in .pdb

Hello scripting Gurus! I am dealing with a .pdb script which has the following general format: ATOM 920 C GLY B 103 -13.977 7.468 -11.253 1.00 0.00 C ATOM 921 O GLY B 103 -14.817 7.213 -12.116 1.00 0.00 O ATOM 922 H GLY B 103 ... (7 Replies)
Discussion started by: marybnewcomb
7 Replies
Login or Register to Ask a Question