Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Extract words from a text file 1

Status
Not open for further replies.

JtheRipper

IS-IT--Management
Oct 4, 2002
274
GB
Hi there,

I have a text file (let's say temp.txt) that looks as follows:

Code:
NetWorker savegroup: (alert) NT Incremental completed, total 43 client(s), 0 Hostname(s) Unresolved, 2 Failed, 41 Succeeded. (extraprd1,
intradev1 Failed)
Start time:   Wed Oct 13 18:00:00 2004
End time:     Wed Oct 13 22:33:58 2004

--- Unsuccessful Save Sets ---

* extraprd1: e:: (returned from savefs) skipped.
The saveset returned from savefs probe does not match the one specified for the client.

* intradev1: e:: (returned from savefs) skipped.
The saveset returned from savefs probe does not match the one specified for the client.

--- Successful Save Sets ---

Successful Save Sets written to verified media noted with "V" in first column.

* apps1:C: savegrp: suppressed 1 lines of output - check daemon.log for details.
* apps1:C: save: C:Documents and SettingsAll UsersApplication

I want to extract the following:
"extraprd1" and "intradev1", and write them to another text file. In other words, I want everything between "Succeeded. (" and "Failed)".

I have some scripting experience, but am not sure on how to go about doing this.

Any info/help will be greatly appreciated.

Thanks,
J.
 
You can use sed, do a search on keyword sed and you will find numerous examples.
also man sed
 
cat filename| grep extraprd1 > file1
cat filename| grep intradev1 >> file1

awk {
j = index($0,"Begin")
if (!j) next
k = index($0,"END")
if (!k) next
print substr($0,j+5,k-j-5)
} >> file1

Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Sorry change Begin and END to Succeeded and Failed.



Mike

"A foolproof method for sculpting an elephant: first, get a huge block of marble, then you chip away everything that doesn't look like an elephant.
 
Have a look at thread822-943530 - a very neat solution.

Mike

"Deliver me from the bane of civilised life; teddy bear envy."

Want to get great answers to your Tek-Tips questions? Have a look at faq219-2884

 
sed -n '/Succeeded\. (/,/ Failed)/{;s!.*Succeeded\. (!!;s! Failed).*!!;p;}' temp.txt

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Thanks for all the replies! I will try them all later today and let you know which one works best for me!

J.
 
Hi,

Thanks PHV, I used your example and it worked perfectly.
I am having trouble though to use the script to remove an asterisk and a colon ":".

My text looks something like (test.log)
* apps1:E: save: E:SQL
* apps7:E: save: E:SQL
* apps2:E: save: E:SQL
* apps5:E: save: E:SQL

My sed command looks like this:
sed -n '/\* /,/\:)/{;s!.*\* !!;s!\:.*!!;p;}' test.log > test.out

I only want the part between the "* " and the first ":" so my tset.out will look like this:
apps1
apps7
apps2
apps5


Thanks,
J.
 
Your last sample may be scanned in a simpler manner :
sed -n 's!^\* \([^:]*\):.*!\1!p' test.log > test.out

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Hi,

Thanks for the quick reply. Can I please ask you to explain to me the above command so I can try and maybe help myself in the future, else you guys are going to get sick of me asking questions all the time!

I know the ^ means start from the beginning of the line, but what do the brackets "[" and "]" mean?
Also not too sure about this part "):.*!\1!p".

Thank you,
J.
 
sed -n 's!^\* \([^:]*\):.*!\1!p'[tt]
sed -n start sed without automatic printing
s! start a subtitute command with ! as arguments separator
^\* 3 consecutive tokens expected: beginning of line, literal star and space
\( start a subexpression
[^ start a not matching list
: set of characters is solely colon
]* end of not matching list for zero or more occurrences
\) end of subexpression
:.* colon followed by any characters until end of line expected
!\1 replacement string is the value matched by the 1st subexpression (back-reference)
!p prints the pattern space if a replacement was made[/tt]

Hope This Helps, PH.
Want to get great answers to your Tek-Tips questions? Have a look at FAQ219-2884 or FAQ222-2244
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top