Computing.Net > Forums > Unix > Possible advanced GREP or script?

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Possible advanced GREP or script?

Reply to Message Icon

Name: Jamay2k
Date: January 7, 2009 at 11:07:39 Pacific
OS: WinXP
CPU/Ram: 1GB
Product: N/a / N/A
Subcategory: General
Comment:

Hi ,
Within Unix from a large file containing text (not always seperated with spaces) 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 thoughts/help would be great!

Jama



Sponsored Link
Ads by Google

Response Number 1
Name: Jamay2k
Date: January 7, 2009 at 11:29:50 Pacific
Reply:

Just to add to it, The specific word that I want to find may not always be in the same place on each line.


0

Response Number 2
Name: nails
Date: January 7, 2009 at 14:19:02 Pacific
Reply:

I think a script is required. This is my take. Let me know if you have any questions:


#!/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

If efficiency is an issue, you might consider greping your original file first, and place the results in a temp file and run the script against the temp file:


#!/bin/ksh

word=myword

grep "$word" myfile > tmpmyfile
while read line
do
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"
done < tmpmyfile



0

Response Number 3
Name: lankrypt0
Date: January 8, 2009 at 09:11:50 Pacific
Reply:

If I am understanding this right, you can probably do this one liner.

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

This will look for any line with your beginning word, cut everything before it, then everything after your ending word. With the following file, using one as my begin and three as my end:
nine one two three asdf asdf asdf
one four seven three asdf asdf asdf
four six nine asdf asdf asdf
seventeen one five thirteen six three asdf asdf asdf asdf
eleven twelve asdf asdf asdf asdf asdf a
one six six six six five three xxsd asdfa asdf asdf asdf

it produces:
one two three
one four seven three
one five thirteen six three
one six six six six five three


0

Response Number 4
Name: Jamay2k
Date: January 8, 2009 at 09:56:23 Pacific
Reply:

Thank you both for your replies .
lankrypt0 have not had chance to try your one liner out , but it looks good!


0

Response Number 5
Name: Fist (by fmwap)
Date: January 31, 2009 at 00:50:43 Pacific
Reply:

grep -E
There's actually a link to this option as 'egrep', because it's so common:

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


0

Related Posts

See More



Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Unix Forum Home


Sponsored links

Ads by Google


Results for: Possible advanced GREP or script?

grep - print next line too www.computing.net/answers/unix/grep-print-next-line-too/7417.html

need help with grep.... www.computing.net/answers/unix/need-help-with-grep/7366.html

Utility or script for renaming files on www.computing.net/answers/unix/utility-or-script-for-renaming-files-on-/2538.html