20071213

sed : Remove first 4 letters in each line

sed 's/^.\{4\}//g' test.txt

3 comments:

  1. Hey codept,

    sed 's/^.\{4\}//g' test.txt

    This is not removing first 4 letters, this is to replace first 4 characters as . means single character. And if your line started with things like space, underscore and etc, it will match too as long as it is single character that matching 4 times.

    The more correct way to do this is using

    sed -s 's/^[[:alpha:]]\{4\}//g' file

    Or

    sed -s 's/^[a-zA-Z]\{4\}//g' file

    Now this is really "letter"(and you match whether it is upper or lower case, by the way it is wrong also that you call it as removing first 4 letters in your regex, because ^ is to match start of the line.

    Be careful when building the regex, it can be tricky to get it right sometime.

    Cheers ;]

    ReplyDelete
  2. Thanks geek00l!

    By the way, I just wanted to remove numbers when you view in vim. Since line numbering in vim are right aligned, the smaller numbers are preceded with spaces before the number itself. For this I need to include spaces for removal.

    ReplyDelete
  3. @geek00l:

    Thanks! Exactly what I needed, worked like a charm :)

    Cheers,

    MR

    ReplyDelete