How to Replace the value of a column using awk command?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to Replace the value of a column using awk command?
# 1  
Old 01-07-2013
How to Replace the value of a column using awk command?

Hi
cat test.txt

Code:
H|123|341|567|asfg
D|dfg|trtyy|errt
D|ert|frty|wer

Here I need to replace the third column value with 100 of the first record only and while printing I need to print the full file content also..I am expecting a result like this

Code:
H|123|100|567|asfg
D|dfg|trtyy|errt
D|ert|frty|wer

Can some one please help on this

Last edited by Scrutinizer; 01-07-2013 at 04:34 AM.. Reason: code tags
# 2  
Old 01-07-2013
Try:
Code:
awk -F"|" -vOFS="|" 'NR==1{$3=100}1' file

# 3  
Old 01-07-2013
Syntax Error

Thanks for the quick post ..But its giving me syntax error

Code:
awk: syntax error near line 1
awk: bailing out near line

---------- Post updated at 03:29 AM ---------- Previous update was at 03:24 AM ----------

I tried something like this
Code:
awk -F"|" -vOFS="|" 'NR==1{$3=100} {print}' file

Its working , but the problem is that its replacing the delimter pipe with space

Last edited by Scrutinizer; 01-07-2013 at 04:34 AM.. Reason: code tags
# 4  
Old 01-07-2013
Code:
 
try this..
 
$ nawk -F\| 'NR==1{$3=100;OFS="|";}1' input.txt
H|123|100|567|asfg
D|dfg|trtyy|errt
D|ert|frty|wer

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

awk script to extract a column, replace one of the header and replace year(from ddmmyy to yyyy)

I have a csv which has lot of columns . I was looking for an awk script which would extract a column twice. for the first occurance the header and data needs to be intact but for the second occurance i want to replace the header name since it a duplicate and extract year value which is in ddmmyy... (10 Replies)
Discussion started by: Kunalcurious
10 Replies

2. Shell Programming and Scripting

Trying to get an awk script to replace values in column

I'm trying to make an awk script to compare values I've set as var1, var2, and var3 earlier in the script to the values in the userinputted column of four text files called Node1.txt, Node2.txt, Node3.txt, and Node4.txt and then replace the values in that userinputted column with either ttt or gcc,... (8 Replies)
Discussion started by: Eric1
8 Replies

3. Shell Programming and Scripting

awk Match First Field and Replace Second Column

Hi Friends, I have looked around the forums and over online but couldn't figure out how to deal with this problem input.txt gene1,axis1/0/1,axis2/0/1 gene1,axis1/1/2,axis2/1/2 gene1,axis1/2/3,axis2/2/3 gene2,axis1/3/4,axis2/3/4 Match on first column and if first column is... (1 Reply)
Discussion started by: jacobs.smith
1 Replies

4. Shell Programming and Scripting

Awk: Multiple Replace In Column From Two Different Files

Master_1.txt 2372,MTS,AP 919821,Airtel,DL 0819,MTS,MUM 919849788001,Airtel,AP 1430,Aircel MP,20 405899143999999,MTS,KRL USSDLIKE,MTS,DEL Master_2.txt 919136,DL 9664,RAJ 919143,KOL 9888,PUN Input File: (4 Replies)
Discussion started by: siramitsharma
4 Replies

5. Shell Programming and Scripting

Help with replace specific column command

Input file: ASD_QAW 12 A_@ AE_AQ 21 PA_123 ASDA_@ 23 ADA_AS . . Output file: ASD_QAW 12 A @ AE_AQ 21 PA 123 ASDA_@ 23 ADA AS . . Do anybody know how to just specific and replace "_" in column 3 with tab delimiter (\t)? Thanks for advice. (2 Replies)
Discussion started by: perl_beginner
2 Replies

6. Shell Programming and Scripting

Selective Replace awk column values

Hi, I have the following data: 2860377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEGATIVE" 32340377|"DATA1"|"DATA2"|"65343"|"DATA2"|"DATA4"|"11"|"DATA5"|"DATA6"|"65343"|"DATA7"|"0"|"8"|"1"|"NEG-DID"... (3 Replies)
Discussion started by: sdohn
3 Replies

7. Shell Programming and Scripting

awk compare column n replace with in one file

hi Friends need to compare columns in one file where the data looks like below laptop,IBM phone,samsung car,rental user1,laptop user2,laptop user3,phone want to get output as laptop,IBM phone,samsung car,rental user1,IBM user2,IBM user3,samsung need to seach $2 in array of $1 and... (4 Replies)
Discussion started by: arun1401
4 Replies

8. Shell Programming and Scripting

awk command to print only selected rows in a particular column specified by column name

Dear All, I have a data file input.csv like below. (Only five column shown here for example.) Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 3,2,4,5,6 5,3,5,5,6 From this I want the below output Data1,StepNo,Data2,Data3,Data4 2,1,3,4,5 3,1,5,6,7 where the second column... (4 Replies)
Discussion started by: ks_reddy
4 Replies

9. Shell Programming and Scripting

awk to replace part of a column

dear all, I'm trying to use Awk to eliminate the last two characters from the first column in a file. This two characters are "-1" and I need to eliminate them from each row that I have in the files. The files have two columns and look like: ID_090-1 2 ID_3787-1 4 ID_0098-1 1 ID_12-1 4 I... (4 Replies)
Discussion started by: gabrysfe
4 Replies

10. Shell Programming and Scripting

awk/sed column replace using column header - help

$ cat log.txt Name Age Sex Lcation nfld alias xsd CC 25 M XYZ asx KK Y BB 21 F XAS awe SS N SD 21 M AQW rty SD A How can I replace the column with header "Lcation" with the column with header "alias" and delete the "alias" column? so that the final output will become: Name Age Sex... (10 Replies)
Discussion started by: jkl_jkl
10 Replies
Login or Register to Ask a Question