Write a new value to a column in AWK


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Write a new value to a column in AWK
# 1  
Old 03-31-2009
Write a new value to a column in AWK

I have a file with columns which are separated by tabs and spaces like

Code:
111          333 444 555  7gg 
333          555 678 778  6yy

I need to write a AWK commmand , to search for a pattern in column 1 eg "111" and replace the value in column 5 with say "7kk" , I tried the assignment $5="7kk" , but if I print the line {print $0} , the column are separated only by spaces, but I want to retain the original format.

Can anyone please help me with this

Last edited by Yogesh Sawant; 03-31-2009 at 04:35 AM.. Reason: added code tags
# 2  
Old 03-31-2009
HOpe This can help you..

Code:
$ echo "111          333 444 555  7gg"| awk '/111/ {sub($5,"7kk");print}'
111          333 444 555  7kk

Thanks
Sha
# 3  
Old 03-31-2009
Thanks a lot for your reply , but the command sub did not work
# 4  
Old 03-31-2009
Whish OS ..you are in ?

if you are using solaris...then use nawk instead on awk command!!

Thanks
SHa
# 5  
Old 03-31-2009
Sorry I should have mentioned that the OS is Sun Solaris 5.10.
{sub($5,"7kk");print}' works in nawk

Thanks again
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Write column

Hello, I looking for a simple way to write into a file in column, e.g: I've a file contains below: 1 And I wish add another number from another command something like this: 1 2 I hope have been clear. I would be able to display these values but I cannot write :confused: Regards,... (3 Replies)
Discussion started by: Arnaudh78
3 Replies

2. Shell Programming and Scripting

Get the average from column and write the value at the last field

Dear Experts, Kindly help me please to get the average from column 14 and to write the value at the last field., But we need to take as reference the column 16., example the first 4 lines has the same value in column 16, therefore I want ot get the average only for these lines in column 14. And... (2 Replies)
Discussion started by: jiam912
2 Replies

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

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

5. UNIX for Dummies Questions & Answers

Count the lines with the same values in a column and write the output to a file

Hey everyone! I have a tab delimited data set which I want to create an output contained the calculation of number of those lines with a certain value in 2nd and 3rd column. my input file is like this: ID1 1 10M AAATTTCCGG ID2 5 4M ACGT ID3 5 8M ACCTTGGA ID4 5 ... (7 Replies)
Discussion started by: @man
7 Replies

6. Shell Programming and Scripting

How to write to different files based on a column

Hi, I have the following sample file 32895901-d17f-414c-ac93-3e7e0f5ec240 AND @GDF_INPUT 73b129e1-1fa9-4c0d-b95b-4682e5389612 AUS @GDF_INPUT 40f82e88-d1ff-4ce2-9b8e-d827ddb39447 BEL @GDF_INPUT ffbcc6fe-ba35-489c-ae08-e70e8897aa23 BEL @FF_INPUT... (3 Replies)
Discussion started by: ramky79
3 Replies

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

8. Shell Programming and Scripting

Help need to write a script on column separation for syslog output in perl

HI Pros, I have a issue.I need to write a script to parse the logs got from syslog server and update the same in my database.I need the following output.I donot know perl and I heard it very easy to write in perl I have the sample log I need each column seperated by commas and all equals... (0 Replies)
Discussion started by: iron_michael86
0 Replies

9. Shell Programming and Scripting

How to write data in column

I Have Two Files, A And B. How Can I Rewrite Them Into A New File With The Following Pattern? File A 123 456 789 File B 235 478 987 Become 123 235 456 478 987 789 Thanks In Advance!! (8 Replies)
Discussion started by: c0384
8 Replies
Login or Register to Ask a Question