Help with Awk finding and replacing a field based on a condition


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with Awk finding and replacing a field based on a condition
# 1  
Old 05-04-2011
Help with Awk finding and replacing a field based on a condition

Hi everybody,

I'm trying to replace the $98 field with "T" if the last field (108th) is T

I've tried
Code:
 awk 'BEGIN{OFS=FS="|"} {if ($108=="T")sub($98,"T"); print}' test.txt

but that doesn't do anything

also tried
Code:
  awk 'BEGIN{OFS=FS="|"}{ /*T.$/ sub($98,"T")} { print}'  test.txt

but that doesn't seem to do anything either

---------- Post updated at 02:01 PM ---------- Previous update was at 01:52 PM ----------

I've also tried the below and that doesn't work either

Code:
awk -F"|" '/T.$/ sub( $98,"T")' test.txt


Last edited by jghi123; 05-04-2011 at 03:01 PM.. Reason: wrong character
# 2  
Old 05-04-2011
Unsure of your problem

Your first command seems to have correct syntax.

Code:
$ cat sample5.txt
A|T|X|G
G|X|T|A
G|T|A|X

$ awk 'BEGIN {OFS=FS="|"} {if ($2=="T")sub($1,"T"); print }' sample5.txt
T|T|X|G
G|X|T|A
T|T|A|X

This User Gave Thanks to joeyg For This Post:
# 3  
Old 05-04-2011
thanks =)

data integrity issue

column $108 acutally had a space after the T

---------- Post updated at 02:22 PM ---------- Previous update was at 02:21 PM ----------

Code:
 awk 'BEGIN{OFS=FS="|"} /\|T.$/{$98="T"}1'

works
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Problem with getting awk to multiply a field by a value set based on condition of another field

Hi, So awk is driving me crazy on this one. I have searched everywhere and read man, docs and every related post Google can find and still no luck. The actual files I need to run this on are sensitive in nature, but it is the same thing as if I needed to calculate weighted grades for multiple... (15 Replies)
Discussion started by: cotilloe
15 Replies

2. UNIX for Beginners Questions & Answers

Change the field color based on condition in email

Request your help to change the field color based on condition , if it is otherthan 0. using html in unix. Here is my condition for(i=1;i<=NF;i++) { print "<td> "$i"</td> } Please use CODE tags when displaying sample input, output, and code segments. (17 Replies)
Discussion started by: CatchMe
17 Replies

3. Shell Programming and Scripting

awk to change contents of field based on condition in same file

In the awk below I am trying to copy the entire contents of $6 there may be multiple values seperated by a ;, to $8, if $8 is . (lines 1 and 3 are examples). If that condition $8 is not . (line2 is an example) then that line is skipped and printed as is. The awk does execute but prints the output... (3 Replies)
Discussion started by: cmccabe
3 Replies

4. Shell Programming and Scripting

Replacing field based on the value of other field

Hi Team, In the below input file, if I have the value 23,24,25 then for those records 1st field value should get updated from "a" to "b". I also want to pass these values in file as input as it can be done dynamically. Tried awk commands but not getting desired output.Using SunOS 5.10 version.... (14 Replies)
Discussion started by: weknowd
14 Replies

5. UNIX for Beginners Questions & Answers

Replacing tag based on condition

Hi All, I am having a file like below. The file will having information about the records.If you see the file the file is header and data. For example it have 1 men tag and the tag id will be come after headers. The change is I want to convert All pets tag from P to X. I did a sed like below... (5 Replies)
Discussion started by: arunkumar_mca
5 Replies

6. Shell Programming and Scripting

awk to adjust coordinates in field based on sequential numbers in another field

I am trying to output a tab-delimited result that uses the data from a tab-delimited file to combine and subtract specific lines. If $4 matches in each line then the first matching sequential $6 value is added to $2, unless the value is 1, then the original $2 is used (like in the case of line... (3 Replies)
Discussion started by: cmccabe
3 Replies

7. Shell Programming and Scripting

awk to update value in field based on another field

In the tab-delimeted input file below I am trying to use awk to update the value in $2 if TYPE=ins in bold, by adding the value of HRUN= in italics. In the below since in line 1 TYPE=ins the 117282541 value in $2 has 6 added because that is the value of HRUN=. Hopefully the awk is a start but I... (2 Replies)
Discussion started by: cmccabe
2 Replies

8. Shell Programming and Scripting

Finding/replacing strings in some files based on a file

Hi, We have a file (e.g. a .csv file, but could be any other format), with 2 columns: the old value and the new value. We need to modify all the files within the current directory (including subdirectories), so find and replace the contents found in the first column within the file, with the... (9 Replies)
Discussion started by: Talkabout
9 Replies

9. Shell Programming and Scripting

Replacing the text in a row based on certain condition

Hi All, I felt tough to frame my question. Any way find my below input. (.CSV file) SNo, City 1, Chennai 2, None 3, Delhi 4,None Note that I have many rows ans also other columns beside my City column. What I need is the below output. SNo, City 1, Chennai 2, Chennai_new 3, Delhi... (2 Replies)
Discussion started by: ks_reddy
2 Replies

10. Shell Programming and Scripting

Update a field in a file based on condition

Hi i am new to scripting. i have a file file.dat with content as : CONTENT_STORAGE PERCENTAGE FLAG: /storage_01 64% 0 /storage_02 17% 1 I need to update the value of FLAG for a particular CONTENT_STORAGE value I have written the following code #!/bin/sh threshold=20... (1 Reply)
Discussion started by: kichu
1 Replies
Login or Register to Ask a Question