Go Back   TechArena Community > Software > Operating Systems
Become a Member!
Forgot your username/password?
Register Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



Creating script with GREP OR ?

Operating Systems


Reply
 
Thread Tools Search this Thread
  #1  
Old 12-03-2010
Member
 
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!

Reply With Quote
  #2  
Old 12-03-2010
Member
 
Join Date: Jan 2008
Posts: 3,384
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"
Reply With Quote
  #3  
Old 12-03-2010
Member
 
Join Date: May 2008
Posts: 4,080
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
Reply With Quote
  #4  
Old 12-03-2010
Member
 
Join Date: Apr 2008
Posts: 3,259
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
Reply With Quote
  #5  
Old 12-03-2010
Member
 
Join Date: Apr 2008
Posts: 3,516
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.
Reply With Quote
Reply

  TechArena Community > Software > Operating Systems
Tags: , , , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "Creating script with GREP OR ?"
Thread Thread Starter Forum Replies Last Post
Bash script for creating a new month and year Kohlmann Software Development 4 30-08-2010 10:09 PM
creating a script for a host file mopat Technology & Internet 1 08-07-2010 12:44 AM
Creating shell script in ubuntu Elizabeth Allen Operating Systems 5 24-03-2010 02:35 PM
Need help creating a SCRIPT file for navigation buttons Rob Dizzle Software Development 4 19-01-2010 04:53 PM
Problem After Creating Home Folder with vbs script Susan Bradley Active Directory 3 18-08-2008 10:33 PM


All times are GMT +5.5. The time now is 12:26 PM.