awk how to replace specific field with new value


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk how to replace specific field with new value
# 8  
Old 04-07-2011
Code:
 awk -v row=2 -v col=2 'FNR==1{print FILENAME} FNR==row{$col="100"}1' datafile*

You can specify the row and col where you want to make change.
# 9  
Old 04-07-2011
Code:
awk '{if (NR==2) $2=100; print $0}' input

this handles "only the 2nd column/field in 2nd line"
# 10  
Old 04-07-2011
It works!

Thank You.
# 11  
Old 04-07-2011
awk '{if(NR==2)$2=100;print $0}' input

---------- Post updated at 10:17 AM ---------- Previous update was at 10:16 AM ----------

Type 'man awk' and 'man bash' at the command line for complete documentation.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to add symbol to specific field

Trying to use awk to add a . to $4. The input and output is tab-delimeted, but the awk seems to add a . in front of $5 and is space-delimeted. It seems close, but I am not able to produce the desired output. Thank you :). file chr1 948895 949015 chr1:948895-949015 ISG15 chr1 ... (1 Reply)
Discussion started by: cmccabe
1 Replies

2. Shell Programming and Scripting

awk - how to print specific field if a string is matched

hi gurus, I would like to be able to use awk to process 1 file as such: abc 1 2 3 4 5 6 7 8 9 10 flags 1 2 4 flags 1 2 5 abc 2 3 4 5 6 7 8 9 10 11 flags 1 2 3 abc 4 5 6 7 8 9 6 7 78 89 flags 1 2 3 flags 1 2 4 flags 1 2 3 4 I would like to be able to print field 1 and 5 when the... (4 Replies)
Discussion started by: revaroo
4 Replies

3. Shell Programming and Scripting

awk to replace a specific field in certain condition

Hi, I have a file like below PRUM,67016800 ,CC ,C1,67016800 , ,Y,Y,2 ,CK,BX,FOX ,00000001,EA,00000001,20141120 00:00:00, ,N,Y,Y,CK ABCDEF... (7 Replies)
Discussion started by: mady135
7 Replies

4. UNIX for Dummies Questions & Answers

How to replace and remove few junk characters from a specific field?

I would like to remove all characters starting with "%" and ending with ")" in the 4th field - please help!! 1412007819.864 /device/services/heartbeatxx 204 0.547%!i(int=0) 0.434 0.112 1412007819.866 /device/services/heartbeatxx 204 0.547%!i(int=1) 0.423 0.123... (10 Replies)
Discussion started by: snemuk14
10 Replies

5. Shell Programming and Scripting

How to print with awk specific field different from specific character?

Hello, i need help with awk. I have this file: cat number DirB port 67 er_enc_out 0 er_bad_os 0 DirB port 71 er_enc_out 56 er_bad_os 0 DirB port 74 er_enc_out 0 er_bad_os 0 DirB port 75 ... (4 Replies)
Discussion started by: elilmal
4 Replies

6. Shell Programming and Scripting

AWK replace one field

I have some problem with this. Need to change field #3 to 5 if field #1 = A and filed #2 =B DataA C 6 T C B 4 R A B 3 T D E 5 4 I would like to do two things if statement is true, but can not get it to work. Here it prints column #3 if statement is true, and this works awk '{if... (3 Replies)
Discussion started by: Jotne
3 Replies

7. Shell Programming and Scripting

awk search/replace specific field, using variables for regexp & subsitution then overwrite file

Hello, I'm trying the solve the following problem. I have a file which I intend to use as a csv called master.csv The columns are separated by commas. I want to change the text on a specific row in either column 3,4,5 or 6 from xxx to yyy depending upon if column 1 matches a specified pattern.... (3 Replies)
Discussion started by: cyphex
3 Replies

8. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

9. Shell Programming and Scripting

Awk to replace a field

Hi I am using awk to replace the 4th feild of the input line. My code is below: REP_LINE=$(echo $Line | awk -v var=$REPL_DT '{$4=var; print}') It does replaces but all words/phrases after the 4th feild are erased. Is there a way to modify the 4th feild of the input line and to have all... (7 Replies)
Discussion started by: sugan
7 Replies

10. Shell Programming and Scripting

awk to replace particular field

i have bad data for instance, the use of capital letters in the middle of the word. I identified the errors, made a list and put it in a file error_correction.txt Incorrect,Correct VeNOM,Venom nos,NOS . . . My data file looks like this: vgr,bugatti veron,,3.5,Venom,6,.......,....... (14 Replies)
Discussion started by: VGR
14 Replies
Login or Register to Ask a Question