1754357 Members
4799 Online
108813 Solutions
New Discussion юеВ

Script/awk/sed question

 
SOLVED
Go to solution
Duke Nguyen
Occasional Advisor

Script/awk/sed question

I am trying to search a text file for a particular string and print that string plus the next n number of characters following that string. For example:

Contents of text file:
1234 qwer Phone 123-123-1234
76 wejyf Phone 567-325-8765
Phone 983-876-3241 3 deddfgh
q Phone 000-335-6593 ddi555

Want output to be:
Phone 123-123-1234
Phone 567-325-8765
Phone 983-876-3241
Phone 000-335-6593

What would be the best way to accomplish this? Thanks in advance.

Duke

5 REPLIES 5
Tom Maloy
Respected Contributor
Solution

Re: Script/awk/sed question

Assume Phone is your pattern.
Assume the data is in file yourData.
Assume you need 18 columns (characters) at the end (Phone xxx-xxx-xxxx).

grep Phone yourData | sed -e "s/.*Phone/Phone/" | cut -c1-18

grep finds the pattern.
sed deletes characters in front of the pattern.
cut keeps the number of characters you want.

Tom
Carpe diem!
H.Merijn Brand (procura
Honored Contributor

Re: Script/awk/sed question

perl -pe 's/^.*\b(Phone\s*[-\d]+)\b.*$/$1/' phone-file
Enjoy, Have FUN! H.Merijn
Hai Nguyen_1
Honored Contributor

Re: Script/awk/sed question

Duke,

This should work as well.

# sed 's/^.*Phone/Phone/' text_file | awk '{print $1 " " $2}'

Hai
Robin Wakefield
Honored Contributor

Re: Script/awk/sed question

...or even:

sed 's/.*\(Phone [^ ]*\).*/\1/' filename

Rgds, Robin
Duke Nguyen
Occasional Advisor

Re: Script/awk/sed question

Thanks everyone!