0

I'm trying to create a script that will find/replace instances of a string matching:

vc/integer

The file /etc/securetty contains the following strings:

vc/1
vc/2
vc/3
vc/4
vc/5
vc/6
vc/7
vc/8
vc/9
vc/10
vc/11



grep -E 'vc/(\d{0,4}\D)*' /etc/securetty | while read line; do sed -ie "s=$line==g" /etc/securetty; done

As expected, the above command removes all the strings mentioned, but doesn't strip out the second digit, the 0 and the 1, which are a part of the last two strings, vc/10 vc/11. I'm left with a file containing

0
1

How can I modify the command to remove a single and double digit match?

5
  • grep -E 'vc/[0-9]+'.
    – DopeGhoti
    Commented Mar 22, 2017 at 16:40
  • That didn't seem to work. I get the same output.
    – GreNIX
    Commented Mar 22, 2017 at 16:45
  • 1
    Your command is quite convoluted. Why don't you simply delete with sed each line matching your grep pattern? Without grep and while I mean.
    – xhienne
    Commented Mar 22, 2017 at 16:57
  • @xhienne Thanks, I've removed the grep and while pieces from the command.
    – GreNIX
    Commented Mar 22, 2017 at 17:02
  • I'm confused as to what matched the \D (non-digit character) in your nearly successful use shown in your question, especially since \D (or anything to serve in its place) is not included in the answer below, that you indicate works correctly. Commented Sep 26, 2018 at 4:31

1 Answer 1

4

Why don't you just do this simply?

sed -i -e 's|vc/[0-9]\{1,\}||g' /etc/security

Explanation:

The regex [0-9]\{1,\} => 1 or more matches of the preceding atom, which in your case happens to be [0-9]. Note that \d+ although equivalent to what I've given [0-9]\{1,\}, is not POSIX sed compliant. In the same vein, note that [0-9]\{0,\} is equivalent to \d*.

1
  • That worked! Could you please give me some info on how that command is working? Specifically, the \{1,\} part.
    – GreNIX
    Commented Mar 22, 2017 at 16:53

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .