Find the answer to your Linux question:
Results 1 to 4 of 4
I have a file within there is this sentence: "title dos". how could i know with if condition that this sentence exist in the file and it exactly as it ...
  1. #1
    Just Joined!
    Join Date
    May 2010
    Posts
    4

    Question need help with wildcards

    I have a file within there is this sentence: "title dos". how could i know with if condition that this sentence exist in the file and it exactly as it should be. I dont have a problem if the line will end with whitespaces or tabs(this may happen if someone didn't write it right("title dos ").what i mean is how could i know the line is right and doesnt end with a letter that someone wrote in mistake
    (for example:"title dos h"). I will be so glad if someone could help and send the exact answer.

  2. #2
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    Hi,
    if I understand the requirements right, then this should do:
    Code:
    egrep 'title dos([[:space:]]*)?$' filename
    You must always face the curtain with a bow.

  3. #3
    Just Joined!
    Join Date
    May 2010
    Posts
    4

    there is still a problem

    i dont know how to put it in an if condition

  4. #4
    Linux Guru Irithori's Avatar
    Join Date
    May 2009
    Location
    Munich
    Posts
    2,097
    You are lucky, I was a bit bored, so I wrote it
    Save that as "find_title_dos.sh",
    make it executeable
    and call it with your file as argument.

    Code:
    #!/bin/env bash
    
    SEARCHSTRING="title dos"
    EGREP=`which egrep`
    
    if [ -z "$EGREP" ]; then
       echo "GNU egrep not found, exiting"
       exit 1;
    fi
    
    if [ -z "$1" -o ! -f "$1" ]; then
       echo "Usage: find_title_dos.sh [FILE]"
       exit 1
    fi
    
    OUTPUT=`$EGREP "$SEARCHSTRING" $1 |$EGREP -v "$SEARCHSTRING([[:space:]]*)?$"`
    RC=$?
    
    case "$RC" in
       0)
          echo "Warning: Found unwanted lines in file \"$1\":"
          echo "$OUTPUT"
          exit 1;
          ;;
       1)
          echo "File \"$1\" is ok"
          exit 0;
          ;;
       *)
          echo "Error in search"
          exit 1;
          ;;
    esac
    P.S.:
    This script is a bit hack-ish, please check if the logic is good enough for your use case.
    Especially that part about egrep *not* finding the pattern leading to "File is ok" is flacky.. :
    If the file does not contain "title dos" at all, then the script will still tell "File is ok"

    So, please adapt to your needs
    Last edited by Irithori; 05-24-2010 at 02:00 PM.
    You must always face the curtain with a bow.

Posting Permissions

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