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.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
how to read the column and print the values under that column gemini106 Shell Programming and Scripting 6 03-28-2008 07:05 AM
How to check Null values in a file column by column if columns are Not NULLs Mandab Shell Programming and Scripting 7 03-15-2008 09:57 AM
Need help with switching field/column values sonyd8 Shell Programming and Scripting 7 02-13-2008 01:10 AM
replace a column values with the first value in column sumeet UNIX for Advanced & Expert Users 3 02-06-2007 01:13 PM
Replace 10th column with a new column--- Terriblly hurry ahmedwaseem2000 Shell Programming and Scripting 2 09-06-2005 02:10 AM

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 03-31-2008
charandevu charandevu is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 29
replace the column values.

I have the below file ...where some of the column values should replaced with desired values ....below file u can find that 3 column where ever 'AAA' comes should replaced with ' CC '

NOTE : we have to pass the column number ,AAA,CC (modified value) as the parameters to the code.

File 1 :

E100,A456,AAA,E12
E101,A22,BBB,E13
E101,A,AAA,E14


Result file :

E100,A456,CC,E12
E101,A22,BBB,E13
E101,A,CC,E14


Please help .....
  #2 (permalink)  
Old 03-31-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433

Code:
awk 'BEGIN { FS=OFS="," } $field==$from { $field=$to } 1' field=3 from=AAA to=CC inputfile


jean-Pierre.
  #3 (permalink)  
Old 04-01-2008
invinzin21 invinzin21 is offline
Registered User
  
 

Join Date: Dec 2007
Location: Philippines, Cebu City
Posts: 75
Hey, if the figures is constant you can use this

$ cat a
E100,A456,AAA,E12
E101,A22,BBB,E13
E101,A,AAA,E14

$ awk -F, '{ if($3 ~ "AAA") {print $1","$2",""CC"","$4} else {print $0} }' a
E100,A456,CC,E12
E101,A22,BBB,E13
E101,A,CC,E14

$
  #4 (permalink)  
Old 04-01-2008
charandevu charandevu is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 29
Thanks ...

Hi ....
It is working fine ....thanks .....
I will get back if any required in the above .......
  #5 (permalink)  
Old 04-01-2008
charandevu charandevu is offline
Registered User
  
 

Join Date: Mar 2008
Posts: 29
replacing multiple columns ???

If iam having multiple columns to be replaced ....does the above code works ... please provide the code for the below.....
Below i have to replace 3 rd and 5 th column to 'CC' & 'XXX' repectively .....

File1 :
E100,A456,AAA,E12,MMM
E101,A22,BBB,E13,MMM
E101,A,AAA,E14 ,MMM


Result :

E100,A456,CC,E12,XXX
E101,A22,BBB,E13,XXX
E101,A,CC,E14 ,XXX
  #6 (permalink)  
Old 04-01-2008
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,433

Code:
awk '
   BEGIN { FS=OFS="," }
   $3 == "AAA" { $3 = "CC" }
   $5 == "MMM" { $5 = "XXX" }
   1
    ' inputfile

Jean-Pierre.
  #7 (permalink)  
Old 04-02-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,099
nawk -v p=$1 -v old=$2 -v new=$3 'BEGIN{ OFS=FS=","}
{
for(i=1;i<=NF;i++)
{
if(p==i && $i==old)
{
$i=new
}
}
print $0
}' filename
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 12:50 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