LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 10-30-2005, 11:38 PM   #1
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Rep: Reputation: 15
shell script: grep using string with specail charactors?


Shell script: How can I assign a string to a variable, then use that variable to grep. For example:
Code:
var = grep 'PATTERN' $FILE1
grep $var $FILE2
If the string contain special charactors such as "[]", how can I achieve the goal?

I'm searching for a general solution.

Thanks.
 
Old 10-31-2005, 12:12 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Is this what you have in mind?
Code:
#!/bin/bash
var=`grep -o [[the]] file.txt`
grep $var file1.txt
I have file.txt which looks like this....
cat file.txt
now is [the] time
for all good men
to come to the

and file1.txt looks like this....
cat file1.txt
now is not [the] time
for all good men
to come to the

The results of the script is a line from file1.txt
now is not [the] time
 
Old 10-31-2005, 12:45 AM   #3
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
I have write a short script to analyze the ouput of "mailq or postqueue -p", to analyze the status of mails to a domain,
Code:
#!/bin/sh

if [ $# -lt 1 ]; then
    echo "Please give a domain name."
    exit 1
fi

_domain=$1

echo "mails to $_domain in the queue:"
queue=`postqueue -p | grep "^.*@$_domain$" -B2`
infomation=`echo "$queue" | sed -n '/[0-9A-F]/{n;p;}'`

echo -n "TOTOL: "
echo "$infomation" | grep -v -c '^--'

echo "sort by different error info:"
info=`echo "$infomation" | grep -v '^--' | sort | uniq -c | sort -r +0n`
echo "$info"

info=`echo "$info" | sed 's/^[:digit:]\>//g'`

echo ""
and the result is:
Code:
mails to qq.com in the queue:
TOTOL: 2238
sort by different error info:
      1 (connect to mx0.qq.com[219.133.40.163]: server dropped connection without sending the initial SMTP greeting)
      1 (connect to mx0.qq.com[219.133.60.154]: server dropped connection without sending the initial SMTP greeting)
      1 (Host or domain name not found. Name service error for name=mx0.qq.com type=A: Host not found, try again)
      2 (connect to mx0.qq.com[219.133.40.165]: server dropped connection without sending the initial SMTP greeting)
      5 (connect to mx0.qq.com[219.133.40.166]: server dropped connection without sending the initial SMTP greeting)
    356 (delivery temporarily suspended: connect to mx0.qq.com[219.133.40.163]: server dropped connection without sending the initial SMTP greeting)
    779 (delivery temporarily suspended: connect to mx0.qq.com[219.133.40.166]: server dropped connection without sending the initial SMTP greeting)
   1093 (delivery temporarily suspended: connect to mx0.qq.com[219.133.40.165]: Connection timed out)
Then I want to find the queue IDs for each of this infomations, so I want to get every line string and re-grep & sed to get the ID, such as "E42D3A285D7".

But as you can see, there is [...] in some lines, you can't do this:
Code:
mailq | grep "(connect to mx0.qq.com[219.133.40.163]: server dropped connection without sending the initial SMTP greeting)" -B1 -A1
directly!
 
Old 10-31-2005, 01:01 AM   #4
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
mailq | grep -x "(connect to mx0.qq.com[219.133.40.165]: server dropped connection without sending the initial SMTP greeting)" -B1 -A1 -F

That OK, use the parameter "-F"!
 
Old 11-07-2005, 07:36 PM   #5
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
But what about the condition of sed.

If I read a variable, like $value="/path/to/file", and then I want to sed:
sed "s/$key/$value/g" ?
 
Old 11-12-2005, 07:44 AM   #6
eddiebaby1023
Member
 
Registered: May 2005
Posts: 378

Rep: Reputation: 33
Quote:
Originally posted by Chowroc
But what about the condition of sed.

If I read a variable, like $value="/path/to/file", and then I want to sed:
sed "s/$key/$value/g" ?
You don't have to use / as the delimiter in sed, you can use pretty much any character. Find one that's not going to be in your strings:
Code:
sed "s!$key!$value!g"
 
Old 11-14-2005, 08:47 PM   #7
Chowroc
Member
 
Registered: Dec 2004
Posts: 145

Original Poster
Rep: Reputation: 15
Can I do this?

But how to use defferent delimiter? I think sed must be told that just like awk.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Shell Script - String Replacement revof11 Programming 7 11-29-2005 06:38 AM
(shell script) string parsing kuru Programming 4 09-12-2005 07:59 PM
Grep Syslog - email shell script voodoofxz Linux - Newbie 1 09-06-2005 04:27 PM
Shell script to find a particular string Prasun1 Linux - General 5 08-30-2005 09:23 AM
grep in shell script fails on redhat 3.0 rlangsto Linux - General 4 03-06-2005 07:42 PM


All times are GMT -5. The time now is 04:43 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration