Find the answer to your Linux question:
Results 1 to 9 of 9
I am trying to find a way in bash to delete all lines in a file that contains the backslash (\) . I tried using sed to delete the file ...
  1. #1
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75

    Delete lines that match.

    I am trying to find a way in bash to delete all lines in a file that contains the backslash (\) . I tried using sed to delete the file out with the \\ to delete the lines but this does not appear to work.

    Code:
    sed /"ACC7575 NAS\Backup of files on X$,System State and Services Database\bob"/d
    I am going to be making a script that will read a file full of these lines. I figured sed would be the easy one to do this.

  2. #2
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Try:

    Code:
    grep -v '\\' file
    Regards

  3. #3
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    If you prefer sed:

    Code:
    sed '/\\/d' file
    Regards

  4. #4
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    Quote Originally Posted by Franklin52 View Post
    If you prefer sed:

    Code:
    sed '/\\/d' file
    Regards

    Let me clarify what I meant

    Status:
    Code:
     
    ACCT00251 SERVER01\Backup of files on P$\administrator Thursday
    ACCT00251 DC\Backup of System State \administrator Thursday
    ACCT00251 SUA\Backup of files on D$\administrator Thursday
    ACCT00253 BOB1\Backup of files on C$,D$\admin Thursday
    ACCT00253 MIKE\Backup of Microsoft Exchange Server\admin Thursday
    ACCT00253 RCLS03\Backup of files on C$,D$\admin Thursday
    I have those lines in a file.

    In another file I have a few lines that look like:
    Exclusion:
    Code:
    ACCT00253 MIKE\Backup of Microsoft Exchange Server\backup_admin
    ACCT00253 BOB1\Backup of files on C$,D$\admin
    I want to delete any line in in the exclusion file that matches in the status file. I was trying to make sed delete the lines (I want to match the full line aside from the Day (in the example Thursday). But I was unable to get sed to delete anything because the file has \ and if I add \\ to the exlcude file it will still not delete the file.

  5. #5
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    Quote Originally Posted by Korelis View Post
    Let me clarify what I meant

    Status:
    Code:
     
    ACCT00251 SERVER01\Backup of files on P$\administrator Thursday
    ACCT00251 DC\Backup of System State \administrator Thursday
    ACCT00251 SUA\Backup of files on D$\administrator Thursday
    ACCT00253 BOB1\Backup of files on C$,D$\admin Thursday
    ACCT00253 MIKE\Backup of Microsoft Exchange Server\admin Thursday
    ACCT00253 RCLS03\Backup of files on C$,D$\admin Thursday
    I have those lines in a file.

    In another file I have a few lines that look like:
    Exclusion:
    Code:
    ACCT00253 MIKE\Backup of Microsoft Exchange Server\backup_admin
    ACCT00253 BOB1\Backup of files on C$,D$\admin
    I want to delete any line in in the exclusion file that matches in the status file. I was trying to make sed delete the lines (I want to match the full line aside from the Day (in the example Thursday). But I was unable to get sed to delete anything because the file has \ and if I add \\ to the exlcude file it will still not delete the file.
    This deletes any line in the exclusion file that matches the first 2 columns in the status file:

    Code:
    awk 'NR==FNR{x[$1$2];next}$1$2 in x{next}1' Statusfile Exclusionfile
    Regards

  6. #6
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Code:
    awk 'BEGIN{ FS="\\" }
         NR==FNR{ a[$1$2]=$0;next}
         { if ( !( $1$2 in a ) ) print $1 }
         END { }' "statusfile" "exclusion"

  7. #7
    Just Joined!
    Join Date
    Oct 2007
    Location
    Houston
    Posts
    75
    If the line exists in the exclusion file delete that line from the status file. I had a phone call while typing that post up. Sorry. I switched the exclusion and status file but it is not deleting right. I need the line to be an exact match to ensure we do not delete a similar line.

  8. #8
    Linux Enthusiast
    Join Date
    Aug 2006
    Posts
    631
    You can simply change the order of the 2 files like this:

    Code:
    awk 'NR==FNR{x[$1$2];next}$1$2 in x{next}1' Exclusionfile Statusfile
    An exact match of the whole line in both files:

    Code:
    awk 'NR==FNR{x[$0];next}$0 in x{next}1' Exclusionfile Statusfile
    Regards

  9. #9
    Linux User
    Join Date
    Aug 2006
    Posts
    458
    Quote Originally Posted by Korelis View Post
    If the line exists in the exclusion file delete that line from the status file. I had a phone call while typing that post up. Sorry. I switched the exclusion and status file but it is not deleting right. I need the line to be an exact match to ensure we do not delete a similar line.
    Code:
    awk 'BEGIN{ FS="\\" }
         NR==FNR{ a[$0];next}
         { if ( !( $0 in a ) ) print $0 }
         END { }' "file2" "file"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...