LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-18-2004, 02:53 PM   #1
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Rep: Reputation: 30
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
 
Old 02-18-2004, 02:58 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
If you are wanting to replace text then sed is probably better. You probably don't need to use grep.
 
Old 02-18-2004, 03:05 PM   #3
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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".....
 
Old 02-18-2004, 03:15 PM   #4
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 64
you could probably use sed in conjunction with a "find -exec" command.
 
Old 02-18-2004, 03:17 PM   #5
Rick485
Member
 
Registered: Sep 2003
Location: Arizona
Distribution: Kubuntu 8.04
Posts: 202

Rep: Reputation: 30
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.
 
Old 02-18-2004, 03:23 PM   #6
Rick485
Member
 
Registered: Sep 2003
Location: Arizona
Distribution: Kubuntu 8.04
Posts: 202

Rep: Reputation: 30
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.
 
Old 02-18-2004, 03:27 PM   #7
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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.


Last edited by wedgeworth; 02-18-2004 at 03:55 PM.
 
Old 02-18-2004, 06:27 PM   #8
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
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
 
Old 02-19-2004, 03:44 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Hi Wedgeworth...

Just out of curiosity ...

Did this work for you?


Cheers,
Tink
 
Old 02-20-2004, 07:48 AM   #10
wedgeworth
Member
 
Registered: Aug 2003
Posts: 234

Original Poster
Rep: Reputation: 30
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....
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About awk script sachin_keluskar Linux - Software 2 06-24-2005 03:19 AM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM
[cshell] awk help! saiz66 Programming 3 09-28-2004 06:55 PM
awk problem jpostma Programming 7 10-22-2003 06:07 AM
awk question dilberim82 Programming 2 10-01-2003 07:12 PM


All times are GMT -5. The time now is 09:33 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration