Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Find string in text file (bash script)


    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
    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
    awk .... filename.txt
    echo $MYVAR
    'file doesn't contain '''test-string-first'' string!'
    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
    3rd rock from sun - Often seen near moon
    I would provide some hints . I hope you can take it from there.

    grep command is used to search for text.
    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 Online Linux Terminal

Posting Permissions

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