LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
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, 09:36 AM   #16
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled

Quote:
Originally Posted by dedec0 View Post
I know regex well, for vim and a few other programs. But for sed, it is sometimes strange to use. I never try it, unless it is something ready to run, or with changes i understand how to do. Can you show something to me?
sed is basically the same:
Code:
sed -E 's/.{72}\S{,8}/&\n/g' your_file
With indentation and guarding against short lines that would be
Code:
sed -E '/.{81,}/s/.{72}\S{,8}/&\n\t/g' your_file

Last edited by shruggy; 10-21-2021 at 09:49 AM.
 
Old 10-21-2021, 09:48 AM   #17
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
discard
 
Old 10-21-2021, 09:50 AM   #18
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
Yep. 72 characters is the soft limit. Or what the fmt man page calls goal. The splitting occurs after each 72 characters plus 0 to 8 non-spacing characters. So 80 characters is the hard limit. The {,8} quantifier is greedy, so it will try to consume as many characters as it can (up to 8). As soon as there's a white space among those 8 characters, the RE fires and splitting occurs. If there's no white space in that range, the splitting occurs after the 80th character anyway.
I am still not sure i understand it. Does this splitting happens only from 73 to 80 spaces, or before 81 if there was no space in this interval?
 
Old 10-21-2021, 09:58 AM   #19
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
Does this splitting happens only from 73 to 80 spaces, or before 81 if there was no space in this interval?
The splitting happens before the first white space after the 72th character, or before the 81th character if there was no white space in that interval.
 
Old 10-21-2021, 10:01 AM   #20
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
sed is basically the same:
Code:
sed -E 's/.{72}\S{,8}/&\n/g' your_file
With indentation and guarding against short lines that would be

Code:
sed -E '/.{81,}/s/.{72}\S{,8}/&\n\t/g' your_file
The first command is pretty much what is written in Vim - just the magic/nonmagic changes, right?

But can you explain me the second command? Or i will have to study sed manual and/or something else about it. A pointer is welcome.
 
Old 10-21-2021, 10:05 AM   #21
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
The first command is pretty much what is written in Vim - just the magic/nonmagic changes, right?
Right.
Quote:
Originally Posted by dedec0 View Post
But can you explain me the second command? Or i will have to study sed manual and/or something else about it. A pointer is welcome.
In Vim parlance, that would be
Code:
:g/.\{81,}/s/.\{72}\S\{,8}/&\r\t/g

Last edited by shruggy; 10-21-2021 at 11:20 AM.
 
1 members found this post helpful.
Old 10-21-2021, 10:06 AM   #22
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
The splitting happens before the first white space after the 72th character, or before the 81th character if there was no white space in that interval.
If i understand you right, this is not my idea.

Considering your limits, but my wish: it should be in the *last* space after 72th, but before 80th; or before the 81th character.

With my limits: the *last* space before 80th character; or before the 81th character (which means no space was found until it).
 
Old 10-21-2021, 10:17 AM   #23
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
Or i will have to study sed manual and/or something else about it. A pointer is welcome.
See 4.1 Addresses overview in the GNU sed manual.

Last edited by shruggy; 10-21-2021 at 01:16 PM.
 
1 members found this post helpful.
Old 10-21-2021, 10:17 AM   #24
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
Or i will have to study sed manual and/or something else about it. A pointer is welcome.
See 4.1 Addresses overview in the GNU sed manual.

Last edited by shruggy; 10-21-2021 at 01:16 PM.
 
1 members found this post helpful.
Old 10-21-2021, 10:38 AM   #25
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
With my limits: the *last* space before 80th character; or before the 81th character (which means no space was found until it).
Like this?
Code:
sed -E 's/.{,79}\s|\S{80}/&\n/g'
 
1 members found this post helpful.
Old 10-21-2021, 01:13 PM   #26
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
Like this?
Code:
sed -E 's/.{,79}\s|\S{80}/&\n/g'
Exactly what i was thinking about writing, after seeing your command and explanation. Simpler than most things we have seen here. But i kind of worried if it would work, since any of you wrote that regex (in vim or not).

