LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This 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


Reply
  Search this Thread
Old 10-21-2021, 01:55 PM   #31
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51

Quote:
Originally Posted by shruggy View Post
Right.

In Vim parlance, that would be
Code:
:g/.\{81,}/s/.\{72}\S\{,8}/&\r\t/g
I left this command waiting, for a few newer posts, because ":g" is something i do not yet know.

Anyway, it does not work. The result is what i already said in #32.

Last edited by dedec0; 10-21-2021 at 01:56 PM.
 
Old 10-21-2021, 02:05 PM   #32
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Post Edited 2021/10/21 16:07 UTC-3

Quote:
Originally Posted by shruggy View Post
OK. Then how about
Code:
%:s/\v.{10}\S{,4}($)@!/&\r/g

[ typo in the first 3 symbols, which should have
  been ":%s" instead: ]

:%s/\v.{10}\S{,4}($)@!/&\r/g
No indent, and a few other issues. I said indentation is wanted, when i wrote the algorithm.

Result:

Code:
1234567890
 234567890
 234567890
1234567890
 234567890
 234567890
12345678901234
567890 2345678
90
12345678901234
567890 2345678
90
12345678901234
567890 2345678
90
33 66 999
33 66 999
33 66 999
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.

Last edited by dedec0; 10-25-2021 at 07:25 PM.
 
Old 10-21-2021, 02:08 PM   #33
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
because ":g" is something i do not yet know.
Zintz tutorial has a good chapter on it. And Vim help, of course.

Quote:
No indent
Then add indent
Code:
:%s/\v.{10}\S{,4}($)@!/&\r /g

Last edited by shruggy; 10-21-2021 at 03:14 PM. Reason: Corrected %: to :%
 
1 members found this post helpful.
Old 10-21-2021, 02:34 PM   #34
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Question Why your command is "%:s" instead of "%s"?

Quote:
Originally Posted by shruggy View Post
Zintz tutorial has a good chapter on it. And Vim help, of course.


Then add indent
Code:
%:s/\v.{10}\S{,4}($)@!/&\r /g
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
Code:
:%:s/\v.{10}\S{,4}($)@!/&\r /g
, with one more ":".
 
Old 10-21-2021, 02:44 PM   #35
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
Then add indent
Code:
%:s/\v.{10}\S{,4}($)@!/&\r /g
The other issues continue. So, another soso solution.

File:

Code:
1234567890 234567890 234567890
1234567890 234567890 234567890
12345678901234567890 234567890
12345678901234567890 234567890
12345678901234567890 234567890
33 66 999
33 66 999
33 66 999
Command:
Code:
:%:s/\v.{10}\S{,4}($)@!/&\r /g
(note: "\v" here is just to avoid typing several backslahes?)

Result:

Code:
1234567890
  234567890
  234567890
1234567890
  234567890
  234567890
12345678901234
 567890 2345678
 90
12345678901234
 567890 2345678
 90
12345678901234
 567890 2345678
 90
33 66 999
33 66 999
33 66 999
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.
 
Old 10-21-2021, 02:54 PM   #36
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
(note: "\v" here is just to avoid typing several backslahes?)
Yep.

Quote:
Originally Posted by dedec0 View Post
And there is a strange difference in the size of resulting lines, depending on the reason they are splitted.
That one is easy: don't include the trailing white space
Code:
:%s/\v(.{10}\S{,4})\s*($)@!/\1\r /g
 
1 members found this post helpful.
Old 10-21-2021, 02:59 PM   #37
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Question 'sed' expression addresses question

Quote:
Originally Posted by shruggy View Post
See 4.1 Addresses overview in the GNU sed manual.
Quote:
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?
 
Old 10-21-2021, 03:01 PM   #38
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
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' "?
It means from line 14 to the first line after it containing 'apple'. After it is important. 'apple' in the line 14 itself won't count
Code:
seq -f'%2.f hello' 15|sed '14s/$/ apple/'|sed 14,/apple/s/hello/world/

Last edited by shruggy; 10-21-2021 at 04:59 PM.
 
1 members found this post helpful.
Old 10-21-2021, 03:13 PM   #39
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
Why your command is "%:s" instead of "%s"?
Just a typo. Corrected.
 
Old 10-21-2021, 03:29 PM   #40
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Post

Quote:
Originally Posted by shruggy View Post
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:

File:

