Unix/Linux Go Back    


Shell Programming and Scripting 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

Shell Programming and Scripting


Tags
column, condition, find field, replace

Closed    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 05-04-2011   -   Original Discussion by jghi123
jghi123's Unix or Linux Image
jghi123 jghi123 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 20 May 2011, 4:36 PM EDT
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
    #2  
Old Unix and Linux 05-04-2011   -   Original Discussion by jghi123
joeyg's Unix or Linux Image
joeyg joeyg is offline Forum Staff  
modérateur
 
Join Date: Dec 2007
Last Activity: 23 January 2018, 3:00 PM EST
Location: Within two miles of a Dunkin donuts.
Posts: 2,474
Thanks: 137
Thanked 204 Times in 179 Posts
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)
Sponsored Links
    #3  
Old Unix and Linux 05-04-2011   -   Original Discussion by jghi123
jghi123's Unix or Linux Image
jghi123 jghi123 is offline
Registered User
 
Join Date: Apr 2011
Last Activity: 20 May 2011, 4:36 PM EDT
Posts: 6
Thanks: 2
Thanked 0 Times in 0 Posts
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
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Update a field in a file based on condition kichu Shell Programming and Scripting 1 12-07-2010 01:57 PM
awk - Summing a field based on another field treesloth UNIX for Dummies Questions & Answers 7 06-21-2009 07:04 PM
Find top N values for field X based on field Y's value FrancoisCN Shell Programming and Scripting 1 05-29-2009 10:57 AM
finding difficult to check a condition megh Solaris 1 11-07-2008 09:48 AM
need help with replacing a certain field... shennanigan83 Shell Programming and Scripting 5 04-09-2008 02:00 AM



All times are GMT -4. The time now is 10:06 PM.