1

I've tried to removed the binary columns in my octal help function.
First echo is in line 70, I wanted to delete |000 and the same pattern .. first | + three numerals for all echo lines.
So I have tried this:

  • :70,77s/*|[01][01][01]*/
  • :70,77s/*|[0-1][0-1][0-1]*/
  • :70,77s/*|[0..1][0..1][0..1]*/

Outcome for all three was E486: Pattern not found.
Do I have to escape something here or what did I do wrong?

# octal will show octal permission scheme for chmod command
function octal {
        echo "|0|000|---|";
        echo "|1|001|--x|";
        echo "|2|010|-w-|";
        echo "|3|011|-wx|";
        echo "|4|100|r--|";
        echo "|5|101|r-x|";
        echo "|6|110|rw-|";
        echo "|7|111|rwx|";
        echo 'Example: chmod 777 file = -rwxrwxrwx';
        echo 'Example: chmod 600 file = -rw-------';
}
5
  • I've also tried 70,77s/*|???*/ … same result. Commented Jan 2, 2021 at 22:46
  • what you want to do ? just remove the octals (and leave : || instead of |oct| ?) or something more? please show a before / after Commented Jan 2, 2021 at 22:51
  • before: echo "|0|000|---|"; … after: echo "|0|---|"; Commented Jan 2, 2021 at 22:53
  • Thanks, I updated my answer. and just now corrected it. Commented Jan 2, 2021 at 22:55
  • 1
    note: * is not a wildcard. in regex it would be : .*, ie "any character" (.) "repeated 0,1 or n times" (*), ie a string of any length (and any content). * alone is often taken as a literral *, unless it is preceded with something. cars* : will match: car cars carsssssssss etc, for exemple. but *cars will match any line containing *cars in it, literrally (* being the first character, it is taken literrally as it can't "repeat 0, 1 or n times" something before it) Commented Jan 2, 2021 at 23:05

1 Answer 1

2

If you insist of doing it in vim:

If you want to just delete the octals, and the following pipe, and nothing more: delete the first occurence of 3 0to7 digits between 2 pipes:

:70,77s/|[01]\{3}|/|/ 

edit : updated according to the exemple given

12
  • 1
    @Heiko : careful, your last comment also would match a |010abcde , and change this into abcde .. better add the 2 "limits" (the | on either sides) to ensure only 3 occurences between 2 pipes are matched, and not something starting with 3 occurences instead. Commented Jan 2, 2021 at 23:12
  • 1
    note also: each program (vim, python, grep, awk, etc) use one or another variant of regular expression matching. For exemple : in grep or sed, you may have to change any literral "|" with "\|", so that it is not taked as an "or" ( ex: egrep "foo|bar" matches any lines containing "foo" or "bar" ... egrep "foo\|bar" will instead match lines containing "foo|bar" ). Experiment (and there are great sites explaining it all, such as regular-expressions.info) Commented Jan 2, 2021 at 23:14
  • 1
    @Heiko : in shell command line: * matching is called "globbing", and follows different rules than regexes. And as noted above, there are also many variant of regexes. Practice will make it all make sense ^^. Commented Jan 2, 2021 at 23:23
  • 1
    @Heiko: in shell: you can do : for file in a*b ; do something with "$file" ; done (will be filling the file variable with any file starting with a and finishing with b in the current directory, and each time execute : something with "$file" ), or also : for file in a*[bcd] ; do ... (any files starting with a, and ending in b, c or d). in regex: ^a.*b$ and ^a.*[bcd]$ would be "close" approximations (but now it is usually used to match against a text string, not a file or directory name). Commented Jan 2, 2021 at 23:27
  • 1
    and "normal" shells do not use regex unless under very specific circumstances. They usually do "globbing" instead against filenames inthe current directory in general. Commented Jan 2, 2021 at 23:31

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.