Search code examples
awksedfindstr

Filter file with findstr or awk and sed


I am trying to filter this file based on the backslash before a semicolon, for example filter the file below to obtain the lines that contain up to six backslashes before a semicolon

    \\Filehomeserver\homefolder\eduardo\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI (0x03);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\Thumbs.db;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:IA (0x10);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:IA (0x10)
    \\Filehomeserver\homefolder\eduardo\My Documents\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\My Documents\SametimeChatHistory\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\Notes ID file\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\Notes ID file\eduardo.id;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:IA (0x10);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:IA (0x10)

Desired output

    \\Filehomeserver\homefolder\eduardo\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI (0x03);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\Thumbs.db;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:IA (0x10);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:IA (0x10)
    \\Filehomeserver\homefolder\eduardo\My Documents\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)

I tried to run

    findstr /r "\\\\.*\\.*\\.*\\;" 

    \\Filehomeserver\homefolder\eduardo\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI (0x03);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\My Documents\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\My Documents\SametimeChatHistory\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
    \\Filehomeserver\homefolder\eduardo\Notes ID file\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)

But it doesn't work, it brings more lines than what I wanted, if anyone has any other idea with either windows findstr or awk / sed I will appreciate it


Solution

  • To show lines with six backslashes only before first semicolon. Change the == in the if statement to <= if need to keep all lines less than or equal to 6 before the semicolon.

    $ awk -F';'  '{if (split($1,a,"\\") == 7) print $0;}' test
        \\Filehomeserver\homefolder\eduardo\My Documents\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
        \\Filehomeserver\homefolder\eduardo\Notes ID file\;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:OI,CI,IA (0x13);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:OI,CI,IA (0x13)
        \\Filehomeserver\homefolder\eduardo\Notes ID file\eduardo.id;USDOM\eduardo : Allowed CHGE:EWXD (0x001301BF) Flags:IA (0x10);USDOM\sec_&Conf_group: Allowed FULL:EWXPOD (0x001F01FF) Flags:IA (0x10)
    

    As pointed out by @Ed Morton in the comments below this can be simplified to just

    awk -F';'  'split($1,a,/\\/) <= 7' test