Find and replace with variable using sed or awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find and replace with variable using sed or awk
# 1  
Old 07-16-2010
Find and replace with variable using sed or awk

hi,

i have file say email.temp looks like
Bell_BB 17
Bell_MONTHLY 888
SOLO_UNBEATABLE 721

and another file r3

Bell BB,Bell_BB
Bell,Bell_MONTHLY
SOLO,SOLO_UNBEATABLE

i want email.temp files $1 say Bell_BB should be replaced by r3 Bell BB and Bell_MONTHLY by Bell of r3 and so on ..

desired output of email.temp

Bell BB 17
Bell 888
SOLO 721

i tried it with sed

Code:
rk_file=$(echo "r3")
while read all; do

   find=$(echo $all| awk -F"," '{print $2}' )
   rep=$(echo $all| awk -F"," '{print $1}' )
   echo "$find and $rep \n" 
   sed "s/$find/$rep/g" email.temp > email.file
   
  done < $rk_file


but its not giving proper result only last field is SOLO is working

let me wats wrong with sed i am doing or how can i achive this using awk ?
# 2  
Old 07-16-2010
Code:
#!/bin/bash

while read LINE
do
  OLD=$( echo $LINE | cut -d, -f2 )
  NEW=$( echo $LINE | cut -d, -f1 )
  echo "$OLD <> $NEW"
  sed -e "s/^$OLD/$NEW/" -i mail_temp
done < replacements

exit 0
#finis

Code:
[house@leonov] cat replacements
Bell BB,Bell_BB
Bell,Bell_MONTHLY
SOLO,SOLO_UNBEATABLE
[house@leonov] cat mail_temp
Bell_BB 17
Bell_MONTHLY 888
SOLO_UNBEATABLE 721
[house@leonov] bash code.bash
Bell_BB <> Bell BB
Bell_MONTHLY <> Bell
SOLO_UNBEATABLE <> SOLO
[house@leonov] cat mail_temp
Bell BB 17
Bell 888
SOLO 721

# 3  
Old 07-16-2010
i am using ksh it is fialing with invalid option

Code:
sed: illegal option -- i



---------- Post updated at 05:44 AM ---------- Previous update was at 04:42 AM ----------

i achived it using

Code:
while read all; do

   find=$(echo $all| awk -F"," '{print $2}' )
   rep=$(echo $all| awk -F"," '{print $1}' )
   echo "$find and $rep \n" 
   sed -e "s/^$find/$rep/g" email.temp | grep "$rep" | grep -v "_" >> email.file
   
  done < r3


Last edited by radoulov; 07-16-2010 at 07:46 AM.. Reason: Please use code tags!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Awk/sed to replace variable in file

Hi All I have one file with multiple lines in it, each line has static text and some variable enclosed in <<filename>> as well. e.g. as below 123, <<file1.txt>> this is my name, I stay at <<city.txt>> Thanks for visiting 348384y, this is my name <<fileabc.txt>>, I stay at near the mall of... (8 Replies)
Discussion started by: reldb
8 Replies

2. UNIX for Dummies Questions & Answers

Sed/awk to find negative numbers and replace with 1?

Greetings. I have a three column file, and there are some numbers in the second column that are <1. However I need all numbers to be positive, thus need to replace all those numbers with just one. I feel like there must be a simple way to use awk to find these numbers and sed to replace but can't... (5 Replies)
Discussion started by: Twinklefingers
5 Replies

3. Shell Programming and Scripting

sed and awk -Find and Replace

All, I have thousands of lines in a file with following format DATA=_ONE_XXX_YYY_CCC_HHHG_ DATA1=_GGG_JJJJ_HHH_UUU_JJJJ_HHHH_LLL_ DATA3=_MMM_GG_NN_QQQQ_FFF_III_ I want to replace _ with . by ignoring the first (=_) and last (_) So that out put should looks like... (4 Replies)
Discussion started by: baluchen
4 Replies

4. Shell Programming and Scripting

find and replace with variable -sed

Hi, I have a ksh script where I am trying to mask the password in the log files. $loc - is my directory $PGUIDE_DB_USER_PSW - is a variable that holds the password I am looking for find $loc/logs -type f -exec sed -i "s/$PGUIDE_DB_USER_PSW/*****/"g {} \; I get an error: ... (2 Replies)
Discussion started by: amitlib
2 Replies

5. Red Hat

How to pass value of pwd as variable in SED to replace variable in a script file

Hi all, Hereby wish to have your advise for below: Main concept is I intend to get current directory of my script file. This script file will be copied to /etc/init.d. A string in this copy will be replaced with current directory value. Below is original script file: ... (6 Replies)
Discussion started by: cielle
6 Replies

6. Shell Programming and Scripting

How to find a certain string in a file and replace it with a value from another file using sed/awk?

Hi Everyone, I am new to this forum and new to sed/awk programming too !! I need to find particular string in file1(text file) and replace it with a value from another text file(file2) the file2 has only one line and the value to be replaced with is in the second column. file 1: (assert (=... (21 Replies)
Discussion started by: paramad
21 Replies

7. Shell Programming and Scripting

Find and replace a column that has '' to NULL in a comma delimited using awk or sed

Hi this is my first time posting ever. I'm relatively new in using AWK/SED, I've been trying many a solution. I'm trying to replace the 59th column in a file where if I encounter '' then I would like to replace it with the word NULL. example 0 , '' , '' , 0 , 195.538462 change it to 0... (5 Replies)
Discussion started by: gumal901
5 Replies

8. Shell Programming and Scripting

[sed/awk] find info and replace

Hi :) I have some problems with "FOR"... I have a text file in this format: name1 www.link1/random_number name2 www.link2/random_number name3 www.link3/random_number ... (Names and info changes) Now, I need: (4 Replies)
Discussion started by: aspire
4 Replies

9. Shell Programming and Scripting

find and replace variable

Is there an easy way of doing this cat file1 jkasjhjgfg LTRIM(RTRIM(aa_bb_cde)) aragsfdg LTRIM(RTRIM(aa_bb_cde)) aregfafdgfg sdgsfdagdfg gadfg eafgsadgsa asdgsfdgag LTRIM(RTRIM(aa_bb_cde)) rfghsdfhd I want to replace each occurence of LTRIM(RTRIM($x)) with LENGTH(LTRIM(RTRIM($x)))=0... (4 Replies)
Discussion started by: alfredo123
4 Replies

10. Shell Programming and Scripting

Find and replace the value in a variable

I have a variable whose value is I="user1:x:1100:1200:ID for user1:/home/user1:/bin/ssh-dummy-shell" I want to replace the last part '/bin/ssh-dummy-shell' with '/bin/true' I tried using sed but it garbled sed 's/\/bin\/ssh-dummy-shell/\/bin\/true' $I Thanks for the help (5 Replies)
Discussion started by: aajmani
5 Replies
Login or Register to Ask a Question