search all file for particular text and make changes to line 3


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers search all file for particular text and make changes to line 3
# 1  
Old 06-24-2011
search all file for particular text and make changes to line 3

Hi All,

I am sitting on HPUX. I want to change the exit into #exit, which appears into 3red line of code in shell scripting, wondering how shell script to be called up to perform action. I have following code in all files. Now, I need to find the text exit and replace into #exit.

#!/sbin/sh
SID=pfin
exit

hare krishna
# 2  
Old 06-24-2011
Code:
# In case something goes wrong when batch-editing all your source at once!
tar -cf /path/to/backup.tar *.sh

for FILE in /path/to/*.sh
do
        # Print all lines, and add a comment to the front of line 3
        awk '{ if(NR==3) $1="#" $1 } 1' < "$FILE" > /tmp/$$
        cat /tmp/$$ > "$FILE"
done

rm -f /tmp/$$

# 3  
Old 06-24-2011
Without temporary files:
Code:
for i in *.sh; do perl -i -pe '$.==3 && s/exit/#exit/' $i; done

# 4  
Old 06-24-2011
Thanks for your reply. Also, I have one more questiion. If I have following code and I need to chane $tnspinhg into the given format.

Code:
#!/sbin/sh
SID=prod03
#exit
. qa02
typeset -i mHHMM=`date +%H%M`
typeset -i mDD=$(date +%d)
mDay=`date +%a`
if [[ "${mDay}" = "Sun" && ${mHHMM} -gt 0150 ]]; then
echo "Exiting because time falls under Maintenance Window"
exit
fi
if [[ "${mDay}" = "Sun" && ${mHHMM} -lt 0440 ]]; then
echo "Exiting because time falls under Maintenance Window"
exit
fi
#if [[ ${mDD} -ge 12 && ${mDD} -le 13 ]]; then
#echo "Second Monday of the month"
#exit
#fi
tnsping $SID  ==> this needs to be changed into sleep 5 tnsping $SID as the same
sleep 5
tnsping $SID

hare krishna
# 5  
Old 06-24-2011
Is it always the same line?

If your scripts all have this much identical code at the start, why not create a file with the identical code and just source it in all the scripts?
# 6  
Old 06-24-2011
It's not identical, the database name happend to be different in all the files.
# 7  
Old 06-24-2011
I still need to know if it's always the same line.

If they're all NEARLY identical then why not make one script that takes a parameter? And if people insist on having individual scripts, they can all just be
Code:
#!/bin/sh
exec /path/to/myscript.sh PARAMETER

instead of repeating the same code hundreds of times. Then next time you need to alter it you can do so in ONE file instead of having to dangerously batch-edit hundreds of them at once. The computer's supposed to work for you, not vice versa!
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

Read in search strings from text file, search for string in second text file and output to CSV

Hi guys, I have a text file named file1.txt that is formatted like this: 001 , ID , 20000 002 , Name , Brandon 003 , Phone_Number , 616-234-1999 004 , SSNumber , 234-23-234 005 , Model , Toyota 007 , Engine ,V8 008 , GPS , OFF and I have file2.txt formatted like this: ... (2 Replies)
Discussion started by: An0mander
2 Replies

3. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

4. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

5. Shell Programming and Scripting

search between keywords and make a single line

have a very big file where need to format it like below example file: abcd today is great day; search keyword 'abcd' and append to it all words till we reach ; to make it a single line. output should look like. abcd today is great day; There are many occurrence of such... (2 Replies)
Discussion started by: giri4332
2 Replies

6. Shell Programming and Scripting

PERL or SHELL Scrript to search in Directories by taking line by line from a text file

Unix box server version *********** >uname -r B.11.00 >echo $SHELL /usr/bin/ksh --> in this server, I have the path like /IMbuild/dev/im0serv1 ---> in that directory I have the folders startup(.jsp files nearly 100 jsp's ) and scripts(contains .js files nearly 100 files) ... (9 Replies)
Discussion started by: pasam
9 Replies

7. Shell Programming and Scripting

Search text from a file and print text and one previous line too

Hi, Please let me know how to find text and print text and its previous line. Please don't get irritated few days back I asked text and next line. I am using HP-UX 11.11 Thanks for your help. (6 Replies)
Discussion started by: kamranjalal
6 Replies

8. Shell Programming and Scripting

reverse search a text file from a specified line

Hello All, I have a following task that I need to accomplish through a script or program and I am looking for some help as I have exhausted my ideas. 1. given: a text file with thousands of lines 2. find: pattern A in file and get line number ( grep -n works) 3. find: the first occurence of... (14 Replies)
Discussion started by: PacificWonder
14 Replies

9. UNIX for Dummies Questions & Answers

how can search a String in one text file and replace the whole line in another file

i am very new to UNIX plz help me in this scenario i have two text files as below file1.txt name=Rajakumar. Discipline=Electronics and communication. Designation=software Engineer. file2.txt name=Kannan. Discipline=Mechanical. Designation=CADD Design Engineer. ... (6 Replies)
Discussion started by: kkraja
6 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