0

i m try to grep two lines of html pattern from a file and print using cat. The lines are:

<link type="text/css" rel="stylesheet" href="https://test.mydomain.com/store.css">

and

<script type="text/javascript" language="javascript" src="https://test.mydomain.com/store/gwt.nocache.js">

if i use them seperatly using grep command. like

if grep -F -i -n '<link type="text/css" rel="stylesheet" href="https://test.mydomain.com/store.css">' tmp.html > out.txt; then cat out.txt # IT WORKS.

but i am in need to grep both the pattern. i tried with egrep. as

egrep '(pattern1|pattern2)' 

as

if egrep -e -i -c '(\<link type\=\"text\/css\" rel\=\"stylesheet\" href\=\"https\:\//test\.mydomain\.com\/gwtstore\/store\.css\"\>|\<link type\=\"text\/css\" rel\=\"stylesheet\" href\=\"https\:\//test\.mydomain\.com\/store\/gwt\.nocache\.js\"\>)' tmp.html > out.txt; then cat out.txt 

but the above syntax not working out.. please Assist. all i m try to search two set of html code in a file, if they exit replace with another pattern using sed command.

yazu
  • 4,462
  • 1
  • 20
  • 14
Johnbritto
  • 149
  • 2
  • 14

3 Answers3

0

Use fgrep with -e option like this

fgrep -e 'firts_pattern' -e 'second_pattern'
yazu
  • 4,462
  • 1
  • 20
  • 14
0

If you plan to use sed you may just want to use the following. You may not need two separate commands. Using sed you can find if the two patterns exist and if so replace both of them with one command.

NOTE: The following contains an inline edit. PLEASE BACKUP YOUR FILE PRIOR TO TESTING

sed -i 's/<link type="text\/css" rel="stylesheet" href="https:\/\/test.mydomain.com\/store.css">/<REPLACE PATTERN 1>/g;s/<script type="text\/javascript" language="javascript" src="https:\/\/test.mydomain.com\/store\/gwt.nocache.js">/<REPLACE PATTERN 2>/g' /yourfile

If you are an OS X user you can try the following

NOTE: The following contains an inline edit. PLEASE BACKUP YOUR FILE PRIOR TO TESTING

sed -i '' 's/<link type="text\/css" rel="stylesheet" href="https:\/\/test.mydomain.com\/store.css">/<REPLACE PATTERN 1>/g;s/<script type="text\/javascript" language="javascript" src="https:\/\/test.mydomain.com\/store\/gwt.nocache.js">/<REPLACE PATTERN 2>/g' /yourfile

Remember to escape the '/' characters in your REPLACE PATTERN if you intend on using them.

Also you may want to try awk. In this case it functions much like grep.

awk '/<link type="text\/css" rel="stylesheet" href="https:\/\/test.mydomain.com\/store.css">/; /<script type="text\/javascript" language="javascript" src="https:\/\/test.mydomain.com\/store\/gwt.nocache.js">/' /yourfile
E1Suave
  • 268
  • 2
  • 10
0

with egrep you don't need those parenthesis, just the pipe separating the patterns inside single quotes.

egrep -n 'pattern1|pattern2'

should work just fine.

TaoJoannes
  • 594
  • 5
  • 14