sed pattern matching or passing variables


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed pattern matching or passing variables
# 1  
Old 03-17-2009
sed pattern matching or passing variables

I need sed to add a "/>" to the end of a line that contains/starts with <meta.

current line is
HTML Code:
<meta name="keywords" content="kayword 1, kwyword2">
and should be
HTML Code:
<meta name="keywords" content="kayword 1, kwyword2 " />

i need something like this?
Code:
find . -name "*.html" -print0 | xargs -0 sed -i 's/<meta *>/<meta * \/>/g'

any help would be appreciated Smilie
# 2  
Old 03-17-2009
Try:
Code:
 sed -n '/^<meta/p' test | sed 's\>\ />\g'

(sed takes only the lines starting with <meta and then changes > to /> )
# 3  
Old 03-17-2009
....note that "test" must be your own file...
# 4  
Old 03-17-2009
Another approach:

Code:
sed 's!\(<meta.*\).$!\1 />!'

Regards
# 5  
Old 03-17-2009
I tried
Code:
's!\(<meta.*\).$!\1 />!'

with find like this
Code:
find . -name "*.html" -print0 | xargs -0 sed -i 's!\(<meta.*\).$!\1 />!'

It works,but the result contains two ">" like this...
HTML Code:
<meta name="keywords" content="keyword1, keyword2"> />
i could run another sed to replace "> />", but if it is a easy improvement in the find section above?

I played with JCastro's example, but could not get it to work with find piped to sed

I am close and have enough to play further, but any further assistance would be great.
# 6  
Old 03-17-2009
This works...
Code:
find lib/lib-pal -name "*.html" -print0 | xargs -0 sed -i 's!\(<meta.*\).$!\1 />!;s/"> \/>/" \/>/g'

Thanks

Last edited by sky_rivers; 03-17-2009 at 03:05 PM..
# 7  
Old 03-18-2009
The following uses regular expression to remove any unlisted characters (including ">") and seems to be more predictable.
HTML Code:
find . -name "*.html" -print0 | xargs -0 sed -i s/\(<meta name[a-zA-Z \=\"\,\.\0-8\&\;\@-]*\).*/\1 \/>/
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Passing variables to a sed function

Hi everyone, I've re-written some of our scripts to use more functions and I've run into a situation where passing a variable to a sed function does not work. My function is a one-liner sed command as follows: function StringSub() { sed -i "${1}/${2}/${3}/${4}" ${5} } Where ${1} through... (4 Replies)
Discussion started by: richardsantink
4 Replies

2. Shell Programming and Scripting

Sed: printing lines AFTER pattern matching EXCLUDING the line containing the pattern

'Hi I'm using the following code to extract the lines(and redirect them to a txt file) after the pattern match. But the output is inclusive of the line with pattern match. Which option is to be used to exclude the line containing the pattern? sed -n '/Conn.*User/,$p' > consumers.txt (11 Replies)
Discussion started by: essem
11 Replies

3. Shell Programming and Scripting

Help with passing multiple variables into SED

Hello I am hoping you can help. I use ksh in Solaris9 I am trying to pass user imputed variables into SED but for some reason can only get SED to recognize one variable. I have experimented with te below command with putting ' ' and " " in different places but I cant seem to get it to... (8 Replies)
Discussion started by: lostincashe
8 Replies

4. Shell Programming and Scripting

sed pattern matching

Unfortunately this chap has been banned for some reason and I was looking forward to the resolution of his question: - https://www.unix.com/shell-programming-scripting/123118-append-position-28-33-a.html He was asking if you can use sed to match a pattern you want to replace within a... (6 Replies)
Discussion started by: steadyonabix
6 Replies

5. Shell Programming and Scripting

passing variables to sed in ksh

Hi, i need help passing variables to sed using ksh. My goal is to get particular data from log files. first i put a mark to the log files. echo "TEST_"`date + %m_%d_%Y_%T"` >markFile this will produce a 'markFile' which contain text like this TEST_06_01_2009_21:55:09 then i put the mark... (2 Replies)
Discussion started by: d.anggrianto
2 Replies

6. Shell Programming and Scripting

sed - matching pattern one but not pattern two

All, I have the following file: -------------------------------------- # # /etc/pam.d/common-password - password-related modules common to all services # # This file is included from other service-specific PAM config files, # and should contain a list of modules that define the services... (2 Replies)
Discussion started by: RobertBerrie
2 Replies

7. Shell Programming and Scripting

Passing variables to sed

Hi Folks, How can I make the following to work from a korn shell? old="OLDSTRING" new="NEWSTRING" file="myfile.txt" sed -n 's/$old/$new/gp' $file Thanks in advance rogers42 (3 Replies)
Discussion started by: rogers42
3 Replies

8. Shell Programming and Scripting

passing variables to sed function in a script ....

Hello , I have a script named testscript.sh wherein I have two variables $var and $final (both of which contain a number) I have a sed write function inside this script as follows: sed '1,2 w somefile.txt' fromfile.txt Now , in the above i want to pass $var and $final instead of... (2 Replies)
Discussion started by: shweta_d
2 Replies

9. Shell Programming and Scripting

passing variables to sed inside script

I am trying to pass a regular expression variable from a simple script to sed to remove entries from a text file e.g. a='aaaa bbbb cccc ...|...:' then executing sed from the script sed s'/"'$a"'//g <$FILE > $FILE"_"1 my output file is always the same as the input file !! any... (5 Replies)
Discussion started by: Daniel234
5 Replies

10. Shell Programming and Scripting

Passing variables to sed

Hi folks, I'm looking for a solution to pass variables to a sed-command. I'm reading a lot of threats and also the q&a "How can I use a variable in sed?". None of these commands works. I'm using AIX 5.2. I want to do the following: NUMBER=` echo 38341` | sed -n '/$NUMBER/p' an obtained... (3 Replies)
Discussion started by: jfisch
3 Replies
Login or Register to Ask a Question