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 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 |
Code:
sed '/police/{n;n;n;s/no/yes/;}' filename |
Quote:
thanks a lot, will try this code & revert. |
Hello.
You can try following ed script: Code:
/police/ Code:
ed filename < script |
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 |
Quote:
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. |
Quote:
Quote:
|
Quote:
|
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' |
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 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. |
@OP, if you have Python
Code:
!#/usr/bin/python Code:
awk '/police/{line=NR+3;} |
All times are GMT -5. The time now is 08:31 AM. |