- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- scripting.. searching for a string
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:14 AM
10-06-2006 08:14 AM
each line in a file contains four strings within double quotes("").
like this:
An "Apple" "a" day "keeps" the "doctor" away.
i need to grep for this this four strings in each line.
PS:i understand the need to learn GOOD scriping.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:32 AM
10-06-2006 08:32 AM
Re: scripting.. searching for a string
IN this line i need to grep the four strings and put in a new file as:
Apple a keeps doctor
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:33 AM
10-06-2006 08:33 AM
Solutiongrep -E '"Apple".*"a".*"keeps".*"doctor"' < myfile
Now if the order is not important then:
grep -E '"Apple"' < myfile | grep '"a"' | grep '"keeps"' | grep -E '"doctor"'
You can add a -i in addition to each -E to make grep's matchs case-insensitive. Note that the single-quotes are used to escape the double-quotes in your target strings.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:40 AM
10-06-2006 08:40 AM
Re: scripting.. searching for a string
i need to search for the four strings(each string will be inside "") in a line.
sorry for the wrong example.
the strings are not constant. so i need to search for the strings inside "".
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:47 AM
10-06-2006 08:47 AM
Re: scripting.. searching for a string
In that case, you should change the question because the task as you now define it (with your second posting) is not a good use of grep. There are better tools for the task when you want to print only the target strings.
typeset -i I=1
typeset -i N=$(grep -E '"Apple".*"a".*"keeps".*"doctor"' myfile | wc -l)
while [[ ${I} -le ${N} ]]
do
echo "Apple a day doctor" >> newfile
((I += 1))
done
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:49 AM
10-06-2006 08:49 AM
Re: scripting.. searching for a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:50 AM
10-06-2006 08:50 AM
Re: scripting.. searching for a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 08:59 AM
10-06-2006 08:59 AM
Re: scripting.. searching for a string
Just a go.
Will print all lines in the file that has all 4 words "apple", "a", "keeps", and "doctor" quoted in any casing in any order.
That what you want?
Probably can be done even shorter, but I don't wanna play golf right now
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 09:01 AM
10-06-2006 09:01 AM
Re: scripting.. searching for a string
>> PS:i understand the need to learn GOOD scriping.
The single most critical part of good scripting it learn to get and capture a good problem statement. Exactly what needs to happen? What should not happen? which eventualities need to be protected against (defensive scripting!).
I find that clear sample data, both input and output are critical in understanding the problem, the constraints of the solution, and as 'proof' that the script works.
Now as far as the task on hand...
What does 'grep for this this four strings' mean?
Do you want to grep for a single string in any of those for 'fields' but not outside?
So if the data was
1: An "Apple" "a" day "keeps" the "doctor" away.
2: An "pear" in "the" night is "just" "right".
Now when you 'grep' for 'the', should find the second line but not the first?
This awk will do that... just barely:
>awk -F\" -v x=the "{ if (index($2 $4 $6 $8, x) ) { print }}" tmp.txt
How does that work?
If a double quote is made the 'split' character with -F, then every other variable will be a quoted word: 2,4,6,8...
"$2 $4 $6 $6" glues them together, and if we find a non-zero return then the string pushed in x must be in there.
Now a sample contraint question:
In the code about 'theju' will match "the" "just" is that acceptable?
If not then we either have to look in the individual strings:
>awk -F\" -v x=the "{if (index($2,x) + index($4,x) + index($6,x) + index($8,x)) { print }}" tmp.txt
or use same acceptable seperator (here *), passed in as a variable to keep it flexible.
>awk -F\" -v x=theju -v a=* "{ if (index($2 a $4 a $6 a $8, x) ){print} }" tmp.txt
so many solutions...
so many gotchas.
Enjoy,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 09:03 AM
10-06-2006 09:03 AM
Re: scripting.. searching for a string
use the quote as delimiter and output the even numbered fields:
awk -F'"' 'NF==9 {for(i=2;i
will do this.
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-06-2006 10:39 AM
10-06-2006 10:39 AM
Re: scripting.. searching for a string
# awk -F\" '{for(i=1;i<=NF;++i){if(!(i%2)) printf("%s ",$i)} print ""}' infile
~hope it helps
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 02:58 AM
10-11-2006 02:58 AM
Re: scripting.. searching for a string
i thought to edit the first post thru Moderator intervention, but when others will read after sometime, they will not come to know what happend..so i am leaving that as it is.
now again, the REAL question is:
my file contains line like this:
firstLine: CONSTANT="no" NAME="abcde" initialValue="1" NAME="abcde"
secondLine: CONSTANT="no" NAME="fghij" initialValue="0" NAME="fghij"
1.)each line contains four strings
2.)out of which first string is always "no"
3.)second string and fourth string are same.
4.)third string can take only one of two values (0 or 1).
now, from this two lines:
CONSTANT="no" NAME="abcde" initialValue="1" NAME="abcde"
CONSTANT="no" NAME="fghij" initialValue="0" NAME="fghij"
i need to create a file like this:
abcde 1
fghij 0
note that: i cant "cut" since in the line starting there may be one space or two spaces or a tab.
hope u understand it clearly now.
it may be simple for GOOD admins.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 03:22 AM
10-11-2006 03:22 AM
Re: scripting.. searching for a string
cat file | awk -F"[ =]" '{print $4, $6}' | sed -e 's/"//g'
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 03:39 AM
10-11-2006 03:39 AM
Re: scripting.. searching for a string
lt09:/home/merijn 108 > cat xx.dta
CONSTANT="no" NAME="abcde" initialValue="1" NAME="abcde"
CONSTANT="no" NAME="fghij" initialValue="0" NAME="fghij"
lt09:/home/merijn 109 > cut -d\" -f4,6 xx.dta
abcde"1
fghij"0
lt09:/home/merijn 110 >
Or, *literally* applying that criteria:
lt09:/home/merijn 110 > perl -ne's/^.*?(NAME="([^"]+)").*?"([01])"\s+\1.*/$2\t$3/&&print' xx.dta
abcde 1
fghij 0
lt09:/home/merijn 111 >
Enjoy, Have FUN! H.merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 04:11 AM
10-11-2006 04:11 AM
Re: scripting.. searching for a string
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 04:22 AM
10-11-2006 04:22 AM
Re: scripting.. searching for a string
Using 'cat' in a pipeline to 'swk' is a waste of another process!
# cat file | awk 'FS="\"" {print $4 " " $6}'
...should be:
# awk 'FS="\"" {print $4 " " $6}' file
...or, more succinctly:
# awk 'FS="\"" {print $4,$6}' file
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 04:26 AM
10-11-2006 04:26 AM
Re: scripting.. searching for a string
~cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 04:34 AM
10-11-2006 04:34 AM
Re: scripting.. searching for a string
and two chars shorter as James' and even working in gawk:
awk -F'"' '{print $4,$6}' file
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
10-11-2006 04:44 AM
10-11-2006 04:44 AM
Re: scripting.. searching for a string
Yeah, two characters shorter --- I like that better than mine!
Regards!
...JRF...