Monday, August 31, 2009

Re: Use VIM to perform text manipulation on file (like sed/awk)

Hi yosi!

On Mo, 31 Aug 2009, yosi izaq wrote:

> Let me see if I got your script right. You tell sed to work on the
> range from "-c" to first blank line right?
> Then you instruct it to comment the lines that are not already in
> comment in that range (which in this case is also a paragraph).
> It should work well so long as $1 is the first line in the paragraph.

No. Using -c will comment the selected paragraph (that is, from the
line that matches the pattern until the next blank line), without -c
it will uncomment the paragraph:

#v+
chrisbra t41:~/sed [1025]% cat resolv.conf
#Company IT nameservers
nameserver 1.2.3.4
nameserver 1.2.3.5
nameserver 1.2.3.6

Lab1 nameservers
nameserver 10.1.2.3
nameserver 10.1.2.4

#Lab2 nameservers
#nameserver 20.1.2.2

chrisbra t41:~/sed [1026]% ./resolv.sh -c Lab1 resolv.conf; cat resolv.conf
#Company IT nameservers
nameserver 1.2.3.4
nameserver 1.2.3.5
nameserver 1.2.3.6

#Lab1 nameservers
#nameserver 10.1.2.3
#nameserver 10.1.2.4

#Lab2 nameservers
#nameserver 20.1.2.2

chrisbra t41:~/sed [1027]% ./resolv.sh Lab2 resolv.conf; cat resolv.conf
#Company IT nameservers
nameserver 1.2.3.4
nameserver 1.2.3.5
nameserver 1.2.3.6

#Lab1 nameservers
#nameserver 10.1.2.3
#nameserver 10.1.2.4

Lab2 nameservers
nameserver 20.1.2.2
#v-

An even simpler approach would be something like letting sed toggle,
wherever the pattern matches, like this:

#v+
chrisbra t41:~/sed [1067]% cat resolv1.sh
#!/bin/sh

sed -i "
/$1/,/^$/{
s/^[^#]/#&/g;
t
s/^#\+//g;
}" "$2"
chrisbra t41:~/sed [1072]% cat resolv.conf
#Company IT nameservers
nameserver 1.2.3.4
nameserver 1.2.3.5
nameserver 1.2.3.6

#Lab1 nameservers
#nameserver 10.1.2.3
#nameserver 10.1.2.4

Lab2 nameservers
nameserver 20.1.2.2
chrisbra t41:~/sed [1073]% ./resolv1.sh Lab2 resolv.conf ; cat resolv.conf
#Company IT nameservers
nameserver 1.2.3.4
nameserver 1.2.3.5
nameserver 1.2.3.6

#Lab1 nameservers
#nameserver 10.1.2.3
#nameserver 10.1.2.4

#Lab2 nameservers
#nameserver 20.1.2.2
chrisbra t41:~/sed [1074]% ./resolv1.sh Lab2 resolv.conf ; cat resolv.conf
#Company IT nameservers
nameserver 1.2.3.4
nameserver 1.2.3.5
nameserver 1.2.3.6

#Lab1 nameservers
#nameserver 10.1.2.3
#nameserver 10.1.2.4

Lab2 nameservers
nameserver 20.1.2.2
chrisbra t41:~/sed [1074]%
#v-

regards,
Christian
--
Citizens are not allowed to attend a movie house or theater nor ride in a
public streetcar within at least four hours after eating garlic.
[real standing law in Indiana, United States of America]

--~--~---------~--~----~------------~-------~--~----~
You received this message from the "vim_use" maillist.
For more information, visit http://www.vim.org/maillist.php
-~----------~----~----~----~------~----~------~--~---

No comments: