Find the answer to your Linux question:
Results 1 to 2 of 2
Hello! Please, help me to write such script (bash script) . I have some text file with name filename.txt I must check if this file contains string "test-string-first", I must ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Apr 2011
    Posts
    2

    Find string in text file (bash script)


    Hello!

    Please, help me to write such script (bash script).
    I have some text file with name filename.txt
    I must check if this file contains string "test-string-first", I must cut from this file string which follows string "keyword-string:" and till first white-space and save it to some variable.

    For example. File:
    PHP Code:
    PHP Code:
    Start 15022011 Eng 12-3-42
    SN1232324422 11 test
    -string-first
    SN322211 securities
    HH keyword
    -string:123456321-net mark (11-22
    This file contains "test-string-first" and I need to get 123456321-net string (between "keyword-string:" and first space)

    and i want to have such code structure:
    PHP Code:
    if (check_exists_of('test-string-first','filename.txt'then
     MYVAR 
    awk .... filename.txt
     
    echo $MYVAR
    else
     echo 
    'file doesn't contain '''test-string-first'' string!'
     
    fi 
    help me pls to change check_exists_of('test-string-first','filename.txt') and MYVAR = awk .... filename.txt statements to the correct.

  2. #2
    Linux Guru Lakshmipathi's Avatar
    Join Date
    Sep 2006
    Location
    3rd rock from sun - Often seen near moon
    Posts
    1,769
    I would provide some hints . I hope you can take it from there.

    grep command is used to search for text.
    like
    ret=`grep "searchtext" Filename.txt`

    If grep command pass (ie it found "searchtext" ) $? will be equal to zero.
    First they ignore you,Then they laugh at you,Then they fight with you,Then you win. - M.K.Gandhi
    -----
    FOSS India Award winning ext3fs Undelete tool www.giis.co.in. Online Linux Terminal http://www.webminal.org

Posting Permissions

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