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!
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"
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
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
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.