Unix/Linux Go Back    


Shell Programming and Scripting BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

awk how to replace specific field with new value

Shell Programming and Scripting


Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 04-06-2011   -   Original Discussion by setepo
setepo's Unix or Linux Image
setepo setepo is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 7 April 2011, 5:34 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
awk how to replace specific field with new value

I need to replace specific field (x) in a table with new value (y):


Input:

1 2 3 4
5 x 6 7
8 9 0 0


Output:

1 2 3 4
5 y 6 7
8 9 0 0


I have no idea how to do this.
Sponsored Links
    #2  
Old Unix and Linux 04-06-2011   -   Original Discussion by setepo
sk1418's Unix or Linux Image
sk1418 sk1418 is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 8 March 2012, 11:08 AM EST
Posts: 236
Thanks: 1
Thanked 55 Times in 54 Posts
replace all x into y?
or only in 2nd line?
or in lines that match some pattern? e.g. starts with 5?

the codes below change any x-fields to y only in the 2nd line.

Code:
awk '{if (NR==2)for(i=1;i<=NF;i++) $i=($i=="x")?"y":$i; print $0}' input

The Following User Says Thank You to sk1418 For This Useful Post:
freegnu (04-06-2011)
Sponsored Links
    #3  
Old Unix and Linux 04-06-2011   -   Original Discussion by setepo
freegnu's Unix or Linux Image
freegnu freegnu is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 9 June 2011, 1:56 PM EDT
Location: Brooklyn
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
This does it all. Remove parts to do one or the other.

Code:
awk '/^5/||(NR==2){for(i=1;i<=NF;i++)if($i=="x")$i="y";print $0};/^[^5]/&&(NR!=2)' input


Last edited by freegnu; 04-06-2011 at 08:23 PM.. Reason: I refactored it. My original version doesn't satisfy all of the criteria.
    #4  
Old Unix and Linux 04-06-2011   -   Original Discussion by setepo
Chubler_XL's Unix or Linux Image
Chubler_XL Chubler_XL is offline Forum Staff  
Moderator
 
Join Date: Oct 2010
Last Activity: 7 December 2017, 5:57 PM EST
Posts: 3,424
Thanks: 147
Thanked 1,212 Times in 1,112 Posts

Code:
awk 'NR==2 {sub(" x ", " y ")}1' input

or with sed

Code:
sed '2s/ x / y /' input


Last edited by Chubler_XL; 04-06-2011 at 08:46 PM..
Sponsored Links
    #5  
Old Unix and Linux 04-06-2011   -   Original Discussion by setepo
setepo's Unix or Linux Image
setepo setepo is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 7 April 2011, 5:34 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Thanks, it works.

But I have set of data and in every file instead of "x" is different number. I need to clear this field and after that fill with new value for example 100 (for all data sets).

data 1

0 0 0 0
0 3 0 0
0 0 0 0

data 2

0 0 0 0
0 1 0 0
0 0 0 0

data 3

0 0 0 0
0 8 0 0
0 0 0 0

output

0 0 0 0
0 100 0 0
0 0 0 0
Sponsored Links
    #6  
Old Unix and Linux 04-06-2011   -   Original Discussion by setepo
freegnu's Unix or Linux Image
freegnu freegnu is offline
Registered User
 
Join Date: Mar 2011
Last Activity: 9 June 2011, 1:56 PM EDT
Location: Brooklyn
Posts: 12
Thanks: 8
Thanked 0 Times in 0 Posts
Need more info

Is the value you want to replace always the second value in the second row? Or is it the only nonzero value in the file? Do the files only have 3 rows?
Sponsored Links
    #7  
Old Unix and Linux 04-07-2011   -   Original Discussion by setepo
setepo's Unix or Linux Image
setepo setepo is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 7 April 2011, 5:34 AM EDT
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
I want to replace always second value in second row.

My real input data has thousands of rows and about 20 columns with different numbers. In every file I want to replace value in specific field (for example second row, second column).
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Find and replace a string a specific value in specific location in AIX techmoris Shell Programming and Scripting 5 03-11-2010 07:24 PM
Using sed to replace specific character and specific position programmer22 Shell Programming and Scripting 5 10-11-2009 08:50 AM
using sed to replace a specific string on a specific line number using variables todd.cutting Shell Programming and Scripting 2 08-13-2009 10:40 PM
How to replace a specific word in specific column? ./hari.sh Shell Programming and Scripting 6 03-07-2009 09:33 AM
how to grep from specific field tiff-matt UNIX for Dummies Questions & Answers 2 07-23-2008 03:51 PM



All times are GMT -4. The time now is 11:55 AM.