The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

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
Perl: Search for string on line then search and replace text Crypto Shell Programming and Scripting 4 01-04-2008 10:24 AM
sed search and replace d__browne UNIX for Dummies Questions & Answers 7 04-26-2006 09:46 AM
how to search for a str and replace it.. sekar sundaram Shell Programming and Scripting 6 12-29-2005 06:11 PM
Search and replace sed or tr bridgeje Shell Programming and Scripting 6 10-28-2003 07:54 AM
search and Replace mukeshannamalai UNIX for Advanced & Expert Users 4 09-14-2001 06:21 AM

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

Join Date: Sep 2005
Posts: 19
Search and Replace in Ksh

All

I need a code in Ksh to search and replace a string in a file.

File A
---
AAAA A
BBBB B
CCCC C
DDDD E
FFFF F


File B:
--------
AAAA
BBBB
CCCC
DDDD


After running the script. I need the File B to be in following format

A
B
C
D

I use grep command to get corresponding value from file A

Then I tried to replace those values in file B

echo "s/$value1/$value2/\nwq!" | ex -s $FileB

I used a while loop and converted one by one.

But I got the following result

File B:

AAAA
BBBB
CCCC
D

Please help to solve this situation.

Thanks in advance

Deepak
  #2 (permalink)  
Old 05-23-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
You can do something like that:
Code:
awk '
   NR==FNR { rep[$1] = $ 2; next; }
   { if ($1 in rep) $1 = rep[$1]; print; }
    ' fileA fileB > fileB.tmp
mv fileB.tmp fileB
Jean-Pierre.
  #3 (permalink)  
Old 05-23-2007
Shell_Life's Avatar
Shell_Life Shell_Life is offline
Registered User
  
 

Join Date: Mar 2007
Location: Bahia, Brazil
Posts: 695
Xavier,
See if this works for you:
Code:
sed -e 's;^;s/;' -e 's; ;/;' -e 's;$;/;' file_A > $$sedTemp
sed -f $$sedTemp file_B
rm -f $$sedTemp
  #4 (permalink)  
Old 05-23-2007
Abhishek Ghose Abhishek Ghose is offline
Registered User
  
 

Join Date: Sep 2005
Location: Chennai
Posts: 81
Hi,

I had a similar requirement sometime back. Check up the 'join' command in UNIX. The idea is to join (join as in a table-join in relational databases) the files using the first column and extracting the last column of the second file. Unfortunately, I dont have a UNIx box right now, and cant give you a concrete example.


Thanks,
Abhishek Ghose
  #5 (permalink)  
Old 05-24-2007
anbu23 anbu23 is offline Forum Advisor  
Registered User
  
 

Join Date: Mar 2006
Location: Bangalore,India
Posts: 1,398
Code:
join -1 1 -2 1 -o 1.2 filea fileb
  #6 (permalink)  
Old 05-24-2007
lorcan lorcan is offline
Registered User
  
 

Join Date: May 2007
Posts: 219
Wont this work

Code:
join -o 1.2 filea fileb
  #7 (permalink)  
Old 05-24-2007
aigles's Avatar
aigles aigles is offline Forum Advisor  
Registered User
  
 

Join Date: Apr 2004
Location: Bordeaux, France
Posts: 1,414
There are two limitations with join :
  • The two files must be sorted.
  • Lines from fileB without correspondance in fileA aren't displayed.

Code:
$ cat fileA
AAAA A
BBBB B
CCCC C
DDDD E
FFFF F
$ cat fileB
BBBB
UUUU
CCCC
AAAA
DDDD
$ join -1 1 -2 1 -o 1.2 fileA fileB
B
$ join -o 1.2 fileA fileB         
B
$
Jean-Pierre.
Sponsored Links
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 07:55 PM.


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