Results 1 to 5 of 5

Thread: Creating script with GREP OR ?

  1. #1
    Join Date
    Oct 2009
    Posts
    38

    Creating script with GREP OR ?

    Hi ,

    I am using Unix operating system and need some suggestion regarding script. From a large file containing text I would like to be able to look for a word , and then extract the word with the next n amount of characters or the next amount of characters untill it hits a certain character (such as a / or a ], but not extract the whole line.

    Any idea and help would be great for me!

  2. #2
    Join Date
    Jan 2008
    Posts
    3,388

    Re: Creating script with GREP OR ?

    Hello man !

    You want to create a script or to choose the command line to perform whatever you want to do anything else but i have a good suggestion.You question was very difficult to understand that what exactly you need to do.

    I have a solution and you need not create script,you can issue this command from the terminal .

    Code:
    $ grep beginword infile | sed -e "s/.*beginword/beginword/g" -e "s/endword.*/endword/g"

  3. #3
    Join Date
    May 2008
    Posts
    4,085

    Creating script with GREP OR ?

    I think,you need to use this script to achieve your desire.The script is coded as follows -

    Code:
    #!/bin/ksh
    
    word=myword
    
    while read line
    do
    if echo "$line"|grep "$word" > /dev/null
    then # if word exists in line
    str=""
    len=${#line} # length of line
    mypos=$(echo "$line"|awk ' { print index($0, "'"$word"'")} ')
    while [[ $mypos -le $len ]]
    do # stop at the end of the line
    # grab the character
    charpos=$(echo "$line"| cut -c$mypos)
    # get the next char position
    mypos=$((mypos+1))
    if [[ $charpos = "/" || $charpos == "]" ]]
    then # stop at / or ]
    break
    fi
    # build the string
    str="$str"${charpos}
    done
    echo "$str"
    fi
    done < myfile

  4. #4
    Join Date
    Apr 2008
    Posts
    3,267

    Creating script with GREP ?

    According to me,the grep -E command line would be helpful for you to deal with this type of problem.It's exactly a link to this option as 'egrep' because it's so easier :

    The command can be constructed as follows -

    Code:
    egrep -o '(match|this|or|this)' file.txt

  5. #5
    Join Date
    Apr 2008
    Posts
    3,522

    Variation of Grep command

    This would be the one of the helpful site for you to know more about the grep command and their behavior.

    The page would help you provide different optional arguments can be attached with the grep command and then just find out the proper solution for your self and create the script for your own.

    The long description about the Grep command.

Similar Threads

  1. Bash script for creating a new month and year
    By Kohlmann in forum Software Development
    Replies: 4
    Last Post: 30-08-2010, 09:09 PM
  2. creating a script for a host file
    By mopat in forum Technology & Internet
    Replies: 1
    Last Post: 07-07-2010, 11:44 PM
  3. Creating shell script in ubuntu
    By Elizabeth Allen in forum Operating Systems
    Replies: 5
    Last Post: 24-03-2010, 01:35 PM
  4. Need help creating a SCRIPT file for navigation buttons
    By Rob Dizzle in forum Software Development
    Replies: 4
    Last Post: 19-01-2010, 04:53 PM
  5. Problem After Creating Home Folder with vbs script
    By Susan Bradley in forum Active Directory
    Replies: 3
    Last Post: 18-08-2008, 09:33 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,711,708,125.68023 seconds with 17 queries