Sponsored Content
Operating Systems Solaris How to find multiple strings on different lines in file? Post 302957377 by Don Cragun on Friday 9th of October 2015 09:42:16 PM
Old 10-09-2015
I would try using a single awk (or on Solaris/SunOS systems /usr/xpg4/bin/awk or nawk) script instead of trying to compare outputs from two fgrep commands:
Code:
#!/bin/ksh
if [ $# -lt 1 ]
then	printf 'Usage: %s file...\n' "${0##*/}" >&2
	exit 1
fi
/usr/xpg4/bin/awk '
function check() {
	if(f1 && f2)
		print fn
	fn = FILENAME
	f1 = f2 = 0
}
FNR == 1 {
	check()
}
/<Orin>sop/ {
	f1 = 1
}
/<Dup>two/ {
	f2 = 1
}
END {	check()
}' "$@"

But, if you want to do it using fgrep, you could try something like:
Code:
#!/bin/ksh
if [ $# -lt 1 ]
then	printf 'Usage: %s file...\n' "${0##*/}" >&2
	exit 1
fi
fgrep -l '<Orin>sop' "$@" | sort -r > match1.$$
fgrep -l '<Dup>two' "$@" > match2.$$
fgrep -xf match1.$$ match2.$$
rm -rf match[12].$$

Note that I don't believe that the sort -r half of the pipeline with the 1st fgrep should be needed, but on some systems (including Apple OS X which uses a BSD based grep utility), the command:
Code:
fgrep -x -e pattern -e pattern2 file

will not print lines in file matching pattern2 if pattern is a string matching an initial substring of pattern2. But, if the order of those matching patterns is reversed, it correctly finds all of the matching lines. I don't know if the Solaris fgrep suffers from this issue or not, but with the sort -r, the problem should be avoided if it does exist.

Note also that the 2nd script takes fewer lines (and is 35 characters shorter), running 3 fgrep commands and an rm command (not counting the sort that might not be needed depending on how fgrep -x works on your system) will take longer to run than one invocation of awk unless you are processing LARGE files and the patterns you're looking for appear early in the files. On systems where awk includes the nextfile command, that difference could be removed, but awk, nawk, and /usr/xpg4/bin/awk on Solaris systems do not provide do not provide that extension.
 

10 More Discussions You Might Find Interesting

1. Linux

To find multiple strings count in a file

I need to find the line count of multiple strings in a particular file. The strings are as follows: bmgcc bmgccftp bsmsftp bulkftp cctuneftp crbtftp crmpos cso gujhr I am doing manual grep for each of the string to find the line count. The command i am using right now is: grep mark... (3 Replies)
Discussion started by: salaathi
3 Replies

2. UNIX for Dummies Questions & Answers

grep command to find multiple strings in multiple lines in a file.

I want to search files (basically .cc files) in /xx folder and subfolders. Those files (*.cc files) must contain #include "header.h" AND x() function. I am writing it another way to make it clear, I wanna list of *.cc files that have 'header.h' & 'x()'. They must have two strings, header.h... (2 Replies)
Discussion started by: ritikaSharma
2 Replies

3. Shell Programming and Scripting

Extract strings from multiple lines into one file -

input file Desired csv output gc_type, date/time, milli secs af, Mar 17 13:09:04 2011, 144.596 af, Mar 20 00:37:37 2011, 144.242 af, ar 20 21:30:59 2011, 108.518 Hi All, Any help in acheiving the above would be appreciated. I would like to parse through lines within one file and... (5 Replies)
Discussion started by: satish.vampire
5 Replies

4. Shell Programming and Scripting

replace a string with contents of a txt file containing multiple lines of strings

Hello everyone, ive been trying to replace a string "kw01" in an xml file with the contents of a txt file having multiple lines. im a unix newbie and all the sed combinations i tried resulted to being garbled. Below is the contents of the txt file: RAISEDATTIME --------------------... (13 Replies)
Discussion started by: 4dirk1
13 Replies

5. Shell Programming and Scripting

Extract strings from multiple lines into one csv file

Hi all, Please go through my requirement. I have a log file in the location /opt/WebSphere61/AppServer/profiles/EMQbatchprofile/logs/EMQbatch This file contains the follwing pattern data <af type="tenured" id="42" timestamp="May 14 13:44:13 2011" intervalms="955.624"> <minimum... (8 Replies)
Discussion started by: satish.vampire
8 Replies

6. Shell Programming and Scripting

Delete lines in file containing duplicate strings, keeping longer strings

The question is not as simple as the title... I have a file, it looks like this <string name="string1">RZ-LED</string> <string name="string2">2.0</string> <string name="string2">Version 2.0</string> <string name="string3">BP</string> I would like to check for duplicate entries of... (11 Replies)
Discussion started by: raidzero
11 Replies

7. Shell Programming and Scripting

Finding strings through multiple lines

Hi, I need to search for a multiple line pattern and remove it the pattern is search for (ln number) <TABLE name=*> and if 3 lines below that the line is (ln number) </TABLE> Then remove those 4 lines. Thank you (14 Replies)
Discussion started by: legolad
14 Replies

8. Shell Programming and Scripting

Find position of character in multiple strings in a file

Greetings. I have a file with information like this: AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU?AMNDHRKEOEU? AMNDHRKEEU?AMNDHREOEU? AMNDHREU?AHRKEOEU?AMNDHRKEU?AMNDKEOEU? What I need to extract is the position, in every line, of every occurrence of '?' A desired output would be something... (6 Replies)
Discussion started by: Twinklefingers
6 Replies

9. Shell Programming and Scripting

Exclude lines in a file with matches with multiple Strings using egrep

Hi I have a txt file and I would like to use egrep without using -v option to exclude the lines which matches with multiple Strings. Let's say I have some text in the txt file. The command should not fetch lines if they have strings something like CAT MAT DAT The command should fetch me... (4 Replies)
Discussion started by: Sathwik
4 Replies

10. UNIX for Beginners Questions & Answers

How to pass strings from a list of strings from another file and create multiple files?

Hello Everyone , Iam a newbie to shell programming and iam reaching out if anyone can help in this :- I have two files 1) Insert.txt 2) partition_list.txt insert.txt looks like this :- insert into emp1 partition (partition_name) (a1, b2, c4, s6, d8) select a1, b2, c4, (2 Replies)
Discussion started by: nubie2linux
2 Replies
All times are GMT -4. The time now is 06:55 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy