Please Help to Check script Search and Replace


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Please Help to Check script Search and Replace
# 1  
Old 04-29-2010
Please Help again to Check script Search and Replace with nawk

Please Help to Check script Search and Replace
Ex. Search 0001 and Replete un_0001


---script
Code:

Code:
nawk -F\" 'NR==FNR{a[$0];next}$2 in a{sub($2,"un_"$2)}1' input.txt file*.txt > resoult.txt



script is work to one result but
if i have file1.txt, file2.txt, file3.txt i want to Replace Result in old file
please help again
thank you

---file1.txt
Code:
Event: "0001","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "0003","0002","0001","0003"
Event: "0003","0002","0001","0003"

---file2.txt
Code:
Event: "0003","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "0001","0002","0001","0003"
Event: "0001","0002","0001","0003"

--- input.txt
Code:
0001
0003

---Result
---file1.txt
Code:
Event: "un_0001","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "un_0003","0002","0001","0003"
Event: "un_0003","0002","0001","0003"

---file2.txt
Code:
Event: "un_0003","0002","0001","0003"
Event: "0002","0002","0001","0003"
Event: "un_0001","0002","0001","0003"
Event: "un_0001","0002","0001","0003"


Last edited by kittiwas; 04-30-2010 at 01:05 AM.. Reason: Please Help again
# 2  
Old 04-29-2010
You want to repalce 0001 in the begging or anywhere. Please be specific

you can use sed or tr command to achive this. Otherwise you will write a perl program for the same
This User Gave Thanks to pritish.sas For This Post:
# 3  
Old 04-29-2010
Hi,

Try this...

Code:
#!/bin/sh

file_to_search=file.txt
original_file=$file_to_search

cp $original_file newfile
for da_b in $(cat /test/input.txt)
do
sed "s/Event: \"${da_b}\"/Event: \"un_${da_b}\"/g" $file_to_search > test_$da_b
rm $file_to_search
file_to_search=test_$da_b
done

cat $file_to_search
rm $file_to_search
cp newfile $original_file
rm newfile


Last edited by Scott; 04-29-2010 at 09:31 AM.. Reason: Fixed code tags
This User Gave Thanks to pravin27 For This Post:
# 4  
Old 04-29-2010
Code:
awk -F\" 'NR==FNR{a[$0];next}$2 in a{sub($2,"un_"$2)}1' input.txt file.txt

This User Gave Thanks to danmero For This Post:
# 5  
Old 04-30-2010
thank you For Script Smilie pravin27 and danmero

Code:
nawk -F\" 'NR==FNR{a[$0];next}$2 in a{sub($2,"un_"$2)}1' input.txt file*.txt > resoult.txt

if i have file1.txt, file2.txt, file3.txt
script is work to one result but i want to Replace Result in old file
please help again
thank you

---------- Post updated at 01:29 PM ---------- Previous update was at 09:02 AM ----------

thank you for all answer

---------- Post updated at 01:30 PM ---------- Previous update was at 01:29 PM ----------

thank for all answer

Last edited by kittiwas; 04-29-2010 at 11:58 PM..
# 6  
Old 04-30-2010
Quote:
Originally Posted by kittiwas
script is work to one result but i want to Replace Result in old file
Is not safe to edit-in-place, use a temporary file for output and move the temporary file over old file.
This User Gave Thanks to danmero For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Shell script for search and replace by field

Hi, I have an input file with below data and rules file to apply search and replace by each field in the input based on exact value or pattern. Could you please help me with unix script to read input file and rules file and then create the output and reject files based on the rules file. Input... (13 Replies)
Discussion started by: chandrath
13 Replies

2. Shell Programming and Scripting

Search and replace script

Hi, Below is the script which will find a particular text and replace with another one in a group of files under a directory /test #!/bin/bash old=$1 --- first input old text new=$2--- input new text cd /test --- folder into which files need to be checked for y in `ls *`; do sed... (2 Replies)
Discussion started by: chetansingh23
2 Replies

3. Shell Programming and Scripting

Script to search and replace

Hi All, I am trying to write a script which will find a particular text in certain group of files under a directory and if found correctly it will replace them with a new text in all the files. Could any one let me know how do i find the text in many files under a directory. Thanks (3 Replies)
Discussion started by: chetansingh23
3 Replies

4. Shell Programming and Scripting

Script Search replace - complicated

I have a text file for which i need a script which does some fancy search and replace. Basically i want to loop through each line, if i find an occurance of certain string format then i want to carry on search on replace another line, once i replaced this line i will contine to search for the... (7 Replies)
Discussion started by: kelseyh
7 Replies

5. Shell Programming and Scripting

Script to check a file and replace some of the contents

Hi I have a file that looks like this: Line 0 animal elephant Line 1 animal elephant Line 2 animal elephant Line 3 animal elephant What i am aiming to do is with a script and an input value of... (6 Replies)
Discussion started by: tara
6 Replies

6. UNIX for Dummies Questions & Answers

Unix script, sed search and replace?

Hi, I am trying to write a shell script designed to take input line by line by line from a file with a word on each line for editing with sed. Example file: 1.ejverything 2.bllown 3.maikling 4.manegement 5.existjing 6.systems My design currently takes input from the user, and... (2 Replies)
Discussion started by: mkfitzwilliams
2 Replies

7. Shell Programming and Scripting

Molecular biologist requires help re: search / replace script

Monday April 07, 2008 Hello - I was wondering if someone could help me? I have some basic knowledge of awk, etc., and can create simple scripts (e.g. a search_replace.awk file) that can be called from the command line: $ awk -f search_replace.awk <file to be searched> I have a... (11 Replies)
Discussion started by: gstuart
11 Replies

8. Shell Programming and Scripting

search & replace password perl script

I wanted a perl script to be done for Password search & replace in two files. For Example: Example 1)--i am having a file such as cat /opt/customer/Ariba/UAT/ariba/app/buyer/Server/config/Parameters.table Example 2)--and i am having a other file in other location such as cat... (4 Replies)
Discussion started by: shellscript22
4 Replies

9. UNIX for Dummies Questions & Answers

multiple input search and replace script

hi, i want to create a script that will search and replace the values inside a particular file. i have 5 files that i need to change some values inside and i don't want to use vi to edit these files. All the inputted values on the script below will be passed into the files. cho "" echo... (3 Replies)
Discussion started by: tungaw2004
3 Replies

10. Shell Programming and Scripting

search and replace dynamic data in a shell script

Hi, I have a file that looks something like this: ... 0,6,256,87,0,0,0,1187443420 0,6,438,37,0,0,0,1187443380 0,2,0,0,0,10,0,1197140320 0,3,0,0,0,10,0,1197140875 0,2,0,0,0,23,0,1197140332 0,3,0,0,0,23,0,1197140437 0,2,0,0,0,17,0,1197140447 0,3,0,0,0,17,0,1197140543... (8 Replies)
Discussion started by: csejl
8 Replies
Login or Register to Ask a Question