-2

I try to use sed or whatever to replace a number of a line /opt/tin/toss to become a *

For example

37 7 * * * /opt/tin/toss
43 7 * * * /opt/tin/te1
58 7 * * * /opt/tin/test2

Output:

37 * * * * /opt/tin/toss
43 7 * * * /opt/tin/te1
58 7 * * * /opt/tin/test2

Normally, 7 generates randomly, It can be 2, 4 or 8, running from 0-24.

  • Replace whole line using sed. – Prvt_Yadv Mar 24 at 4:40
  • I don't understand what you want. I guess you want to modify the line that contains /opt/tin/toss. What change do you want to make? Do you want to change the second field, the second number, the third digit, or what? What do you mean by "7 generates randomly, It can be 2, 4 or 8, running from 0-24"? – G-Man Mar 24 at 4:51
  • yes, I want to modify the line contains /opt/tin/toss. Number 7 is not static number. Sometimes it changes to 8 or 12... I want to change 7 to * – Quang Ngô Mar 24 at 5:18
0
sed '/\/opt\/tin\/toss/ s/^\([^ ]* \+\)[^ ]*\( .*\)$/\1*\2/'

You'd read this as,

Where there is /opt/tin/toss, substitute the pattern that matches

^ starting of line followed by

\([^ ]* \+\)

first group \( of [^ ]* any number characters until one or more spaces \+, and closing of group \)

[^ ]*

followed by [^ ]* anything until space

\( .*\)

followed by second group \( .*\)$ matching space followed by anything until end

with

/\1*\2/

first group followed by * followed by second group.

-1

A example of sed that will so

sed  's/^\([0-9]*\) [0-9]*  *\(\*  *\* *\*  *\/opt\/tin\/toss.*\)$/\1 * \2/'

this \([0-9]*\) will match for the first digit an keep the value in \1

this \(\* *\* *\* *\/opt\/tin\/toss.*\)will match for the last 3 stars ond the command line , and keep the value in \1

  • This assumes there are * at every other location where there is /opt/tin/toss, would not work in case of 37-39/2 7 2 * * /opt/tin/toss – GypsyCosmonaut Mar 24 at 5:57

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.