awk: find and replace in certain field only, help needed


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk: find and replace in certain field only, help needed
# 1  
Old 08-31-2007
awk: find and replace in certain field only, help needed

I got a sample file like this.

$ cat test
12|13|100|s
12|13|100|s
100|13|100|s
12|13|100|s

I want to replace all 100 by 2000 only in 3rd field using "awk"

This is replacing all 100's :-(
$ awk -F "|" '{gsub( /100/,"2000");print}' test
12|13|2000|s
12|13|2000|s
2000|13|2000|s
12|13|2000|s

I tried using something like this also - '$3 ~ /100/
but no luck :-(

Please help.

HTH,
jkl_jkl
# 2  
Old 08-31-2007
use sed instead.

Code:
/home/kamitsin>sed 's/|100/|2000/g' s2

HTML Code:
12|13|2000|s
12|13|2000|s
100|13|2000|s
12|13|2000|s
Cheers,
K
kamitsin
# 3  
Old 08-31-2007
Try this:

Code:
awk -F "|" '{ if ( $3 == 100 ) print $1"|"$2"|"2000"|"$4 }' test

# 4  
Old 08-31-2007
Code:
awk 'BEGIN{OFS=FS="|"}$3==100{$3=2000}{print}' file

# 5  
Old 08-31-2007
Quote:
Originally Posted by ghostdog74
Code:
awk 'BEGIN{OFS=FS="|"}$3==100{$3=2000}{print}' file

This is a nice reply(more generic) , worked for me :-) Thanks :-)
# 6  
Old 02-21-2008
Data Hurry !!! Its very urgent for me...

Hi All,

Need a quick help on the similar issue...I am trying to replace the 87th column ina file with some other value but this command doesn't work for me.

awk 'BEGIN{OFS=FS=";"}$87==10000019{$9=123456700}{print}' ab

Can someone help please...
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Find max length of the field and then replace zero

hai guys, pick the 1st field and calculate max length. if suppose max length is 2, then compare the all records if <2 then add zero's prefix of the record. for ex: s.no,sname 1,djud 37,jtuhe in this max length of the 1st field is 2 right the output wil be s.no,sname 01,djud... (6 Replies)
Discussion started by: Suneelbabu.etl
6 Replies

2. Shell Programming and Scripting

Find a blank field and replace values to NA

Hi All, i have a file like col1 col2 col3 13 24 NA 12 13 14 11 12 13 14 22 NA 18 26 NA in this file if i found "NA" other values in the line are also replace by NA Could you help me! (7 Replies)
Discussion started by: Shenbaga.d
7 Replies

3. Shell Programming and Scripting

To find char field and replace null

hi, i having a file with | seperated in which i need to search char in 3rd column and replace with null. i need to replace only the coulmn where character occurs in 3rd field for eg: file1.txt xx|yy|xx|12 output file: xx|yy||12 (5 Replies)
Discussion started by: rohit_shinez
5 Replies

4. Shell Programming and Scripting

Find field count and replace

Hello All, I have a file with contents like apple|ball|charlie|David| England|France|Germany| Ireland|Japan|King|London| Man|Nancy|Orange| here the column delimiter is | so if any of the lines/rows in the file has 3 only records (last field is empty), i want to place a | at the end of... (4 Replies)
Discussion started by: vinredmac
4 Replies

5. Shell Programming and Scripting

Find and replace blank in the last field

Hi all, I have a huge file and I need to get ride of the fields 6-11 and replace the blanks in field 5 with a missing value(99999). 159,93848,5354,343,67898,45,677,5443,434,5545,45 677,45545,3522,244, 554,54344,3342,456, 344,43443,2344,444,23477... (12 Replies)
Discussion started by: GoldenFire
12 Replies

6. UNIX for Advanced & Expert Users

Assistance Needed With Find/Replace in Vi

Hello All I have always had a question about find and replace in Vi. As this uses Vi, sed, and RegEx I never knew how or where to post the question but I thought I would give it a shot here. Say I have a text file filled with the following: Sue, your IP address is 192.168.1.10 which is... (4 Replies)
Discussion started by: NoSalt
4 Replies

7. UNIX for Dummies Questions & Answers

Awk help needed for display particular field alone for searching pattern

Hi, I have a requirement for taking an particular number in a log file. if i grep for the particular string it will retrieve the entire line for the particular string. but i want to display only the string from each line which i am searching for, Note: The searching field varies its position... (3 Replies)
Discussion started by: senthilkumar_ak
3 Replies

8. UNIX for Dummies Questions & Answers

Find and replace a field in the last line

I have a file 'test.out' with contents: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| 4|1|10|10|I|hgfj| 34|0|10|10|I|sdg| I want to modify the fifth column with value 'I' to 'A' for only the last line. Below is what I expect to see: 1|1|10|10|I|asdf| 2|1|10|10|I|sdfg| ... (3 Replies)
Discussion started by: ChicagoBlues
3 Replies

9. Shell Programming and Scripting

find pattern and replace another field

HI all I have a problem, I need to replace a field in a file, but only in the lines that have some pattern, example: 100099C01101C00000000059394200701CREoperadora_TX 100099C01201C00000000000099786137OPERADORA_TX2 in the example above I need to change the first field from 1 to 2 only if... (3 Replies)
Discussion started by: sergiioo
3 Replies

10. UNIX for Dummies Questions & Answers

Find and Replace code help needed

I have a file "dbshot.xml" that contains lines that need replacing in a batch format but the parameters are based on two lines. Ex. <role roletype="01"> <status>1 needs to be changed to <role roletype="01"> <status>0 I can't use simply "<status>1" replace since the... (2 Replies)
Discussion started by: E Orgill
2 Replies
Login or Register to Ask a Question