Home Man
Search
Today's Posts
Register

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

Tags
shell scripts

👤 Login to reply

 
Thread Tools Search this Thread
# 1  
Old 04-06-2011
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.
# 2  
Old 04-06-2011
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)
# 3  
Old 04-06-2011
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 07:23 PM.. Reason: I refactored it. My original version doesn't satisfy all of the criteria.
# 4  
Old 04-06-2011
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 07:46 PM..
# 5  
Old 04-06-2011
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
# 6  
Old 04-06-2011
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?
# 7  
Old 04-07-2011
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).
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Search and replace specific positions of specific lines dsid Shell Programming and Scripting 7 03-29-2017 02:38 PM
Need to replace last field in a file,if first field matches Sumanthsv Shell Programming and Scripting 4 07-04-2016 01:25 PM
awk to replace a specific field in certain condition mady135 Shell Programming and Scripting 7 11-24-2014 11:02 PM
How to replace and remove few junk characters from a specific field? snemuk14 UNIX for Dummies Questions & Answers 10 10-01-2014 11:32 AM
How to print with awk specific field different from specific character? elilmal Shell Programming and Scripting 4 08-22-2013 04:23 AM
Replace a field with a character as per the field length gani_85 Shell Programming and Scripting 2 01-31-2013 10:00 PM
awk search/replace specific field, using variables for regexp & subsitution then overwrite file cyphex Shell Programming and Scripting 3 09-23-2012 05:46 AM
Replace specific field on specific line sed or awk crownedzero Shell Programming and Scripting 14 11-27-2011 03:34 PM
Find and replace a string a specific value in specific location in AIX techmoris Shell Programming and Scripting 5 03-11-2010 06:24 PM
using sed to replace a specific string on a specific line number using variables todd.cutting Shell Programming and Scripting 2 08-13-2009 09:40 PM


All times are GMT -4. The time now is 01:18 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.
UNIX.COM Login
Username:
Password:  
Show Password