How to extract just a word from a File in Shell?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to extract just a word from a File in Shell?
# 1  
Old 03-23-2010
How to extract just a word from a File in Shell?

Hello Friends,

I have a txt file which has data like this

HTML Code:
TNS Ping Utility for Solaris: Version 10.2.0.3.0 - Production on 23-MAR-2010 15:38:42
Copyright (c) 1997, 2006, Oracle.  All rights reserved.
Used parameter files:

Used TNSNAMES adapter to resolve the alias
Attempting to contact (DESCRIPTION = (ADDRESS_LIST = (ADDRESS = (PROTOCOL = TCP) (HOST = ab1078uk.server.com) (PORT = 1521))) (CONNECT_DATA = (SID = ab1078uk)))
OK (0 msec)
From this file i want to grep for just "ab1078uk.server.com"

Now the above text is repeated in the whole file serveral times and each time the name "ab1078uk" will be different and ".server.com" will be same and i want to extract all these names How can i do it....Hope i am clear with my requirement
# 2  
Old 03-23-2010
Question

Is this
(HOST = ab1078uk.server.com)
what you are searching for?
And, is it always in this format/segment? HOST = xxx.server.com
# 3  
Old 03-23-2010
Thanks for replying
Yes the format
(HOST = ab1078uk.server.com)
Will always be the same...only the word "ab1078uk" will keep changing


I will be searching for ".server.com" and it should return me "ab1078uk.server.com"

something like

cat file_name|grep *.server.com

I know this wont work...just trying to make things clear
# 4  
Old 03-23-2010
Tools

Assuming the sample you showed is stored as nj.txt, you could try:
Code:
>grep HOST <nj.txt | sed 's/(HOST/(~HOST/' | tr "()" "\n" | grep ~HOST
~HOST = ab1078uk.server.com
~HOST = ab1079us.server.com

If you get that far, then the rest is simple to find the websites.
# 5  
Old 03-23-2010
also assuming nj.txt is your filename, and assuming text within is as shown.

Code:
awk -F\( '$6 ~ "server.com" { print $6 }' nj.txt  | awk -F= '{ print $2 }' | tr -d \)

# 6  
Old 03-23-2010
Quote:
Originally Posted by joeyg
Assuming the sample you showed is stored as nj.txt, you could try:
Code:
>grep HOST <nj.txt | sed 's/(HOST/(~HOST/' | tr "()" "\n" | grep ~HOST
~HOST = ab1078uk.server.com
~HOST = ab1079us.server.com

If you get that far, then the rest is simple to find the websites.

Thanks worked

---------- Post updated at 12:39 PM ---------- Previous update was at 12:39 PM ----------

Quote:
Originally Posted by denn
also assuming nj.txt is your filename, and assuming text within is as shown.

Code:
awk -F\( '$6 ~ "server.com" { print $6 }' nj.txt  | awk -F= '{ print $2 }' | tr -d \)


Thanks worked
# 7  
Old 03-23-2010
Code:
awk -F"[=)]" '{for(i=1;i<=NF;i++){if ($i~/[A-Za-z0-9]+\.server\.com/) print $i}}' FILE


Last edited by EAGL€; 03-23-2010 at 05:48 PM.. Reason: removed first /server/ part
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need to extract the word after a particular keyword throughout the file..

Hi Everyone, Need help in extracting the hostname from the below output. Expected output: DS-TESTB-GDS-1.TEST.ABC.COM DS-TESTB-GDS-2.TEST.ABC.COM .... ... /tmp $ cat -n /tmp/patchreport 1 /usr/bin/perl /admin/bin/patch/applyPatches.pl --apply_patches... (4 Replies)
Discussion started by: thiyagoo
4 Replies

2. Shell Programming and Scripting

Extract the word from the file and print it

I have a file which I am reading and then I need to extract a particualr word and if it matches the line. 2015-01-22 07:30:17,814000 +0900 /INFO: - <ns2:virtualServerid="PH11PK" /> Means if the line contain Virtual server I need to extract the id . Code I wrote#!/usr/bin/perl ... (19 Replies)
Discussion started by: karan8810
19 Replies

3. Shell Programming and Scripting

Perl script to extract a word from the file

Hi everyone, I'm a perl newbie and need your help to extract a word inside the list of files with same pattern. <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <ns2:mycode xmlns:ns2="http://www.abcd.com/pqrs/acfSchema-2007a.xsd"> <id>10</id> <name>PaymentServices</name> ... (7 Replies)
Discussion started by: jhamaks
7 Replies

4. Shell Programming and Scripting

extract a word from text file name

Hi i want to extract the word present before .txt in the text file. For example, Sample_ab_a.txt ----------> i need 'a' Sample_abc_b.txt -----------> i need 'b' Can anyone help me in getting the word extracted (5 Replies)
Discussion started by: Sindhuap
5 Replies

5. Shell Programming and Scripting

extract whole thing in word, leaving behind last word. - perl

Hi, i've a string /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD/TESi T_11_HD_120/hd-12 i need to get string, like /u/user/DTE/T_LOGS/20110622_011532_TEST_11_HD_120/HD the words from HD should get deleted, i need only a string till HD, i dont want to use any built in... (4 Replies)
Discussion started by: asak
4 Replies

6. Shell Programming and Scripting

extract word from bracket - shell

Related to : thread : 34769-removing-duplicate-lines-file.html i want to extract the words in () eg: string1="bla bla (aaa) aha hai (aa)" after processing output i need is : aaa aa (2 Replies)
Discussion started by: linuxadmin
2 Replies

7. Shell Programming and Scripting

get the fifth line of a text file into a shell script and trim the line to extract a WORD

FOLKS , i have a text file that is generated automatically of an another korn shell script, i want to bring in the fifth line of the text file in to my korn shell script and look for a particular word in the line . Can you all share some thoughts on this one. thanks... Venu (3 Replies)
Discussion started by: venu
3 Replies

8. Programming

How to extract a sentences of word from a text file.

Hi , i have a text file that contain a story How do i extract the out all the sentences that contain the word Mon. in C++ I only want to show those sentences that contain the word mon eg. Monkey on a tree. Rabbit jumping around the tree. I am very rich, I have lots of money. Today... (1 Reply)
Discussion started by: xiaojesus
1 Replies

9. Shell Programming and Scripting

Can a shell script pull the first word (or nth word) off each line of a text file?

Greetings. I am struggling with a shell script to make my life simpler, with a number of practical ways in which it could be used. I want to take a standard text file, and pull the 'n'th word from each line such as the first word from a text file. I'm struggling to see how each line can be... (5 Replies)
Discussion started by: tricky
5 Replies

10. UNIX for Dummies Questions & Answers

extract last word on line to new file

Can someone please help me with how to extract the last word on a line to a new file? I have a list of names like: Ms. Nell D. Bullock Mrs. Sherrie M Avent LINDA ANNETTE RUSSELL Mr. Jerome R. Harris Pandora Tyndall I want the new file to look like this: Bullock Avent RUSSELL Harris... (10 Replies)
Discussion started by: michieka
10 Replies
Login or Register to Ask a Question