Search and Replace in Ksh


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Search and Replace in Ksh
# 1  
Old 05-23-2007
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  
Old 05-23-2007
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  
Old 05-23-2007
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  
Old 05-23-2007
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  
Old 05-24-2007
Code:
join -1 1 -2 1 -o 1.2 filea fileb

# 6  
Old 05-24-2007
Wont this work

Code:
join -o 1.2 filea fileb

# 7  
Old 05-24-2007
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

2. Shell Programming and Scripting

Replace in ksh

Dear All, I need you help, I have a file allfile_dump.txt \370380CoverPage124007001.pdf \370381CoverPage124007002.pdf \370382CoverPage124007003.pdf \370383CoverPage124007004.pdf \370384CoverPage124007005.pdf \370385CoverPage124007006.pdf \370386CoverPage124007007.pdf... (8 Replies)
Discussion started by: yadavricky
8 Replies

3. Shell Programming and Scripting

how to search array and print index in ksh

Hi, I am using KSH shell to do some programming. I want to search array and print index value of the array. Example.. nodeval4workflow="DESCRIPTION ="" ISENABLED ="YES" ISVALID ="YES" NAME="TESTVALIDATION" set -A strwfVar $nodeval4workflow strwfVar=DESCRIPTION=""... (1 Reply)
Discussion started by: tmalik79
1 Replies

4. Shell Programming and Scripting

Replace in KSH

Hello, i want to replace a string AAAA to BBB in a file using script. File: 1 2 AAAA 3 4 5 old=AAAA new=BBB cat $file_name | while read line; do if ;then sed -e 's/$old/$new/' fi done (5 Replies)
Discussion started by: LiorAmitai
5 Replies

5. Shell Programming and Scripting

Replace string in ksh

Hello, I want to locate a special character in each line of a file and replace it with another string that contains a special character and $i (i is incresing each cycle) string1: export IBAN=AAAAAAAAA . . . . export IBAN=zzzzzzzzzzz I want it to be: export IBAN=AAAAAAAAA . export... (1 Reply)
Discussion started by: LiorAmitai
1 Replies

6. Shell Programming and Scripting

perl search and replace - search in first line and replance in 2nd line

Dear All, i want to search particular string and want to replance next line value. following is the test file. search string is tmp,??? ,10:1 "???" may contain any 3 character it should remain the same and next line replace with ,10:50 tmp,123 --- if match tmp,??? then... (3 Replies)
Discussion started by: arvindng
3 Replies

7. Shell Programming and Scripting

Ksh: Replace backslash characters

Hi All, I have a requirement to read a line from a file with some search string, replace any backslash characters in that line and store in a variable. Shell script: replace.ksh #!/bin/bash file2=input.rtf line=`grep "Invoice Number" ${file2} | head -1 | sed 's/\\//g'` echo "start... (6 Replies)
Discussion started by: prashas_d
6 Replies

8. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

9. Shell Programming and Scripting

Find and Replace in ksh.

Hi Experts, I have a directory which have 500 ksh, they all have common path string. I have replaced all the ksh in different directory so i need to change the path string. Please help me how to search and replace it. Thanks SKG (1 Reply)
Discussion started by: gauravsunil
1 Replies

10. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies
Login or Register to Ask a Question