LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 01-30-2012, 03:15 AM   #1
dsankarmca
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Rep: Reputation: Disabled
Find String


Dear all,

i am new in linux.
i have one file like below..
......
......
ssh root@00.00.00.00 "LANG=C;export LANG;/mnt/trans/m/20120126/a.sh 2>&1 | tee -a /mnt/trans/m/20120126/a.log &" &
ssh root@00.00.00.00 "/mnt/trans/m/20120126/c.sh 2>&1 | tee -a /mnt/trans/m/20120126/c.log &" &
ssh root@00.00.00.00 "/mnt/trans/m/20120126/d.sh 2>&1 | tee -a /mnt/trans/m/20120126/d.log &" &
ssh root@00.00.00.00 "/db/dump/trans/m/20120126/compareDiff.sh 2>&1 | tee -a /db/dump/trans/m/20120126/compareDiff.log &" &
....
....

here i want to display only a.sh,c.sh,d.sh and compareDiff.sh.

Thanks for any help
sankar.d
 
Old 01-30-2012, 03:40 AM   #2
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Code:
grep ".sh" filename
Is the simplest, but would also match "e.sh", "f.sh" etc etc.

Code:
grep 'a.sh\|c.sh\|compareDiff.sh' tmp
would match only the strings separated by "\|"
 
Old 01-30-2012, 05:45 PM   #3
cbtshare
Member
 
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
This script works...

Code:
#!/bin/bash

FILE=/data.txt

FIRST=$(cat $FILE | cut -d "/" -f6 | awk {'print $1'} | grep -E '.sh$')
SECOND=$(cat $FILE | grep -E 'compareDiff.sh' | awk '{print $3}' | cut -d "/" -f7)

echo "$FIRST"
echo "$SECOND"
 
1 members found this post helpful.
Old 01-30-2012, 05:49 PM   #4
cbtshare
Member
 
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
Quote:
Originally Posted by fukawi1 View Post
Code:
grep ".sh" filename
Is the simplest, but would also match "e.sh", "f.sh" etc etc.

Code:
grep 'a.sh\|c.sh\|compareDiff.sh' tmp
would match only the strings separated by "\|"
This wouldn't work as it returns all the lines ..
Quote:
Even using grep -E ' .sh$'
 
Old 01-30-2012, 07:12 PM   #5
schneidz
LQ Guru
 
Registered: May 2005
Location: boston, usa
Distribution: fc-15/ fc-20-live-usb/ aix
Posts: 5,108

Rep: Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873Reputation: 873
does 'grep -o a.sh' work ?
 
Old 01-30-2012, 07:52 PM   #6
dsankarmca
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
thanks for your answer..

Quote:
Originally Posted by cbtshare View Post
This script works...

Code:
#!/bin/bash

FILE=/data.txt

FIRST=$(cat $FILE | cut -d "/" -f6 | awk {'print $1'} | grep -E '.sh$')
SECOND=$(cat $FILE | grep -E 'compareDiff.sh' | awk '{print $3}' | cut -d "/" -f7)

echo "$FIRST"
echo "$SECOND"
Hi..

thanks for your reply..
its working fine.
thanks a lot..

Last edited by dsankarmca; 01-30-2012 at 07:57 PM.
 
Old 01-30-2012, 07:56 PM   #7
dsankarmca
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by schneidz View Post
does 'grep -o a.sh' work ?
hi.
thanks for your reply..
the above query will return only a.sh..
 
Old 01-30-2012, 09:24 PM   #8
cbtshare
Member
 
Registered: Jul 2009
Posts: 619

Rep: Reputation: 42
Quote:
Originally Posted by dsankarmca View Post
Hi..

thanks for your reply..
its working fine.
thanks a lot..
Your welcome, please mark the thread as solved
 
Old 01-31-2012, 12:24 AM   #9
fukawi1
Member
 
Registered: Apr 2009
Location: Melbourne
Distribution: Fedora & CentOS
Posts: 854

Rep: Reputation: 190Reputation: 190
Hate to be a stickler here, but:
Quote:
Code:
#!/bin/bash

FILE=/data.txt

FIRST=$(cat $FILE | cut -d "/" -f6 | awk {'print $1'} | grep -E '.sh$')
SECOND=$(cat $FILE | grep -E 'compareDiff.sh' | awk '{print $3}' | cut -d "/" -f7)

echo "$FIRST"
echo "$SECOND"
Will return *.sh.

I misread the original post, but as schneidz suggested, grep -o, would have the desired effect, and requires a lot less work, than all that awk'ing and cutting.
Code:
grep -o 'a.sh\|c.sh\|compareDiff.sh' file
 
Old 01-31-2012, 01:16 AM   #10
dsankarmca
LQ Newbie
 
Registered: Jan 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by fukawi1 View Post
Hate to be a stickler here, but:

Will return *.sh.

I misread the original post, but as schneidz suggested, grep -o, would have the desired effect, and requires a lot less work, than all that awk'ing and cutting.
Code:
grep -o 'a.sh\|c.sh\|compareDiff.sh' file

hi..
thanks for your support.
a.sh , c.sh , compareDiff.sh, those names are not constant, it may be change or added one more file path etc..
in that case also should be work with the same command.
 
  


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
find time in string kmack1023 Programming 5 12-14-2009 11:02 PM
find string in filename and use string to create directories daberkow Linux - Newbie 11 05-01-2009 02:12 PM
Find a value following a string sgracelin Linux - Newbie 1 07-22-2005 08:41 AM
How to find a string in a directory? iclinux Linux - Newbie 2 07-02-2005 08:20 AM
find a string and write it somewhere eicherlist Programming 11 01-02-2004 10:07 AM


All times are GMT -5. The time now is 02:28 PM.

Main Menu
Advertisement
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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration