Visit Our UNIX and Linux User Community


Loop with sed command to replace line with sed command in it


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Loop with sed command to replace line with sed command in it
# 1  
Old 08-27-2009
Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line.

Line in the many scripts I'm trying to replace:

Code:
       if (ps -ef | grep $(cat $PID_FILE) | grep -v grep); then

I'm trying to replace that with:

Code:
       if (ps -C java -o pid,cmd | grep ${SERVER_NAME}| sed 's/^[ ]//g' | cut -d' ' -f1); then

Here is the script I have:

Code:
#!/bin/sh
for i in `find . -name script1 -o -name script2 -o -name script3 -o -name script4`
        do
        dirVar=`dirname $i`
        fileVar=`basename $i`
        modFileVar=`basename $i .ORI`
        homeVar='/home/chris/tmp'
        echo ${dirVar}/${fileVar}
        cd $dirVar
        cp $fileVar $modFileVar
        if [ -f $fileVar ]
        then
        echo "file exists performing perl line on file $fileVar"
        sed -i "s/ps -ef | grep \$(cat \$PID_FILE) | grep -v grep/ps -C java -o pid,cmd | grep \$\{SERVER_NAME\} | sed 's/^[]//g' |  cut -d' ' -f1 | grep -v grep/" $modFileVar
        chmod 700 $modFileVar
        fi
        echo "File Var $fileVar"
        echo "Modded File Var $modFileVar"
        cd $homeVar
        done
exit

I get this error with the above script:

Code:
sed: -e expression #1, char 102: Unknown option to `s'

Obviously I get it over and over again b/c I'm doing it in a loop.
I was also trying this with perl, and the one line of perl that I was trying was:

Code:
perl -ne 's/ps -ef \| grep \$\(cat \$PID_FILE\) \| grep -v grep/ps -C java -o pid,cmd \| grep \${SERVER_NAME} \| sed 's/^[]//g' \| cut -d -f1 \| grep -v grep' -ne "s/-d/-d' '/; print" $fileVar >> $modFileVar

With the perl line I get this error:
Code:
Warning: Use of "-d" without parentheses is ambiguous at -e line 2.
syntax error at -e line 2, near "s/-d"
  (Might be a runaway multi-line // string starting on line 1)
Execution of -e aborted due to compilation errors.

I have no real preference as to which way to get it working, just that I need to get it working and have been stuck for quite some time now.
# 2  
Old 08-27-2009
probe to chage this line
Code:
sed -i "s/ps -ef | grep \$(cat \$PID_FILE) | grep -v grep/ps -C java -o pid,cmd | grep \$\{SERVER_NAME\} | sed 's/^[]//g' |  cut -d' ' -f1 | grep -v grep/" $modFileVar

for this one:
Code:
sed 's/ps -ef | grep \$(cat \$PID_FILE) | grep -v grep/ps -C java -o pid,cmd | grep \${SERVER_NAME} | sed "s\/\^\[ \]\/\/g | cut -d" " -f1 | grep -v grep/'  $modFileVar


Last edited by chipcmc; 08-27-2009 at 01:14 PM..

Previous Thread | Next Thread
Test Your Knowledge in Computers #707
Difficulty: Medium
The Web console is a debugging tool in the browser which should only be opened and used by web development experts.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed Command to replace particular value.

Hi , My input file contain : list = 3 14 15 10 9 11 12 18 19 20 21 22 23 24 25 26 6 1 2 3 4 5 7 8 16 17 27 28 30 29 Expected output : list = 0 0 0 0 0 0 0 18 0 20 0 0 0 0 0 0 6 0 0 3 4 0 0 0 0 0 0 0 0 0 I want to keep the 8,10,16,17,22 value from the list and put 0 on rest of the... (9 Replies)
Discussion started by: Preeti Chandra
9 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

4. Shell Programming and Scripting

For loop with sed command

Hi Expert, I am working on one script which needs to read the input from the file and replace the words using sed command. For each line in the input file It should create one file. Below is the code. I can see the for loop is working fine but instead of three files it is creating one file ... (3 Replies)
Discussion started by: sharsour
3 Replies

5. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

6. Shell Programming and Scripting

sed command to skip the first line during find and replace operation

Hi Gurus, I did an exhaustive search for finding the script using "sed" to exclude the first line of file during find and replace. The first line in my file is the header names. Thanks for your help.. (4 Replies)
Discussion started by: ks_reddy
4 Replies

7. Shell Programming and Scripting

SED - insert space at the beginning of line and multi replace command

hi I am trying to use SED to replace the line matching a pattern using the command sed 'pattern c\ new line ' <file1 >file 2 I got two questions 1. how do I insert a blank space at the beginning of new line? 2. how do I use this command to execute multiple command using the -e... (5 Replies)
Discussion started by: piynik
5 Replies

8. Shell Programming and Scripting

sed command to replace a word with new line and /

Hi, I have been trying to replace the key word "SQL> spool off " with "/ show errors" with out double quotes in all the files in a directory. above show erros should be displayed next line Could you please help me how to do that. I have tried something like this... (3 Replies)
Discussion started by: pointers
3 Replies

9. Shell Programming and Scripting

Trying to use a sed command in a while loop

I have a file with just over 1 million lines. I am trying to write a script that will find 1400 instances of two particular patterns and insert a url on the 22nd line after the second pattern is matched. Each line in the text file corresponds to a particular check box or dropdown in the software... (3 Replies)
Discussion started by: midniteslice
3 Replies

10. UNIX for Dummies Questions & Answers

How to search and replace a particular line in file with sed command

Hello, I have a file and in that, I want to search for a aprticular word and then replace another word in the same line with something else. Example: In file abc.txt, there is a line <host oa_var="s_hostname">test</host> I want to search with s_hostname text and then replace test with... (2 Replies)
Discussion started by: sshah1001
2 Replies

Featured Tech Videos