0

How to move the the specified string (starting with math) to specific position(20th column) using sed (with regex). Want to move the string starting with math to column 20 in every line and math string always in the last word of the line.

how are you math123 
good math234
try this math500 
  • what is "column" in your case? I don't see 20 columns in your input – RomanPerekhrest May 18 '17 at 21:22
  • For instance, starting math123 from column(position) 20 in line1. how are you takes 12 columns and math should start at 20th column in line1. So 13th column to 19th columns are spaces. – spiderman May 18 '17 at 21:33
  • What if "math" comes later than the 20th column in the input? Leave it where it is? – Wildcard May 19 '17 at 1:40
3

If you really must use sed, then a possible algorithm would be to keep adding spaces in front of string math so long as there are 18 or fewer preceding characters:

$ sed -e :a -e 's/\(^.\{,18\}\)math/\1 math/; ta' file
how are you        math123 
good               math234
try this           math500 

If you want to move only the last occurrence of the string, then you can anchor it to the end of the line. For example, given something like

$ cat file
how are you math123
good math234
try this math500
math101 is enough math

then provided there is no trailing whitespace

$ sed -e :a -e 's/^\(.\{,18\}\)\(math[^[:space:]]*\)$/\1 \2/; ta' file
how are you        math123
good               math234
try this           math500
math101 is enough  math

If your sed has an extended regular expression mode, you can simplify to

sed -E -e :a -e 's/^(.{,18})(math[^[:space:]]*)$/\1 \2/; ta'
  • Nicely done! ... – John1024 May 18 '17 at 22:44
  • It works, but want to move only last match of math in that line. If there are more than one math string match in that line, it should move only last word math. how math1 math2 --> how math1#########math2 (# represents spaces) – spiderman May 18 '17 at 22:49
2

While sed is not good at math, awk excels at it:

$ awk -Fmath '{printf "%-20smath%s\n",$1,$2}' file
how are you         math123 
good                math234
try this            math500 

This code may not correctly handle possible corner cases but it will get you started.

0
perl -pe 's/(?=math)/" " x (19-length($`))/e'      yourfile

perl -pe 's// / while /.*\K(?=math)/g && 19 > pos' yourfile

Working

  • Perl option -p will setup an implicit file read in loop on a line-by-line basis. The current record, aka, line, is stored in $_ variable.
  • The while loop is doing the following:
    • a) /.*\K(?=math)/g operates on the current line, $_ and the regex looks for the position, standing where, just to it's right can be string "math" and to it's left is anything.
      • b) Upon the success of the regex, next thing that is checked is that the position is less than 19. Otherwise, break out of the while loop.
      • c) The body of the while loop adds a space at the position that was determined in the while loop action statement.

Results

         1         2         3
123456789012345678901234567890
good               math234
how are you        math123
1234567890
good               math234
try this           math500
math101 is enough  math

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.