String extraction from a text file


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers String extraction from a text file
# 1  
Old 04-13-2009
String extraction from a text file

The following script code works great for extracting 'postmaster' from a line of text stored in a variable named string:
Code:
string="PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN"
stuff=$( echo $string | cut -d@ -f1 | awk '{ print $NF }' )
echo $stuff

However, I need to be able to get that line of text out of a text file rather than being in a variable, and I cannot for the life of me figure out how to do it.

Can anyone please help?

Thanks.
# 2  
Old 04-13-2009
you can grep it.
grep youtext filename | cut ...........
# 3  
Old 04-13-2009
Quote:
Originally Posted by cleanden
The following script code works great for extracting 'postmaster' from a line of text stored in a variable named string:
Code:
string="PenaltyError:=554 5.7.1 Error, send your mail to postmaster@LOCALDOMAIN"
stuff=$( echo $string | cut -d@ -f1 | awk '{ print $NF }' )
echo $stuff

However, I need to be able to get that line of text out of a text file rather than being in a variable, and I cannot for the life of me figure out how to do it.

Can anyone please help?

Thanks.
you mean you have that line in a file and you want to extract that line from a file??
or
you want to extract that line and from that line you want only string with @LOCALDOMAIN??
# 4  
Old 04-13-2009
That line is in a file and ultimately I need to get to 'postmaster'
I can do it by hard-coding that line into a variable but I cannot figure out the grep cut command to get it out of the file.
# 5  
Old 04-13-2009
Quote:
Originally Posted by cleanden
That line is in a file and ultimately I need to get to 'postmaster'
I can do it by hard-coding that line into a variable but I cannot figure out the grep cut command to get it out of the file.
then i don't know whats the point.. anyways
Code:
 
awk 'BEGIN{RS="@"}/postmaster/{print $NF}{exit}' filename

# 6  
Old 04-13-2009
The point that I guess I failed to make is that postmaster is not a static piece of text - it could be anything, but I know it always precedes @LOCALDOMAIN
# 7  
Old 04-13-2009
Quote:
Originally Posted by cleanden
The point that I guess I failed to make is that postmaster is not a static piece of text - it could be anything, but I know it always precedes @LOCALDOMAIN
thats what i asked you
Quote:
you mean you have that line in a file and you want to extract that line from a file??
or
you want to extract that line and from that line you want only string before @LOCALDOMAIN??
try this
Code:
 
awk '/@LOCALDOMAIN/{split($NF,A,"@")}{print A[1]}' filename

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Search a string and display its location on the entire string and make a text file

I want to search a small string in a large string and find the locations of the string. For this I used grep "string" -ob <file name where the large string is stored>. Now this gives me the locations of that string. Now how do I store these locations in a text file. Please use CODE tags as... (7 Replies)
Discussion started by: ANKIT ROY
7 Replies

2. Shell Programming and Scripting

String Extraction

I am trying to extract a time from the below string in perl but not able to get the time properly I just want to extract the time from the above line I am using the below syntax x=~ /(.*) (\d+)\:(\d+)\:(\d+),(.*)\.com/ $time = $2 . ':' . $3 . ':' . $4; print $time Can... (1 Reply)
Discussion started by: karan8810
1 Replies

3. UNIX for Dummies Questions & Answers

String extraction from log file

Hi, Log file will contain text like below: May 7 14:12:09 nap_fujitsu-nexus_pbb-denek-01-r_DE.de.ignite.net 108767: 115673: May 7 14:12:08: %RTT-3-IPSLATHRESHOLD: IP SLA Monitor(112): Threshold below for rtt May 8 14:12:09 nap_fujitsu-nexus_pbb-denek-01-r_DE.de.ignite.net 108767: 115673:... (3 Replies)
Discussion started by: Dip
3 Replies

4. Shell Programming and Scripting

Text extraction

Dear All, I am trying to extract text from a file containing cron entries. cat /var/tmp/cron_backups/debmed_tmp < * * * * * /bell > * * * * * /belly what I am trying to do is create two text files containing all entries that begin with < and another text files containing entries with > .... (4 Replies)
Discussion started by: Junaid Subhani
4 Replies

5. Shell Programming and Scripting

Search a string in a text file and add another string at the end of line

Dear All I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB... (5 Replies)
Discussion started by: suryanarayana
5 Replies

6. Shell Programming and Scripting

Search a string in a text file and add another string at the particular position of a line

I am having a text file which is having more than 200 lines. EX: 001010122 12000 BIB 12000 11200 1200003 001010122 2000 AND 12000 11200 1200003 001010122 12000 KVB 12000 11200 1200003 In the above file i want to search for string KVB and add/replace... (1 Reply)
Discussion started by: suryanarayana
1 Replies

7. UNIX for Advanced & Expert Users

extraction of data from a text file which follows certain pattern

hi everybody, i have a file, in it I need to extract some data that follows a particular pattern.. For example: my file contains like now running Speak225 sep 22 mon 16:34:05 2008 -------------------------------- ... (4 Replies)
Discussion started by: mohkris
4 Replies

8. Shell Programming and Scripting

extraction of perfect text from file.

Hi All, I have a file of the following format. <?xml version='1.0' encoding='utf-8'?> <tomcat-users> <role rolename="tomcat"/> <role rolename="role1"/> <role rolename="manager"/> <role rolename="admin"/> <user username="tomcat" password="tomcat" roles="tomcat"/> <user... (5 Replies)
Discussion started by: nua7
5 Replies

9. Shell Programming and Scripting

Shell script for text extraction from a file

Hi All, I am new to Shell Scripting. I have a file consisting of XML messages.Each message is associated with a timestamp value(it is not a xml field).I need to extract\copy all messages in a particular time interval and put in another new file using Shell Scripting. My XML looks like... (3 Replies)
Discussion started by: vignesh53
3 Replies

10. Shell Programming and Scripting

appending string to text file based on search string

Hi, I need to append string "Hi" to the beginning of the lines containing some specific string. How can I achieve that? Please help. Malay (1 Reply)
Discussion started by: malaymaru
1 Replies
Login or Register to Ask a Question