[SOLVED] sed -i not available in solaris 5.10


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [SOLVED] sed -i not available in solaris 5.10
# 1  
Old 10-01-2012
[SOLVED] sed -i not available in solaris 5.10

Hi All,

i'm writing a script where i have to grep for a pattern and the 3 lines after the pattern and comment them out.

Note that i have to do this for multiple files, i am able to grep the pattern and the next 3 lines but since solaris does not recognize the -i option, i was wondering if there was any workaround.

sample file:

Code:
cat list.txt

...
...
...
Hello world 
how r u 
whats up
....
....
...

[... represent randon text in the file which is not relevant ]

Code:
vi pattern.sh 

cat list.txt | while read line 
do 
pattern=$(awk '/hello/{x=NR+2}(NR<=x){print}' $line)
sed -e s/^/#/g $pattern 
done

The output is what i require but i want to be able to overwrite list.txt, so my final output should be

Code:
...
...
...
#hello world
#how ru 
# whats up 
...
...
...

- Cheers
# 2  
Old 10-01-2012
write to a new file, then overwrite the old. That's all -i does anyway.

Of course, it's always dangerous to overwrite your originals. Ideally you could just use the originals as templates for the new ones.
This User Gave Thanks to Corona688 For This Post:
# 3  
Old 10-01-2012
Code:
[user@home-pc ~]$ cat file
...
...
...
Hello world
how r u
whats up
....
....
...
[user@home-pc ~]$ perl -i.bak -lne 'if(/Hello/){$f=1;$l=$.;print "# $_";}elsif($f==1 && $.<=$l+2){print "# $_"}else{print}' file
[user@home-pc ~]$ cat file
...
...
...
# Hello world
# how r u
# whats up
....
....
...


Last edited by balajesuri; 10-01-2012 at 11:18 PM..
This User Gave Thanks to balajesuri For This Post:
# 4  
Old 10-02-2012
thanks Balaji that works.

thanks Corona, i see what you are saying i should have been more specific with my sample file. it actually looks like this.

Code:
cat file ... how r u 
whats up  ... Hello world how r u whats up .... .... 
how r u 
whats up

the only unique pattern is "Hello World", the 2 lines after that repeated across the file. so ideally i have to search for Hello world and the 2 lines after that and insert # at the begining of each line. this i was not able to achieve with sed.
# 5  
Old 10-02-2012
Try:

Code:
sed '/Hello world/,+2s/^/#/' list.txt > list_$$.txt
mv list_$$.txt list.txt

This User Gave Thanks to Chubler_XL For This Post:
# 6  
Old 10-03-2012
excellent!. thanks mate, this worked like a charm!

---------- Post updated 10-03-12 at 12:56 PM ---------- Previous update was 10-02-12 at 10:54 PM ----------

sorry to bother again chubler but the command works on linux but not on solaris 5.10. any ideas ?

---------- Post updated at 01:02 PM ---------- Previous update was at 12:56 PM ----------

forgot to post the error message

sed command garbled and then shows the entire expression.
# 7  
Old 10-03-2012
how about awk? Use nawk on solaris.
Code:
awk '/pattern/ {
        for(N=1; N<=3; N++)
        {
                if(N>1) getline
                print "#", $0;
        }
        next } 1' inputfile > outputfile

This User Gave Thanks to Corona688 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

[Solved] sed command help

Hello all. Im trying very hard to figure this out, but Im a newbie. I have a file that looks like this.... 6315551234 NJ224 5162224567 SUFF Im trying to put a command together that will make it into this.... UM,6315551234,,,,,NJ224,0 UM,5162224567,,,,,SUFF,0 Im all over the... (7 Replies)
Discussion started by: jay11789
7 Replies

2. Shell Programming and Scripting

[Solved] sed to replace words

Hello All, I have file named filelist.txt a.bteq.ctl b.bteq.ctl c.bteq.ctl I want to replace the word bteq to tpt in this file. I used this sed command cat filelist.txt | sed 's/bteq/tpt/g' > filelist.txt But this command deletes all records from the filelist.txt Can... (2 Replies)
Discussion started by: nnani
2 Replies

3. Shell Programming and Scripting

[Solved] sed

sed -e 's/console/raw/g' this command will replace the letter pradeep with rawat what if i want to replace a word like FRIENDS with a space simultaneously from the same file i m replacing pradeep. im doing this sed -e 's/console/raw/g' && sed 's/FRIENDS//g' but i dono why this is not happening. (2 Replies)
Discussion started by: console
2 Replies

4. Shell Programming and Scripting

[SOLVED] sed command

Help request, I have tsted this line of code for hours. The first line works and the second line returns the message " sed: command garbled.....". This is running on solaris. The "${} variables all have good values when echoed. ## /bin/sed -n '1,25p' ${file} >> ${MailFile} ... (3 Replies)
Discussion started by: millerg225
3 Replies

5. UNIX for Dummies Questions & Answers

[solved]Help with a sed command

So I have a bunch of strings in a file. Example Line ./prcol/trt/conf/conf-app/jobdefinition/trt-pre-extr-trt-step.jdef Intended Result pre-extr-trt-step So far I have parsed it out to the last bit, echo $line | cut -d'/' -f7 | cut -d. -f1Result trt-pre-extr-trt-step So I added a... (2 Replies)
Discussion started by: J-Man
2 Replies

6. Shell Programming and Scripting

[solved] how to separate using sed !

dears, hope evryone doing good in his work , i have a question about something important : how can i use 'sed' so in a script automatically it will take an enter before the number 1 in this line so 2 commands will be taken insted of one big command ?... (0 Replies)
Discussion started by: semaan
0 Replies

7. Shell Programming and Scripting

Manipulating a variable using sed (solved)

Hi, My variable has value as this: tvar1="bool_risk_enabled" Boolean "true" Now I need to replace this true with false. Which is the best way to do this? Can we do this with sed command? Please help me. ---------- Post updated at 05:23 PM ---------- Previous update was at 05:00 PM... (2 Replies)
Discussion started by: pravintse
2 Replies

8. Shell Programming and Scripting

Solved: AWK SED HELP

Hi, I need to process a file as below. Could you please help to achieve that using awk/sed commands. Input file: --------------- AB | "abcdef 12345" | 7r5561451.pdf PQRST | "fghfghf hgkjgtjhghb ghhgjhg hghjghg " | 76er6ry.pdf 12345 | "fghfgcv uytdywe bww76 jkh7dscbc 78 : nvchtry hbuyt"... (0 Replies)
Discussion started by: viveksr
0 Replies

9. Shell Programming and Scripting

[solved] remove pattern with sed

Hi, i want to remove a certain pattern when i type pwd. pwd will look like this: ..../....../....../Pat_logs/..../....../...../...... the dotted lines are just random directory names, i want it to remove the "Pat_logs/...../....../....../" part so for example: ... (8 Replies)
Discussion started by: a27wang
8 Replies

10. Shell Programming and Scripting

can this been solved with awk and sed?

Hi Masters, ___________________________________________________________________________________ Group of orthologs #1. Best score 3010 bits Score difference with first non-orthologous sequence - yeast:3010 human:2754 YHR165C 100.00% PRP8_HUMAN 100.00%... (16 Replies)
Discussion started by: mskcc
16 Replies
Login or Register to Ask a Question