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-------';
}
Heiko
  • 261
  • 3
  • 9
  • I've also tried 70,77s/*|???*/ … same result. – Heiko Jan 02 '21 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 – Olivier Dulac Jan 02 '21 at 22:51
  • before: echo "|0|000|---|"; … after: echo "|0|---|"; – Heiko Jan 02 '21 at 22:53
  • Thanks, I updated my answer. and just now corrected it. – Olivier Dulac Jan 02 '21 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) – Olivier Dulac Jan 02 '21 at 23:05

1 Answers1

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

  • Thank you that worked! But have to state that I don't get it .. I don't need the stars in Vim, they caused the problem, is that right? – Heiko Jan 02 '21 at 23:05
  • Yes, I think I get it now .. 70,77s/|[01][01][01]/ worked as well - that is what I've tried first without the stars. – Heiko Jan 02 '21 at 23:10
  • 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. – Olivier Dulac Jan 02 '21 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 https://www.regular-expressions.info/) – Olivier Dulac Jan 02 '21 at 23:14
  • I have not looked at regex yet .. just passed the bash wildcards chapter in the book I'm following and tried to adapt this in Vim. But thanks, that is very valuable information. I'll pay attention to this and practice it .. so powerful when done correctly. – Heiko Jan 02 '21 at 23:19
  • 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 ^^. – Olivier Dulac Jan 02 '21 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). – Olivier Dulac Jan 02 '21 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. – Olivier Dulac Jan 02 '21 at 23:31
  • @OlivierDulac I've just yesterday learned about wildcards and practiced it. Most complicated I did was ls [![:digit:]]*[a-z][0-9][0-9][0-9][aA-zZ]*[![:digit:]] It's not the easiest stuff to wrap the beginners head around .. guess that will get even worse with regex .. but I'll practice this, it's totally fascinating. – Heiko Jan 02 '21 at 23:37
  • 1
    @Heiko: actually, most of what you used there are also available in regexes. You seem on a good path to learn! For the shell part, I can recommand: wooledge.com website (mostly: the "bashpitfalls" page, the guide, and the faq : all are recommended reading, but maybe in a few days/weeks)) – Olivier Dulac Jan 02 '21 at 23:53
  • https://mywiki.wooledge.org/ – Heiko Jan 03 '21 at 00:56
  • @Heiko thanks a lot for the coffee :) (Its greatly appreciated, even though it was just a little informations). I will delete this comment. – Olivier Dulac Jan 05 '21 at 00:38