Sunday, April 28, 2024
 Popular · Latest · Hot · Upcoming
4
rated 0 times [  4] [ 0]  / answers: 1 / hits: 2288  / 2 Years ago, mon, may 23, 2022, 9:15:15

For example I have this output:


string1 anynameveryveryverylong string2
string1 othernameveryveryverylong string2

I want truncate the name to the first ten characters:


string1 anynamever  string2
string1 othernamev string2

a pseudo regex can be:


perl -pe "s/([^	]+	)([^	]+)	/12{10}	/g"

How do i get this?


More From » bash

 Answers
7
perl -pe 's/^(S+s+)(S{10})S*/$1$2/'


  • ^ matches at the start of the string

  • S means non-whitespace

  • + means repeated at least once

  • s means whitespace

  • {10} means repeated 10 times


I.e. Keep the first word and the first 10 characters of the following word while forgetting the remaining characters of the second word.


Your pseudoregex has one substantial problem: the {10} is placed in the replacement part, but the replacement is just a string. The regex happens in the pattern part only.


[#2095] Monday, May 23, 2022, 2 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
enefiama

Total Points: 43
Total Questions: 125
Total Answers: 102

Location: Gabon
Member since Sat, Jul 25, 2020
4 Years ago
enefiama questions
Sat, Jun 19, 21, 01:27, 3 Years ago
Mon, Aug 8, 22, 03:41, 2 Years ago
Sat, Jul 2, 22, 04:59, 2 Years ago
;