Using with vim substitute command: lines created are not scanned in future steps. Can they be?
Linux - SoftwareThis forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.
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.
I carefully created example files to make tests easy, but it seems the only person testing commands with them is me. ):
"A hot thread"...
Anyway, although it seems i left a post or another pass by unnoticed, this is just because i am testing them (and frequently correcting them to work) with my test file. This takes some minutes, at times.
Why your command is "%:s" instead of "%s"? And surprisingly, it works (it does something, i mean... not evaluating the result, for this thread). /-: Of course, the command seeing in vim history is
Notice the result lines longer than N (N=10 here). Fine... we just adjust the N to be a bit different. And there is a strange difference in the size of resulting lines, depending on the reason they are splitted.
An address range is specified with two addresses separated by a comma (,). Addresses can be numeric, regular expressions, or a mix of both. The following command replaces the word ‘hello’ with ‘world’ only in lines 4 to 17 (inclusive):
Code:
sed '4,17s/hello/world/' input.txt > output.txt
"Addresses can be numeric, regular expressions, or a mix of both."
So, if i try
Code:
cat input |sed '14,/apple/s/hello/world/'
what does it mean? Is it just "from line 14 to the last line containing 'apple' "? Or will it also count intervals (from lines before 14 that contain "apple") in them, until line 14?
Do you think the documentation can be improved by adding this information?
That one is easy: don't include the trailing white space
Code:
:%s/\v(.{10}\S{,4})\s*($)@!/\1\r /g
"(anything, including spaces, 10 times; not spaces, 4 at most, as least as possible); zero or more spaces; not EOL" -> substitute with the first group; newline.
This seems to be something we (or i) have done before. But it works for some cases:
I have qn; is this just going to be a one time thing(?) in which case i guess a vim cmd will be fine, if you can find one...
If you are going to need to do this > 5 (for example) times, frankly I'd write a short program to do it, which would then be repeatable, easily changed and not prone to fat fingering...
Personally I'd use Perl, but others here would likely use eg awk or or python.
I have qn; is this just going to be a one time thing(?) in which case i guess a vim cmd will be fine, if you can find one...
If you are going to need to do this > 5 (for example) times, frankly I'd write a short program to do it, which would then be repeatable, easily changed and not prone to fat fingering...
Personally I'd use Perl, but others here would likely use eg awk or or python.
No. Not just one time. It will probably have a fair frequency. My most usual plan, to use it, is to format terminal sessions for fora threads or for mailing lists. But understanding what the command does, and how it works, i do not mind rewriting it every time. It will be natural, like using regexes are, today, for me.
I still think it is not a job for sed (because there is no correct search/replace expression for this issue).
That's why I suggested fold or the python textwrap module (or something similar) which can do it perfectly without rewriting, both from command line and from editor.
Breaking that down a bit:
"s/^(.{10})\s(.{9})\s(.*$)/\1\n\t\2\n\t\3/" targets the first three lines in the file, grouping the non-space characters into three for use as back references.
"s/^([^\t]{14})(.*)\s(.*$)/\1\n\t\2\n\t\3/" targets the second three lines in the file, grouping into three the first 14 characters, then the characters up to 'space' and then remaining characters to end of line for use as back references.
(The third three lines do not require processing and so are not targeted.)
The back references are output with new lines and tab characters to provide indentation as required.
PS - Of course this code breaks for other examples suggested by the OP.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.