The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
How to find a count of a word within a file bd_joy Shell Programming and Scripting 9 07-14-2008 06:29 AM
how to move word by word on command line pbsrinivas UNIX for Dummies Questions & Answers 1 11-23-2007 03:17 AM
Change Position of word character cedrichiu Shell Programming and Scripting 6 03-11-2007 10:52 PM
how to find a word repeated in a file gurukottur UNIX for Dummies Questions & Answers 4 08-24-2006 01:53 AM
Can a shell script pull the first word (or nth word) off each line of a text file? tricky Shell Programming and Scripting 5 08-17-2006 03:29 AM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 02-13-2008
Registered User
 

Join Date: Oct 2007
Posts: 65
Unhappy find a word in a file, and change a word beneath it ??

Hi all,

I have a file with lines written somewhat like this.

Code:
aaaa
ccc
aa
linux
browse = no
xssxw
cdcedc
dcsdcd
csdw
police
dwed
dwd
browse = no


cdecec
dsdc
cdc
wd

wdcew
dwed
market
browse = yes

dwdw
wedwe
we
I would like to search word police (which is in middle of file)
and then change the line from browse = no to browse = yes which is after two lines from the word police.

Pls help... I am not so advanced in scripting.

Thanks in advance,
Vikas
Reply With Quote
Forum Sponsor
  #2  
Old 02-13-2008
otheus's Avatar
Moderator ala Mode
 

Join Date: Feb 2007
Location: Innsbruck, Austria
Posts: 605
Quote:
Originally Posted by vikas027 View Post
Hi all,

I would like to search word police (which is in middle of file)
and then change the line from browse = no to browse = yes which is after two lines from the word police.
Code:
awk '/^police$/ { browse=1; } browse==1 && /^browse/ { print "browse = yes"; browse=0; next; } 1'  <infile >outfile
So search for "police"; when found, change the "state". If in this special state, and the line starts with "browse", print out our special version instead and skip to the next line (skipping the next step). The trailing 1 is the next step which always prints out the current line.
Reply With Quote
  #3  
Old 02-13-2008
Registered User
 

Join Date: Oct 2007
Posts: 65
Wink

Quote:
Originally Posted by otheus View Post
Code:
awk '/^police$/ { browse=1; } browse==1 && /^browse/ { print "browse = yes"; browse=0; next; } 1'  <infile >outfile
So search for "police"; when found, change the "state". If in this special state, and the line starts with "browse", print out our special version instead and skip to the next line (skipping the next step). The trailing 1 is the next step which always prints out the current line.


thanks a lot,
will try this code & revert.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 06:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0