LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   using awk (https://www.linuxquestions.org/questions/linux-newbie-8/using-awk-147693/)

wedgeworth 02-18-2004 02:53 PM

using awk
 
if i have a bunch of pages loading bogus information and i need to change one number from 150 to 100....how do i find and replace in all the files the correct number. is awk my best bet? i've never used it. what is the best way to pipe grep into a awk command. preferable only "*.php" files?

thnx

david_ross 02-18-2004 02:58 PM

If you are wanting to replace text then sed is probably better. You probably don't need to use grep.

wedgeworth 02-18-2004 03:05 PM

only reason i thought i might need to use grep is b/c i've got many php files in multiple directories that need numbers changed in them. so i'm trying to come up with a nice command to switch "word1" with "word2".....

david_ross 02-18-2004 03:15 PM

you could probably use sed in conjunction with a "find -exec" command.

Rick485 02-18-2004 03:17 PM

Here is a short online tutorial about using awk:

http://otn.oracle.com/pub/articles/dulaney_awk.html

A couple of years ago I took a Unix class where we were briefly introduced to awk and sed. One of these days I need to go back and try to remember how to awk, sed, grep, fgrep, egrep, pipes and regular expressions. I only use Linux on my home computer and so far have not bothered to use those tools.

Rick485 02-18-2004 03:23 PM

Ooops, I got delayed several minutes in clicking "Submit Reply". Sorry that I did not notice the awk is no longer what you are planning to use.

wedgeworth 02-18-2004 03:27 PM

which ever works....which ever lets me edit "word1" for "word2"...i'm like Rick485 i never bothered to learn awk or sed though i have used somewhat simple greps and finds. if someone knows i nice little pipped command to exchange/edit mulitple files in multiple directories...i would appreciate. as i contunie my search.....thnx guys...


*****UPDATE*******

b/c at the moment i can't seem to get it to work. my experience with sed is only replacing "word1" with "word2" when outputing to the screen. it isn't actually finding and replacing the word(s) in the actual file.

sed 's/ word1 / word2 /g' test

isn't working. help would be appreciated.


Tinkster 02-18-2004 06:27 PM

Code:

find /<your_base_directory_in_question> -iname "*.php" -exec sed -i "s/word1/word2/g" {} \;
Attention, this only works with sed >= 4 ... older
versions don't know about in-file replacement,
and you'd have to build a script to mv original
and copy around ...


Cheers,
Tink

Tinkster 02-19-2004 03:44 PM

Hi Wedgeworth...

Just out of curiosity ...

Did this work for you?


Cheers,
Tink

wedgeworth 02-20-2004 07:48 AM

thanks for the little bit of code. i ended up doing a few greps that redirected to a data file that i read from a did a simple

sed 's/word1/word2/g' $file1 > test99
cat test99 > $file1

commands. i think i already had this running before you shot me over your commands. i held onto it but haven't tried it out yet. plus i'm only running sed version 3.02 on this computer. but thanks for the help though. hopefully when i get my server updated on some newer versions it'll come in handy. thnx....


All times are GMT -5. The time now is 06:03 AM.