Grep for string and replace or insert a different string in each file

Hi,

I have run into an issue in IE8 and IE9 on windows 7 that all of our pages that have flash files no longer display full screen like we want even though the object has height and width set to 100%. Apparently a change was made to IE that the default height and width of the html document and the body tag are no longer 100%, so I need to add a stylesheet into each file or add an inline style to the html and body tag...

so what I was thinking was to use Grep to identify all the files that contain .swf and then add a stylesheet to the header of each of those files, or do a replace on <html and <body to include an inline style...

I am under the impression that I could do this using Grep to pipe the content to AWK or SED but I would like help with the commands.

Thanks!
LVL 17
jrm213jrm213Asked:
Who is Participating?
 
jrm213jrm213Author Commented:
Unfortunately no. I get the same issue. that solution is good if the directory names don't have spaces in them.

I ended up getting a solution from a friend last night

find . -type f -iname "*.html" -exec grep -Z -l \.swf {} \; | xargs -0
-n 1 sed -i.bak 's#<head>#<head><link rel="stylesheet" type="text/css"
media="all" href="/css/for_all.css"/>#'

Open in new window

Credited to PAW

The above makes a backup of the files before writing into them, so if all goes well you will have a bunch of .bak files that you need to remove, but if it doesn't you can revert back.

He gave a very detailed explanation of this solution vs. the one I had been trying.
0
 
jhheiderCommented:
The basic form should be:

sed -i 's/<body/<body style="some: style;"/' `grep -lR \.swf .`

The short explanation is:

sed -> stream editor
-i -> inline (modify files)
s/// -> substitute
your substitutions
`` -> return the results of the command into the command line
grep
-l -> files that match
-R -> recurse
. -> pwd

Does that work? (Please test on non-production files.)
0
 
jrm213jrm213Author Commented:
Will try now on our testing server. I would never try something like this on the live server :)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
jrm213jrm213Author Commented:
ok so I ran jus tthr grep command to get an idea of what would be affected, and for some reason it is finding swf in a lot of mp4 files... is there a way to specify to grep only search .html files
0
 
jhheiderCommented:
Sure:

grep -lR \.swf . | grep \.html$
0
 
jrm213jrm213Author Commented:
so I have tried this
sed -i 's/<head>/<style type/="text/css">html,body{width:100%;height:100%;}/' `grep -lR \.swf` . | `grep \hillsborough.html`

Open in new window


It just sits and does nothing. When I run the  
grep -lR \.swf . | grep \hillsborough.html

it brings back what I expect.

Initially it was complaining about invalid parameter e which I figured as the e in type= so I escaped the =
0
 
jhheiderCommented:
You have an unescaped / in your expression. Try this:

sed -i 's/<head>/<head><style type="text\/css">html,body{width:100%;height:100%;}<\/style>/' `grep -lR \.swf` . | `grep hillsborough.html`

Open in new window

0
 
jrm213jrm213Author Commented:
it just sits and does nothing. I think it has something to do with the ` around the grep command at the end.
0
 
jhheiderCommented:
Ah, I didn't even see that. We've set up a bad pipeline. That should look like:

sed -i 's/<head>/<head><style type="text\/css">html,body{width:100%;height:100%;}<\/style>/' `grep -lR \.swf . | grep hillsborough.html`

Open in new window


rather than:

sed -i 's/<head>/<head><style type="text\/css">html,body{width:100%;height:100%;}<\/style>/' `grep -lR \.swf` . | `grep hillsborough.html`

Open in new window


The first version uses the output of a pair of greps as the file list for sed. The second uses the output of a single grep (with no input) *and* the pwd as input for a sed command, then pipes the output into a grep command to look for hillsborough.html. So, it'll sit forever waiting for stdin for the first grep command.
0
 
jrm213jrm213Author Commented:
ok, so I ran it on the staging server and then ran it on the live server, where of course I found an issue.

some of the paths have spaces in them and it doesn't look like it works in that situation...

I get
sed: can't read .6b/ltut/09/ltut_6b_09.html

I don't think it's too big of an issue because the tutorial files specify exact height and widths and there wasn't a problem with them to begin with. but for future reference is there a way to make it work for paths with spaces in them?
0
 
jhheiderCommented:
Yes, but it starts to get hairy. This is likely to work:

sed -i 's/<head>/<head><style type="text\/css">html,body{width:100%;height:100%;}<\/style>/' `grep -lR \.swf . | sed 's/ /\\ /g' | grep \.html$`

Open in new window

0
 
jrm213jrm213Author Commented:
i was afraid that might be the solution, I will give it a try, it turns out I do need it. In general spaces in directory and file names make me mad, this just gives me another reason to hate them lol.

It didn't seem like that was working and I got to thinking that the way that is written it escapes the wrong part? In what order is this command being run

grep -lR \.swf . | sed 's/ /\\ /g' | grep \.html$

does it find the html files first then escape the spaces, then get a listing from that of files that contain .swf? Or is it the other way around? Either way wouldn't the escaped spaces be replaced by the 2nd grep?
0
 
jhheiderCommented:
Good point. The first grep should output lines, so the second won't care about spaces. Reordering the last grep and the sed should do it.

Spaces really are the bane of the shell-scriptor's existence. xargs solves that problem, but can introduce some others. Eventually, you just get to the point where you want to put down the shell pipelines and use Perl or an AWK script.
0
 
jrm213jrm213Author Commented:
hi

so should it be
sed -i 's/<head>/<head><link rel="stylesheet" type="text\/css" media="all" href="\/css\/for_all.css"\/>/' `sed 's/ /\\ /g' | grep -lR \.swf . | grep \.html`

Open in new window

or
sed -i 's/<head>/<head><link rel="stylesheet" type="text\/css" media="all" href="\/css\/for_all.css"\/>/' `grep -lR \.swf . | grep \.html$ | sed 's/ /\\ /g'`

Open in new window

0
 
jhheiderCommented:
The second:


sed -i 's/<head>/<head><link rel="stylesheet" type="text\/css" media="all" href="\/css\/for_all.css"\/>/' \
  `grep -lR \.swf . | \
    grep \.html$ | \
    sed 's/ /\\ /g'`

Open in new window

0
 
jrm213jrm213Author Commented:
dang, still not working.

for some reason the input that sed is getting to do the inline replace is not seeing the escaped spaces.

if I run this
grep -lR \.swf . | \
    grep \.html$ | \
    sed 's/ /\\ /g'

Open in new window


I get the correct escaped spaces in the output
but when it gets those backticks ` around it, and sed reads it, something gets changed, because it can't read the files and what it outputs as not being able to read shows that the problem is the spaces and that they weren't escaped...
0
 
jhheiderCommented:
When in doubt, escape again. If you change:
    sed 's/ /\\ /g'

Open in new window

to
    sed 's/ /\\\\ /g'

Open in new window

does it magically start working?
0
 
jrm213jrm213Author Commented:
A friend who as far as I know isn't on EE, answered this for me via email. His solution solved the problem so I posted that.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.