The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Bulgarian Greek Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-05-2005
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
Exclamation AWK or SED

Hi
Can some tell to use AWK or SED in the following situtation

file A has 10 columns, whenever column 10 has valuues AorBorCorDorE replace the column value with X else if it has GorH replace the column value with Y.

I'm not too user which one to use, whether mix of both of anone wil do ?

Thanks in advance

BrainDrain
  #2 (permalink)  
Old 12-05-2005
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
Hi,
I was just trying the following, however the value i'm trying to replace($7) with 'Z' for the occurences of 'A' retains it's orginal value. Am I doing something stupid ?

awk -F"~" '{if ($7=='A') $7='Z' ; print $1"~"$7; }' cif_in.tsv
  #3 (permalink)  
Old 12-05-2005
linuxpenguin's Avatar
linuxpenguin linuxpenguin is offline Forum Advisor  
Registered User
  
 

Join Date: May 2002
Location: India
Posts: 295
I guess what you need to use is $7 ~ /A/
here the ~ is used to match the regular expression /A/

awk -F"~" '{
if($7 ~ /A/) {
print $1"~"$2"~"$3"~"$4"~Z~"$6"~"$7
}
else {
print $0
}
}' cif_in.tsv
  #4 (permalink)  
Old 12-05-2005
braindrain braindrain is offline
Registered User
  
 

Join Date: Jul 2005
Posts: 67
Question

Hi,
Thanks for the reply, Well i'm able to evaluvate
if ($7=='A') in AWK, however assignment was not happening. May be i should try printing directly the value as you mentioned, instead of assignment. I thought there must be much neat way of doing this in awk.
  #5 (permalink)  
Old 12-05-2005
Perderabo's Avatar
Perderabo Perderabo is offline Forum Staff  
Unix Daemon
  
 

Join Date: Aug 2001
Location: Ashburn, Virginia
Posts: 9,131
Try this
Code:
$ cat adata
aa bb cc dd ee ff gg hh ii jj
qq ww ee rr tt yy uu ii oo pp
aa ss dd ff gg hh jj kk ll lo
$ awk ' { if ($7 == "uu") $7="zz"; print $0 } ' adata
aa bb cc dd ee ff gg hh ii jj
qq ww ee rr tt yy zz ii oo pp
aa ss dd ff gg hh jj kk ll lo
$

  #6 (permalink)  
Old 12-06-2005
criglerj's Avatar
criglerj criglerj is offline
Registered User
  
 

Join Date: May 2002
Location: Atlanta
Posts: 129
A slight refinement to Perderabo's solution. This is a bit more awk-like.

Code:
awk '$10 == "uu" { $10 = "@@" } { print }' adata

This can be generalized across multiple lines like this (in a bourne-like shell):

Code:
awk '$10 == "uu" || $10 == "99" || $10 == "**" { $10 = "zz" }
$10 == "aa" || $10 == "bb" { $10 = "yy" }
{ print }' adata

This checks field 10 for the strings ("||" means logical OR) and replaces it via the assignment in braces. The final line prints all lines after replacement has been done (if it was needed).

If you're using a C-shell-like shell, I'm told (but cannot confirm) that a backslash at the ends of the first two lines in apostrophes is needed.
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 11:35 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0