Author Topic: Grep/File Searching question  (Read 803 times)

LorKorub

  • Member
  • **
  • Posts: 175
  • Kudos: 0
Grep/File Searching question
« on: 3 October 2002, 14:51 »
I have about 40 or so *.php files that I fucked up with table dimensions on.  I need to delete a single phrase (height=200px) from all of the files.  Is there a way to use grep or awk to do this?
"American English -- the noble language of your superiors"

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Grep/File Searching question
« Reply #1 on: 3 October 2002, 15:50 »
this will find all the *.php files in the current directory and strip out that phrase with sed:

for i in `find ./ -name '*.php'`; do sed 's/height=200px//g' $i > tmp; mv tmp $i; done

[ October 03, 2002: Message edited by: flap ]

"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


LorKorub

  • Member
  • **
  • Posts: 175
  • Kudos: 0
Grep/File Searching question
« Reply #2 on: 3 October 2002, 15:57 »
Thanks for the help, but that didn't work.

My command prompt just turned into a '>'
"American English -- the noble language of your superiors"

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Grep/File Searching question
« Reply #3 on: 3 October 2002, 16:01 »
did you definitely paste/type it correctly? and you are using bash i presume?
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
Grep/File Searching question
« Reply #4 on: 3 October 2002, 16:01 »
so in general if i wanted to change a recurring line (two or three ocurrences in each file) to a different line, i should use find and pipe it to sed? or would i use a different program for replacing.

I will read the manpages for sed and find when i get home, but if there's another utility needed for this, what is it please?

thanks.

Actually i was going to ask this exact question last week.

LorKorub, i suggest reading the manpages since a big long command string may often not be exactly what you require,and anyway there are more than one way to do most things and one will suit your style better than the others.

edit - i just checked flap's post and noticed there is no pipe, so what is going on here, please?
sorry to be so dumb and sorry to hijack somebody's thread but i am trying to do the same thing...

[ October 03, 2002: Message edited by: Calum ]

visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

LorKorub

  • Member
  • **
  • Posts: 175
  • Kudos: 0
Grep/File Searching question
« Reply #5 on: 3 October 2002, 16:02 »
I am using Korn Shell. And I did a reverse-video on it right from the board.

I'll try it in bash...
"American English -- the noble language of your superiors"

LorKorub

  • Member
  • **
  • Posts: 175
  • Kudos: 0
Grep/File Searching question
« Reply #6 on: 3 October 2002, 16:04 »
Bitchin'....

Thanks, dude....

How exactly did all of that work?

EDIT:

Calum....I've been on the man pages and Google all night trying to find a simple way to do this.  I came up with the hypothesis that maybe you could use grep to find the line, pipe it to vi and use the Replace fucntion.....

...didn't work...LOL

[ October 03, 2002: Message edited by: LorKorub / BOB ]

"American English -- the noble language of your superiors"

Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
Grep/File Searching question
« Reply #7 on: 3 October 2002, 16:12 »
hey i hope i didn't sound condescending there, i haven't read the man pages, but these things you have both mentioned i think will have given me enough reading to sort out my own concern, so thanks to both of you!  :D
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Grep/File Searching question
« Reply #8 on: 3 October 2002, 16:17 »
for i in `find ./ -name '*.php'`;
...for each line returned by the command find ./ -name '*.php'...

do sed 's/height=200px//g' $i > tmp;

...do the following: use sed to find every occurrence of the string "height=200px" in the filename denoted by 'i' and replace it with a blank string. the > operator then redirects the output of this command (which would otherwise just be printed to stdout i.e. the screen) to the file 'tmp'

mv tmp $i; done

...overwrite the original unmodified file with the tmp file

In windows, incidentally, the procedure is:

1. Open file in notepad
2. Use the 'Replace' facility to remove all unwanted strings
3. Save file
Repeat 40 times
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
Grep/File Searching question
« Reply #9 on: 3 October 2002, 16:24 »
and to specify a value to replace the original value? (for instance, to replace the line with "height=400px")
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

LorKorub

  • Member
  • **
  • Posts: 175
  • Kudos: 0
Grep/File Searching question
« Reply #10 on: 3 October 2002, 16:24 »
Thanks again....you just saved me a lot of work!

I'll be sure to put this all in a file somehwere.  It is very valuable.
"American English -- the noble language of your superiors"

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Grep/File Searching question
« Reply #11 on: 3 October 2002, 16:27 »
quote:
and to specify a value to replace the original value? (for instance, to replace the line with "height=400px")


sed 's/height=200px/height=400px/g'
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca


Calum

  • Global Moderator
  • Member
  • ***
  • Posts: 7,812
  • Kudos: 1000
    • Calum Carlyle's music
Grep/File Searching question
« Reply #12 on: 3 October 2002, 16:28 »
thank you very much for your thoroughness.  
visit these websites and make yourself happy forever:
It's my music! | My music on MySpace | Integrational Polytheism

flap

  • Member
  • **
  • Posts: 1,268
  • Kudos: 137
Grep/File Searching question
« Reply #13 on: 3 October 2002, 16:30 »
sed tutorial: http://www.grymoire.com/Unix/Sed.html

worth looking at similar tutorials for awk, grep, bash scripting etc
"While envisaging the destruction of imperialism, it is necessary to identify its head, which is none other than the United States of America." - Ernesto Che Guevara

http://counterpunch.org
http://globalresearch.ca