search a pattern across all file and replace it to all files.


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers search a pattern across all file and replace it to all files.
# 1  
Old 06-17-2011
search a pattern across all file and replace it to all files.

Hi All,

HP-UX oradev3 B.11.11 U 9000/800

I have few files, which contain texts. Now I need to search a pattern across all files and replace to that pattern with new one. For example, I have following files they have, which contain something like abc@abc.com, now I need to search text acorss all files and replace it something like def@def.com

Code:
pscc_status.sh
infaprd1_status.sh
dgtst.sh
dgwk.sh
test1.sh
time2.sh

hare krishna
# 2  
Old 06-17-2011
Code:
# Create a backup.
# Essential when batch-editing lots of files I believe.  If something
# goes horribly wrong you could lose ALL your scripts otherwise.
tar -cpf /path/to/backup.tar *.sh

for FILE in *.sh
do
        # sed can't search/replace in place, you have to create a new file and
        # overwrite the old.
        sed "s#user@wherever#user@whenever#g" < "$FILE" >/tmp/$$
        # cat is better than mv or copy here, because it won't alter the original
        # file's permissions.
        cat /tmp/$$ > "$FILE"
done

rm -f /tmp/$$

# 3  
Old 06-18-2011
Thank a lot for your inputs. Your solution worked like a charm. Also, I would reallr appriciate, if you can explan the sed part line ;


Code:
for FILE in *.sh
do
        # sed can't search/replace in place, you have to create a new file and
        # overwrite the old.
        sed "s#user@wherever#user@whenever#g" < "$FILE" >/tmp/$$
        # cat is better than mv or copy here, because it won't alter the original
        # file's permissions.
        cat /tmp/$$ > "$FILE"
done

# 4  
Old 06-18-2011
Code:
s - substitute
# - chosen seperator for s (most often is '/' in other examples) 
"user@wherever" - string to be matched
"user@whenever" - string to replace matched string
g - global change
< "$FILE" - take input from $FILE
>/tmp/$$ - output to a file named like pid of running script ($$)

This User Gave Thanks to Peasant For This Post:
# 5  
Old 06-18-2011
Thanks for explain this. I would appriciate, if you could more light on below line

Code:
>/tmp/$$ - output to a file named like pid of running script ($$)

# 6  
Old 06-18-2011
Quote:
Originally Posted by alok.behria
Thanks for explain this. I would appriciate, if you could more light on below line
It's just a temporary, random-named file. $$ is a special variable that returns the PID of the current shell. That way the file name isn't the same all the time, and isn't likely to bump against other things in /tmp/.

A better way to do it is FILENAME=`mktemp`, which would give truly random names, but not all systems have mktemp.
# 7  
Old 06-19-2011
Thank you very much!. I now understood..

hare krishna

---------- Post updated at 03:57 AM ---------- Previous update was at 03:56 AM ----------

Thanks man!

hare krishna
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Post Here to Contact Site Administrators and Moderators

Search for a pattern and replace a space at specific position with a Character in File

In file, we have millions of records each of 1000 in length. And at specific position say 800 there is a space, we need to replace it with Character X if the ID in that row starts with 123. So far i have used the below which is replacing space at that position to X but its not checking for... (3 Replies)
Discussion started by: Jagmeet Singh
3 Replies

2. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

I have a environment property file which contains: Input file: value1 = url1 value2 = url2 value3 = url3 and so on. I need to search all *.xml files under directory for value1 and replace it with url1. Same thing I have to do for all values mentioned in input file. I need script in unix bash... (7 Replies)
Discussion started by: Shamkamde
7 Replies

3. Shell Programming and Scripting

Mutli line pattern search & replace in a xml file

Hello guys, I need your help for a specific sed command that would search for a multi line pattern and if found, would replace it by another multi line pattern. For instance, here is the input: <RefNickName>abcd</RefNickName> <NickName>efgh</NickName> <Customize> ... (0 Replies)
Discussion started by: xciteddd
0 Replies

4. Shell Programming and Scripting

Help needed :Search and Replace a string pattern with empty in an xml file in unix

Search and Replace a string pattern with empty in an xml file in unix: My xml file would be like this : <Accounts><Name>Harish</Name><mobile>90844444444444445999 </mobile><TRIG>srcujim-1</TRIG></Accounts><Accounts><Name>Satish</Name><mobile>908999</mobile><TRIG>ettertrtt-1</TRIG></Accounts> ... (1 Reply)
Discussion started by: harish_s_ampeo
1 Replies

5. Shell Programming and Scripting

Search & Replace in Multiple Files by reading a input file

Hi, I have a folder which contains multiple config.xml files and one input file, Please see the below format. Config Files format looks like :- Code: <application name="SAMPLE-ARCHIVE"> <NVPairs name="Global Variables"> <NameValuePair> ... (0 Replies)
Discussion started by: haiksuresh
0 Replies

6. Shell Programming and Scripting

search the pattern in a file and replace with variable already defined previously in csh

I want to replace a certain pattern with the variable already defined. e.g. set path_verilog = /home/priya/bin/verilogfile my file contents are : verilog new verilog is defined here verilog_path_comes I am using the below command sed 's/verilog_path_comes/'$path_verilog'/g' <filename>... (2 Replies)
Discussion started by: nehashine
2 Replies

7. Shell Programming and Scripting

perl:: search for tow pattern and replace the third pattern

Hi i want to search two pattern on same line and replace onther pattern.. INPut file aaaa bbbbb nnnnnn ttttt cccc bbbbb nnnnnn ppppp dddd ccccc nnnnnn ttttt ffff bbbbb oooooo ttttt now i want replace this matrix like.. i want search for "bbbbb","nnnnnn" and search and replace for... (4 Replies)
Discussion started by: nitindreamz
4 Replies

8. Shell Programming and Scripting

SED Search Pattern and Replace with the Pattern

Hello All, I have a string "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031", and I just want to extract LLSV1, but I dont get the expected result when using the sed command below. # echo "CP_STATUS OSSRC_R6_0_Shipment_R1H_CU AOM_901046 R1H_LLSV1_2008031" | awk '{print... (4 Replies)
Discussion started by: racbern
4 Replies

9. Shell Programming and Scripting

Search for a Pattern and Replace

Hello All, Can you help me with this , I need to search a pattern replace it with the new pattern in all the files in a directory. what would be the easiest way to do that? Thanks in advance. :) Sam, (6 Replies)
Discussion started by: sbasetty
6 Replies
Login or Register to Ask a Question