Visit The New, Modern Unix Linux Community


Help needed with shell script to search and replace a set of strings among the set of files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed with shell script to search and replace a set of strings among the set of files
# 1  
Help needed with shell script to search and replace a set of strings among the set of files

Hi,

I am looking for a shell script which serves the below purpose.
Please find below the algorithm for the same and any help on this would be highly appreciated.

1)set of strings need to be replaced among set of files(directory may contain different types of files)
2)It should search for the type of files for which replace must be done

Please find below the algorithm for the same and any help on this would be highly appreciated.
Code:
######
#below mapped texts must be replaced
######
SEARCH_N_REPLACE={
abc = test1
efg = test2
gfdg = test3
}
######
#directory may contain pdf/txt/jpg files but it should only consider the #mentioned files here {html,htm,css,js}. 
#######

SEARCH_IN_FILES= {html,htm,css,js}


######

sync.log contains file paths as below:
Code:
./testing/file.pdf
./sample.jpg
./example.html
./testcondition.css

######

Code:
foreach line in sync.log #line means each file path 
foreach ext SEARCH_IN_FILES #should search for extension type of file
if line.ext is ext #if matches the extension
foreach term SEARCH_N_REPLACE
searchterm=parse:SEARCH_N_REPLACE
replaceterm=parse:SEARCH_N_REPLACE
perl -pie 's/searchterm/replaceterm'
perl -pie 's/searchterm/replaceterm' line

Thanks in advance!!!
Image