Code:
1234567890 234567890 234567890 234567890 234567890 234567890
1234567890 234567890 234567890 234567890 234567890 234567890
12345678901234567890 234567890 234567890 234567890 234567890
12345678901234567890 234567890 234567890 234567890 234567890
12345678901234567890 234567890 234567890 234567890 234567890
33 66 999
33 66 999
33 66 999
Command:
Code:
:%s/\v(.{10}\S{,4})\s*($)@!/\1\r /g
Result:

Code:
1234567890
 234567890 2345
 67890 23456789
 0 234567890
 234567890
1234567890
 234567890 2345
 67890 23456789
 0 234567890
 234567890
12345678901234
 567890 2345678
 90 234567890
 234567890 2345
 67890
12345678901234
 567890 2345678
 90 234567890
 234567890 2345
 67890
12345678901234
 567890 2345678
 90 234567890
 234567890 2345
 67890
33 66 999
33 66 999
33 66 999
But the same command, for this other test file, does something wrong. I do not understand why. File:

Code:
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23
Result:

Code:
00 01 02 03
 04 05 06 07
 08 09 10 11
 12 13 14 15
 16 17 18 19
 20 21 22 2
 3
00 01 02 03
 04 05 06 07
 08 09 10 11
 12 13 14 15
 16 17 18 19
 20 21 22 2
 3
00 01 02 03
 04 05 06 07
 08 09 10 11
 12 13 14 15
 16 17 18 19
 20 21 22 2
 3
00 01 02 03
 04 05 06 07
 08 09 10 11
 12 13 14 15
 16 17 18 19
 20 21 22 2
 3
And i discovered that this command can give bad results if there is a space in the end of input lines. So, it is better to remove them, before using.

Last edited by dedec0; 10-21-2021 at 03:35 PM.
 
Old 10-21-2021, 03:56 PM   #41
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,670

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
not spaces, 4 at most, as least as possible
as many as possible. This is what greedy means.

Quote:
Originally Posted by dedec0 View Post
But the same command, for this other test file, does something wrong. I do not understand why.
...
Result:
Code:
00 01 02 03
 04 05 06 07
 08 09 10 11
 12 13 14 15
 16 17 18 19
 20 21 22 2
 3
Because of the "not EOL" condition. On the last iteration for each line, .{10} will match the ten characters in
Code:
20 21 22 23
\S{,4}($)@! will try to match up to four next characters unless they are followed by EOL. But 3 is followed by EOL, so it's unmatched.
 
1 members found this post helpful.
Old 10-21-2021, 10:56 PM   #42
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
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.
 
Old 10-22-2021, 04:41 PM   #43
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by chrism01 View Post
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.
 
Old 10-25-2021, 01:02 AM   #44
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,897

Rep: Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318Reputation: 7318
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.
 
Old 10-25-2021, 09:00 AM   #45
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,374

Rep: Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754Reputation: 2754
There is no _single_ correct search/replace expression for this issue.
It can be done with sed, but requires multiple search/replace expressions.

File:
Code:
1234567890 234567890 234567890
1234567890 234567890 234567890
1234567890 234567890 234567890
12345678901234567890 234567890
12345678901234567890 234567890
12345678901234567890 234567890
33 66 999
33 66 999
33 66 999
Then
Code:
sed -E 's/^([^\t]{10})\s(.{9})\s(.*$)/\1\n\t\2\n\t\3/;s/^([^\t]{14})(.*)\s(.*$)/\1\n\t\2\n\t\3/' input.txt
produces
Code:
1234567890
        234567890
        234567890
1234567890
        234567890
        234567890
1234567890
        234567890
        234567890
12345678901234
        567890
        234567890
12345678901234
        567890
        234567890
12345678901234
        567890
        234567890
33 66 999
33 66 999
33 66 999
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.

Last edited by allend; 10-25-2021 at 09:07 AM.
 
1 members found this post helpful.
  


Reply

Tags
vim



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
Vim question, .file.txt.un~ is it created by vim or notepad++? cola Linux - General 2 09-07-2019 08:01 AM
[SOLVED] Fedora shows 'man vim' but when execute 'vim' got "bash: vim: command not found..." ? flash_os Linux - Newbie 19 01-03-2015 11:56 PM
Switching from vim to vim -g from inside vim iDragoon Linux - Software 4 05-15-2009 11:46 AM
Editor comparison: vim VS vim-lite, Cleaning vim Ruler2112 *BSD 4 04-13-2009 04:26 PM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 05:47 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 12:43 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration