LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 02-13-2008, 08:53 AM   #1
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Rep: Reputation: 99
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
 
Old 02-13-2008, 09:09 AM   #2
radoulov
Member
 
Registered: Apr 2007
Location: Milano, Italia/Варна, България
Distribution: Ubuntu, Open SUSE
Posts: 212

Rep: Reputation: 35
Code:
sed '/police/{n;n;n;s/no/yes/;}' filename
 
Old 02-13-2008, 02:23 PM   #3
vikas027
Senior Member
 
Registered: May 2007
Location: Sydney
Distribution: RHEL, CentOS, Debian, OS X
Posts: 1,266

Original Poster
Rep: Reputation: 99
Wink

Quote:
Originally Posted by radoulov View Post
Code:
sed '/police/{n;n;n;s/no/yes/;}' filename

thanks a lot,
will try this code & revert.
 
Old 02-14-2008, 01:59 AM   #4
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 623

Rep: Reputation: 364Reputation: 364Reputation: 364Reputation: 364
Hello.

You can try following ed script:
Code:
/police/
/browse/
s/no/yes
wq
Save it to file or generate using bash script and run:
Code:
ed filename < script
 
Old 02-14-2008, 12:55 PM   #5
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
The OP wasn’t clear whether the line “browse = no” is always three lines after the original word. If not (i.e., if the number of lines is variable), radoulov’s script might not work.

You can, use sed, though (if you want):
Code:
sed '/police/{:n;n;s/no/yes/;t;bn}' filename
 
Old 02-14-2008, 01:41 PM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by osor View Post
The OP wasn’t clear whether the line “browse = no” is always three lines after the original word. If not (i.e., if the number of lines is variable), radoulov’s script might not work.

You can, use sed, though (if you want):
Code:
sed '/police/{:n;n;s/no/yes/;t;bn}' filename
If I am not mistaken, this is a loop in sed. (Don't see that too often...)

Does the "t" with no label take you out of the loop?
The way I read this, once it finds "police", it loops until in finds the first instance of "no". It then continues parsing looking for the next instance of "police". Any instance of "police" before a "no" will get ignored.
 
Old 02-14-2008, 04:50 PM   #7
osor
HCL Maintainer
 
Registered: Jan 2006
Distribution: (H)LFS, Gentoo
Posts: 2,450

Rep: Reputation: 70
Quote:
Originally Posted by pixellany View Post
Does the "t" with no label take you out of the loop?
Yes, t ends the cycle (or equivalently, jumps to the end) if the substitution succeeded. The reason for the “t;bn” construct is the lack of a “jne” operation in sed (if you are familiar with assembly). So you construct an artificial jne by using a je (i.e., t) followed by a jmp (i.e., b), where the target of the je is the instruction after your jmp (in this case, the target happens to be the end, so you don’t need an explicit label).
Quote:
Originally Posted by pixellany View Post
Any instance of "police" before a "no" will get ignored.
I think you mean any instance of “no” before a “police” will get ignored.
 
Old 02-14-2008, 07:51 PM   #8
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by osor View Post
I think you mean any instance of “no” before a “police” will get ignored.
No--I meant it the way I wrote it. I am assuming that, after finding "police", it then loops looking for a "no". If there is another "police" before finding "no", then I am guessing that it will not look for "no" after (because it is stuck in the loop looking for "no".
 
Old 02-14-2008, 07:57 PM   #9
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
From the structure of the example, it looks like the "browse=no" ends the paragraph.

sed '/police/,/^$/s/browse = no/browse = yes/' data

I'll do something similar to select a device from an lspci -v list:
Code:
sudo /sbin/lspci -v | sed -n '/FireWire/,/^$/p'
root's password:
03:05.0 FireWire (IEEE 1394): Agere Systems FW323 (rev 61) (prog-if 10 [OHCI])
        Subsystem: Hewlett-Packard Company Unknown device 2a34
        Flags: bus master, medium devsel, latency 32, IRQ 19
        Memory at fddff000 (32-bit, non-prefetchable) [size=4K]
        Capabilities: [44] Power Management version 2
Breaking up sections of output with blank lines make it easy to process with sed or awk.

Last edited by jschiwal; 02-14-2008 at 08:04 PM.
 
Old 02-14-2008, 08:20 PM   #10
sundialsvcs
Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 5,348

Rep: Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105Reputation: 1105
Another useful tool to consider for such applications is awk.

This is a tool that "takes sed to the next level." Like most power-tools, the concept behind this tool is disarmingly simple:
  1. The "program" consists of one or more regular-expressions ("text patterns"...) followed by blocks of executable code, expressed in a somewhat C-like language. There are also two special patterns, BEGIN (executed before the first line) and END (executed after the last one).
  2. awk takes each "line" (as defined by you...) and considers each regular-expression in turn. When it finds one, it executes the corresponding code-block.
  3. It can be "that simple" or, if your needs require, much more elaborate.
What this gives you, then, is a very powerful and generalized solution for problems like the one you are dealing with. awk is great for "grabbing all kinds of things out of text-files and manipulating them in some useful way."

The perl programming-language sort of "started where awk ended, and then kept on going well into the next galaxy." But that's another story.

The bottom line is that Unix/Linux environments have "an embarrassment of riches" in terms of very practical programming tools. It's well worth getting to know them.
 
Old 02-14-2008, 09:46 PM   #11
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
@OP, if you have Python
Code:
!#/usr/bin/python
data=open("file").readlines()
for n,items in enumerate(data):
    if "police" in items:
        data[n+3] = data[n+3].replace("no","yes")
print ''.join(data)
if you prefer awk, one way
Code:
awk '/police/{line=NR+3;} 
NR == line { sub("no","yes"); }1
' file
 
  


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
LXer: See changes word by word with dwdiff LXer Syndicated Linux News 0 09-25-2006 07:54 AM
how to change a word in a text file EXnihilo Linux - General 11 09-11-2006 08:07 AM
Microsoft Word won't word wrap Micro420 General 1 06-13-2005 04:36 PM
Viewers for Word, Word Perfect, etc. Claude Williams Linux - General 2 11-22-2001 04:01 AM
How 2 find a duplicate word in a text file cowardnewbie Programming 1 09-16-2001 11:57 PM


All times are GMT -5. The time now is 10:28 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