There is a problem with this: it will split lines shorter than N, if these lines have a space:

1. File:

Code:
1234567890 234567890 234567890
1234567890 234567890 234567890
12345678901234567890 234567890
12345678901234567890 234567890
12345678901234567890 234567890
33 66 999
33 66 999
33 66 999
2. Command:
Code:
:%!sed -E 's/.{,13}\s|\S{14}/&\n/g'
3. Result:

Code:
1234567890 
234567890 
234567890
1234567890 
234567890 
234567890
12345678901234
567890 
234567890
12345678901234
567890 
234567890
12345678901234
567890 
234567890
33 66 
999
33 66 
999
33 66 
999

Last edited by dedec0; 10-21-2021 at 01:16 PM.
 
Old 10-21-2021, 01:21 PM   #27
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
Quote:
Originally Posted by dedec0 View Post
There is a problem with this: it will split lines shorter than N, if these lines have a space:
Because that was your requirement. Compare
Quote:
Originally Posted by dedec0 View Post
Considering your limits, but my wish: it should be in the *last* space after 72th, but before 80th; or before the 81th character.
Quote:
Originally Posted by dedec0 View Post
With my limits: the *last* space before 80th character; or before the 81th character (which means no space was found until it).
So I did it to your limits. I understand your limits thusly: the *last* space before 80th character, even if it is the 2nd character in the line.
 
Old 10-21-2021, 01:28 PM   #28
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
sed is basically the same:
Code:
sed -E 's/.{72}\S{,8}/&\n/g' your_file
With indentation and guarding against short lines that would be
Code:
sed -E '/.{81,}/s/.{72}\S{,8}/&\n\t/g' your_file
shruggy, i think there are strange things in this regex you wrote. See:

Code:
/.{81,}/s/.{72}\S{,8}/&\n\t/g
^      ^ ^           ^     ^
1      2 3           4     5
My questions:

a. Should 2 be a slash or a backslash?

b. Assuming that 2 is a backslash, why this regex has 3 parts, with 4 slashes separating them, instead of just 2? (this is something i have never seen before, so i did not even bother to search about it)

If there are these mistakes in your messages, i suggest that you edit them. The commands may not solve my problem, but others may find them useful.
 
Old 10-21-2021, 01:39 PM   #29
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled
No, the sed code is correct. It's not one regex, it's a sed command consisting out of a regex address and a substitute command.
Code:
/.{81,}/s/.{72}\S{,8}/&\n\t/g
BTW, have you seen my answer in #21?
 
Old 10-21-2021, 01:42 PM   #30
dedec0
Senior Member
 
Registered: May 2007
Posts: 1,372

Original Poster
Rep: Reputation: 51
Quote:
Originally Posted by shruggy View Post
In Vim parlance, that would be
Code:
:g/.\{81,}/s/.\{72}\S\{,8}/&\r\t/g
New command for me: ":g". I will read its help.

But your suggestion did not work:

1. File, with one line only:

Code:
1234567890 234567890 234567890
2. Command:
Code:
:g/.\{15,}/s/.\{11}\S\{,4}/&\r\t/g
3. Result:

Code:
1234567890 2345
	67890 234567890
	# there is a tab inserted here!
The result is indented, but the indentation make the new line go beyound the demanded limit of 14 chars (in my example). And there is an extra tab, in the end of the file.

People, can you, please, write a sentence or a short paragraph to describe the workings of a regex or some command you send? This can be valuable, i think, and it is not hard.
 
Old 10-21-2021, 01:52 PM   #31
shruggy
Senior Member
 
Registered: Mar 2020
Posts: 3,678

Rep: Reputation: Disabled

OK. Then how about
Code:
%:s/\v.{10}\S{,4}($)@!/&\r/g
 
  


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 04:58 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