LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
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 08-29-2007, 09:13 AM   #1
manrui
LQ Newbie
 
Registered: Aug 2007
Posts: 1

Rep: Reputation: 0
AWK (VIM) question


HOw can I find and change a series of words in a document? Using command line or VIM ...? thank you


Manrui
 
Old 08-29-2007, 09:18 AM   #2
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Using vim, the command is similar to sed:
:%s/<pattern to match>/<replacement>/

The "%" covers all of the lines in the text file.

Using sed:
sed 's/<pattern to match>/<replacement>/' textfile >newtextfile
 
Old 08-29-2007, 09:19 AM   #3
MensaWater
LQ Guru
 
Registered: May 2005
Location: Atlanta Georgia USA
Distribution: Redhat (RHEL), CentOS, Fedora, CoreOS, Debian, FreeBSD, HP-UX, Solaris, SCO
Posts: 7,831
Blog Entries: 15

Rep: Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669Reputation: 1669
sed would be better than awk for command line.

In vim or vi you can do something like:

:1,$ s/original/replacement/g

The above in Vim says to do it for every line from 1 to the end the rest of the line says to substitute the original pattern with the replacement pattern. The g at the end says to do it globally so it will do it for each occurrence.
 
Old 08-31-2007, 04:35 PM   #4
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
Quote:
Originally Posted by jlightner View Post
sed would be better than awk for command line.

I think that I would word that differently. Awk would be preferable for structured text files. Sed may be better for editing free form text. In either case, if the original pattern is split between a line, it may be better to use a sed script. You need to build lines in the pattern or hold space so that you can match a pattern containing the "\n" character. There is a book on Sed & Awk available on the web. ( It is the first edition that is available) The order of sed commands are important in a script and some possible input patterns can make the solution non-trivial.
 
  


Reply



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
Awk question blanny Linux - Software 6 05-02-2006 08:03 AM
awk question puishor Programming 2 08-26-2005 09:44 AM
awk question denalitastic Linux - Newbie 1 06-07-2005 10:42 AM
AWK question paraiso Linux - Newbie 5 05-12-2005 01:37 PM
awk question jonlake Linux - Software 2 05-04-2005 03:58 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:54 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration