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.

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

Tags
column, condition, find field, replace, shell scripts

Login to Reply

 
Thread Tools Search this Thread
# 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 02: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

The Following User Says Thank You to joeyg For This Useful Post:
jghi123 (05-04-2011)
# 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 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
awk to change contents of field based on condition in same file cmccabe Shell Programming and Scripting 3 01-29-2018 11:54 AM
Replacing field based on the value of other field weknowd Shell Programming and Scripting 14 01-12-2018 06:23 PM
Replacing tag based on condition arunkumar_mca UNIX for Beginners Questions & Answers 5 09-13-2017 08:14 AM
awk to adjust coordinates in field based on sequential numbers in another field cmccabe Shell Programming and Scripting 3 01-30-2017 07:39 AM
Condition based on Timestamp (Date/Time based) from logfile (Epoch seconds) asjaiswal UNIX for Dummies Questions & Answers 9 11-28-2013 05:11 PM
Finding/replacing strings in some files based on a file Talkabout Shell Programming and Scripting 9 07-14-2012 11:00 PM
Replacing the text in a row based on certain condition ks_reddy Shell Programming and Scripting 2 05-29-2012 08:25 AM
Finding and replacing whole line using sed anishkumarv Shell Programming and Scripting 2 03-20-2011 07:14 AM
Update a field in a file based on condition kichu Shell Programming and Scripting 1 12-07-2010 12:57 PM
Find top N values for field X based on field Y's value FrancoisCN Shell Programming and Scripting 1 05-29-2009 09:57 AM


All times are GMT -4. The time now is 01:43 PM.

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