[SOLVED] find a word in a file, and change a word beneath it ??
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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.
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
Any instance of "police" before a "no" will get ignored.
I think you mean any instance of “no” before a “police” will get ignored.
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".
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:
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).
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.
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.
!#/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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.