Visit Our UNIX and Linux User Community

small sed script on command line.

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting small sed script on command line.
# 1  
Old 08-25-2009
small sed script on command line.

Can anyone help me get this small sed script to work in shell on the command line?
I need it in a one liner really as i want to edit many scripts in a for loop and dont want to have to invoke a separate script each time.


sed '/mailx\ -s.*$ { 
#Comment above mailx line 
mailx -s "blahblah blah my new subject" $someone_who_cares 
}' $1

The script needs to match a pattern, insert a line above it and then change the pattern to something else.

I've tried the following and no joy

sed '/mailx\ -s.*$/ { i\ #Comment above mailx line c\ mailx -s "blahblah blah my new subject" $someone_who_cares }' file > file2  

sed '/mailx\ -s.*$/ {; i\; #Comment above mailx line ;c\; mailx -s "blahblah blah my new subject" $someone_who_cares ;};' file > file2

# 2  
Old 08-26-2009
Not sure if I understood; nevertheless:
$> cat infile
$>sed -e '/pattern/i\#comment' -e 's/pattern/newpattern/' infile

# 3  
Old 08-26-2009
Dude thats great. thanks. I never thought of splitting it into multiple -e 's.

Danke Smilie

---------- Post updated at 11:59 AM ---------- Previous update was at 11:51 AM ----------

Actually bummer its not working. I don't think it likes the i\ on command line which doesnt relaly make sense.

I'm running sed on Solaris 9.

root@host# sed -e '/mailx\ -s.*$/i\#Comment' file >file2
sed: command garbled: /mailx\ -s.*$/i\#comment

# 4  
Old 08-26-2009
i am reading the book <sed&awk>
here is a copy:
The append (a), insert (i), and change (c) commands provide editing functions that are commonly performed with an interactive editor, such as vi. You may find it strange to use these same commands to "enter" text using a noninteractive editor. The syntax of these commands is unusual for sed because they must be specified over multiple lines.
# 5  
Old 08-26-2009
No idea if you have GNU sed (gsed?) on Solaris. You could also do following:
sed '
' infile

Or as oneliner press CTRL+v CTRL+j to get a newline (represented in the code with that ^J):
sed '/pattern/i\#comment^Js/pattern/newpattern/' infile

# 6  
Old 08-26-2009
Thanks guys. Looks like i'll have to do it in a script then. No biggy i suppose.

I aint got gnused on this box.

Cheers for the info.

Previous Thread | Next Thread
Test Your Knowledge in Computers #238
Difficulty: Easy
Steve Jobs was the first to build a theoretical model for communication using packet switching.
True or False?

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed command within script wrongly deleting the last line

Hi, I have a shell script which has a for loop that scans list of files and do find and replace few variables using sed command. While doing this, it deletes the last line of all input file which is something wrong. how to fix this. please suggest. When i add an empty line in all my input file,... (5 Replies)
Discussion started by: rbalaj16
5 Replies

2. Shell Programming and Scripting

sed working on command line but file unchanged when execute with Shell script

I have a simple task to replace unix line feed end of line characters with carriage returns. When I run the following “change file in place” sed instruction from the command line all the Line feeds are successfully replaced with Carriage returns. sed -i 's/$/\r/' lf_file.txt But that same... (1 Reply)
Discussion started by: hawkman2k
1 Replies

3. UNIX for Dummies Questions & Answers

HELP - NEED Small Script ( or single awk Command)

Hi All REQUIREMENT:- ========== There is one folder named /data/ds/dpr_ukdw_sys/working/unixfile In that folders there are files are like below CODS_ACTMZ_TRANS_ALL_20150911.TXT CODS_ACTMZ_REF_CTR_ACT_MIL_20150911.TXT CODS_ACTMZ_REF_NHA_ALL_20150911.TXT ... (4 Replies)
Discussion started by: Nagarjuna4347
4 Replies

4. 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

5. Shell Programming and Scripting

Concatenate small line with next line perl script

Hello to all, I'm new to perl, I have input file that contains the string below: 315350535ff450000014534130101ff4500ff45453779ff450ff45545f01ff45ff453245341ff4500000545000This string has as line separator "ff45". So, I want to print each line but the code below is not working. perl -pe '... (2 Replies)
Discussion started by: Ophiuchus
2 Replies

6. Shell Programming and Scripting

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

Hi, I am running a script 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 :- ./ line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

7. Shell Programming and Scripting

sed command to grep multiple pattern present in single line and delete that line

here is what i want to achieve.. i have a file with below contents cat fileName blah blah blah . .DROP this REJECT that . --sport 7800 -j REJECT --reject-with icmp-port-unreachable --dport 7800 -j REJECT --reject-with icmp-port-unreachable . . . more blah blah blah --dport 3306... (14 Replies)
Discussion started by: vivek d r
14 Replies

8. 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

9. Shell Programming and Scripting

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. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

Need help in sed command [ printing a pattern + its line no or line no alone ]

Hello friends, Only very recently i started learning sed i found that sed is faster in finding the patterns than some of my scripts that uses grep to check the patten inside a file using line by line search method which is time consuming. The below script... (4 Replies)
Discussion started by: frozensmilz
4 Replies

Featured Tech Videos