Last edited by Scrutinizer; 02-26-2014 at 07:21 AM.. Reason: code tags for pseudo code and data
# 2  
Code:
#!/bin/ksh
cat sync.log | while read line
do
F_EXT=${line##*.}
if [[ ${F_EXT} == "html" || ${F_EXT} == "htm" || ${F_EXT} == "css" || ${F_EXT} == "js" ]]; then
# sed -i 's/abc/test1/g;efg/test2/g;gfdg/test3/g' ${line}
perl -i -pe 's/abc/test1/g;' -pe 's/efg/test2/g;' -pe 's/gfdg/test3/g;' ${line}
fi
done

This User Gave Thanks to SriniShoo For This Post:
# 3  
Hi Srini,

Thanks for the script. As the data for the type of files and the replacements will be dynamic, could you please let me know how can i parse the search term and replace among the files as mentioned in the algorithm using for loops.

Thanks a lot for the help .

---------- Post updated at 07:33 AM ---------- Previous update was at 07:05 AM ----------

Quote:
Originally Posted by SriniShoo
Code:
#!/bin/ksh
cat sync.log | while read line
do
F_EXT=${line##*.}
if [[ ${F_EXT} == "html" || ${F_EXT} == "htm" || ${F_EXT} == "css" || ${F_EXT} == "js" ]]; then
# sed -i 's/abc/test1/g;efg/test2/g;gfdg/test3/g' ${line}
perl -i -pe 's/abc/test1/g;' -pe 's/efg/test2/g;' -pe 's/gfdg/test3/g;' ${line}
fi
done

---------- Post updated at 07:34 AM ---------- Previous update was at 07:33 AM ----------

Im using bash shell. scripting with .sh

---------- Post updated at 07:36 AM ---------- Previous update was at 07:34 AM ----------

Quote:
Originally Posted by SriniShoo
Code:
#!/bin/ksh
cat sync.log | while read line
do
F_EXT=${line##*.}
if [[ ${F_EXT} == "html" || ${F_EXT} == "htm" || ${F_EXT} == "css" || ${F_EXT} == "js" ]]; then
# sed -i 's/abc/test1/g;efg/test2/g;gfdg/test3/g' ${line}
perl -i -pe 's/abc/test1/g;' -pe 's/efg/test2/g;' -pe 's/gfdg/test3/g;' ${line}
fi
done

Im using bash shell. scripting with .sh

---------- Post updated at 07:50 AM ---------- Previous update was at 07:36 AM ----------

Quote:
Originally Posted by SriniShoo
Code:
#!/bin/ksh
cat sync.log | while read line
do
F_EXT=${line##*.}
if [[ ${F_EXT} == "html" || ${F_EXT} == "htm" || ${F_EXT} == "css" || ${F_EXT} == "js" ]]; then
# sed -i 's/abc/test1/g;efg/test2/g;gfdg/test3/g' ${line}
perl -i -pe 's/abc/test1/g;' -pe 's/efg/test2/g;' -pe 's/gfdg/test3/g;' ${line}
fi
done

# 4  
Try :

Code:
$ cat log
./testing/file.pdf
./sample.jpg
./example.html
./testcondition.css

Code:
$ echo 'gfdg' >example.html

Code:
$ cat example.html
gfdg

Code:
$ cat test
#!/bin/bash

SEARCH_IN_FILES="html htm css js"
declare -A MYMAP=( [abc]=test1 [efg]=test2 [gfdg]=test3 )

while read line;do

	if grep -w "${line##*.}" <<<$SEARCH_IN_FILES >/dev/null; then
		
		for i in "${!MYMAP[@]}";do
  			 sed -i 's/'"$i"'/'"${MYMAP[$i]}"'/g' $line
		done
	fi

done <"log"

Code:
$ bash test

Code:
$ cat example.html
test3

This User Gave Thanks to Akshay Hegde For This Post:
# 5  
Excellent! Thanks a lot. This is what i needed. Thanks Akshay for the neeful help.

Quote:
Originally Posted by Akshay Hegde
Try :

Code:
$ cat log
./testing/file.pdf
./sample.jpg
./example.html
./testcondition.css

Code:
$ echo 'gfdg' >example.html

Code:
$ cat example.html
gfdg

Code:
$ cat test
#!/bin/bash

SEARCH_IN_FILES="html htm css js"
declare -A MYMAP=( [abc]=test1 [efg]=test2 [gfdg]=test3 )

while read line;do

    if grep -w "${line##*.}" <<<$SEARCH_IN_FILES >/dev/null; then
        
        for i in "${!MYMAP[@]}";do
               sed -i 's/'"$i"'/'"${MYMAP[$i]}"'/g' $line
        done
    fi

done <"log"

Code:
$ bash test

Code:
$ cat example.html
test3

---------- Post updated 2014-02-27 at 12:49 AM ---------- Previous update was 2014-02-26 at 01:47 PM ----------

$ sh testsync.sh
testsync.sh: line 4: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]
# 6  
not sh its$ bash yourscriptname, which might work with sh also.

Last edited by Akshay Hegde; 02-27-2014 at 02:10 AM..
# 7  
getting same error

$ bash testsync
testsync: line 28: declare: -A: invalid option
declare: usage: declare [-afFirtx] [-p] [name[=value] ...]

Previous Thread | Next Thread
Thread Tools Search this Thread
Search this Thread:
Advanced Search

Test Your Knowledge in Computers #416
Difficulty: Medium
JavaScript supports event-driven, functional, and imperative (including object-oriented and prototype-based) programming styles.
True or False?

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search for a value and replace other field in the same set

Hello friends, I have huge file with many sets where each "set" has few lines and each set always begins with "Set" in Sq brackets as shown above. # cat file1 (2 Replies)
Discussion started by: magnus29
2 Replies

2. Shell Programming and Scripting

search and replace, when found, delete multiple lines, add new set of lines?

hey guys, I tried searching but most 'search and replace' questions are related to one liners. Say I have a file to be replaced that has the following: $ cat testing.txt TESTING AAA BBB CCC DDD EEE FFF GGG HHH ENDTESTING This is the input file: (3 Replies)
Discussion started by: DeuceLee
3 Replies

3. Shell Programming and Scripting

Finding compound words from a set of files from another set of files

Hi All, I am completely stuck here. I have a set of files (with names A.txt, B.txt until L.txt) which contain words like these: computer random access memory computer networking mouse terminal windows All the files from A.txt to L.txt have the same format i.e. complete words in... (2 Replies)
Discussion started by: shoaibjameel123
2 Replies

4. UNIX for Dummies Questions & Answers

Loop through Sub Directories and search for set of files

I have the below directory in unix environment /home/bkup/daily: ls -lrt drwxrwx--x 2 user user 256 Jan 12 18:21 20110112/ drwxrwx--x 2 user user 256 Jan 13 17:06 20110113/ drwxrwx--x 2 user user 256 Jan 14 16:44 20110114/ drwxrwx--x 2 user user ... (2 Replies)
Discussion started by: prasannarajesh
2 Replies

5. Shell Programming and Scripting

Complex Search/Replace Multiple Files Script Needed

I have a rather complicated search and replace I need to do among several dozen files and over a hundred occurrences. My site is written in PHP and throughout the old code, you will find things like die("Operation Aborted due to....."); For my new design skins for the site, I need to get... (2 Replies)
Discussion started by: UCCCC
2 Replies

6. Shell Programming and Scripting

search of common words in set of files

Hi, I have a set of simple, one columned text files (in thousands). file1: a b c d file 2: b c d e and so on. There is a collection of words in another file: b d b c d e I have to find out the set of words (in each row) is present or absent in the given set of files. So, the... (4 Replies)
Discussion started by: mala
4 Replies

7. UNIX for Dummies Questions & Answers

vi search/replace using a set

Hi, I'm trying to do a global search/replace in vi using a set - I want to find every occurance of a carriage return followed by a character and replace it with a space. I've tried the following: :%s/\n/ /g It does the search ok, but it replaces the characters with the literal value ""... (2 Replies)
Discussion started by: HudZo
2 Replies

8. Shell Programming and Scripting

Ksh script - Design ? - Search file and set variables

Hi - I'm trying to think of a clever way to write a shell script (trying to stay w/ ksh as that's what I know the best...) that will resolve the following problem: Problem - On a daily basis I have to email folks who are on-call to remind them. I was hoping to script this out so I could have... (9 Replies)
Discussion started by: littlefrog
9 Replies

9. Shell Programming and Scripting

set password using a shell script

Hi All How can I set password in linux.It is OK if it display password in plain text in script. manually i can set: #passwd Changing password for root Enter new password: Bad password: too weak. Re-enter new password: Password changed. # I want this to be done by script.Please let me... (2 Replies)
Discussion started by: tannu
2 Replies

Featured Tech Videos