Sed removes the remaining characters in the string except the first 5

so that the sed command removes all characters in the string except the first 5 leading characters using sed? I tried to go back (back), but this is not the most elegant solution.

+6
source share
2 answers

This may work for you (GNU sed):

echo '1234567890' | sed 's/.//6g' 12345 

Or:

 echo '1234567890' | cut -c-5 12345 
+8
source

Try this (takes 5 repetitions of the character "any" at the beginning of the line and saves it in the first group, then randomly repeat any characters and replace the line with the first group):

 sed 's/^\(.\{5\}\).*/\1/' 

Or an alternative suggested by mouviciel:

 sed 's/^\(.....\).*/\1/' 

(this is more readable if the number of first characters you want is not too large)

+2
source

Source: https://habr.com/ru/post/916440/


All Articles