search multiple patterns


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting search multiple patterns
# 1  
Old 06-20-2011
search multiple patterns

I have two lists in a file that look like
a b
b a
e f
c d
f e
d c

I would like a final list
a b
c d
e f

I've tried multiple grep and awk but can't get it to work
# 2  
Old 06-20-2011
grep can't do it because grep recalls nothing about previous lines. Even in awk you have to tell it to remember, which I do with the associative array A.

Code:
$ echo "a b
b a
e f
c d
f e
d c
" | awk '{
        # get a b order even if we get b a
        if($1 > $2) { tmp=$1; $1=$2; $2=tmp; }
        if(A[$1] != $2)  { print ; A[$1]=$2; }
}' | sort # sort is necessary for your example data, otherwise we get a b \n e f \n ...
a b
c d
e f
$

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 06-20-2011
Try this awk script which sorts each line and removes the dups...
Code:
awk '{
   n = split($0,a," ")
   for (i=1; i<n; i++)
     for (j=1; j<=n-i; j++)
       if (a[j] > a[j+1]) {
          t=a[j]
          a[j]=a[j+1]
          a[j+1]=t
       }
   for (i=1; i<=n; i++)
       v=sprintf("%s",v?v""a[i]:a[i])
   if (v in x); else print $0
   x[v]=v; v=""
}' file

# 4  
Old 06-20-2011
or Corona's solution in slightly different flavor:
Code:
awk '{
  if(a[$1]==$2 || a[$2]==$1) next; 
  a[$1]=$2;  print
}' data

comes out unsorted.
# 5  
Old 06-20-2011
Don't know how the input file can look like but with the given example, the following works :

Code:
fold -w 1 inputfile | sort | uniq | xargs -n2

---------- Post updated at 12:25 AM ---------- Previous update was at 12:22 AM ----------

Code:
fold -w 1 inputfile | sort -u | xargs -n2

# 6  
Old 06-20-2011
Quote:
Originally Posted by ctsgnb
Don't know how the input file can look like but with the given example, the following works :

Code:
fold -w 1 inputfile | sort | uniq | xargs -n2

---------- Post updated at 12:25 AM ---------- Previous update was at 12:22 AM ----------

Code:
fold -w 1 inputfile | sort -u | xargs -n2

This would miss entries if you have multiple occurrences in one column:

Code:
$ cat data
a b
b a
e f
c d
f e
d c
a c
$ fold -w 1 data | sort | uniq | xargs -n2
a b
c d
e f

# 7  
Old 06-20-2011
godzilla07 did not specify the criteria nor the logic needed to get the output of his example, so i am aware that the code i provide may not fit all cases, but i shot it as is since no futher constraints has been specified so far.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search Multiple patterns and display

Hi, I have scenario like below and need to search for multiple patterns Eg: Test Time Started= secs Time Ended = secc Green test Test Time Started= secs Time Ended = secc Green test Output: I need to display the text starting with Test and starting with Time... (2 Replies)
Discussion started by: weknowd
2 Replies

2. Shell Programming and Scripting

Search and replace multiple patterns in a particular column only - efficient script

Hi Bigshots, I have a pattern file with two columns. I have another data file. If column 1 in the pattern file appears as the 4th column in the data file, I need to replace it (4th column of data file) with column 2 of the pattern file. If the pattern is found in any other column, it should not... (6 Replies)
Discussion started by: ss112233
6 Replies

3. Shell Programming and Scripting

Search patterns in multiple logs parallelly.

Hi All, I am starting a service which will redirect its out put into 2 logs say A and B. Now for succesful startup of the service i need to search pattern1 in log A and pattern2 in log B which are writen continuosly. Now my requirement is to find the patterns in the increasing logs A and B... (19 Replies)
Discussion started by: Girish19
19 Replies

4. Shell Programming and Scripting

How to search multiple patterns and remove lines from a file?

Hi, I have a file content as below. Table : PAYR Displayed fields: 15 of 15 Fixed columns: 4 List width 0999... (4 Replies)
Discussion started by: shirdi
4 Replies

5. Shell Programming and Scripting

How to search Multiple patterns in unix

Hi, I tried to search multiple pattern using awk trans=1234 reason=LN MISMATCH rec=`awk '/$trans/ && /'"$reason"'/' file` whenevr i tried to run on command promt it is executing but when i tried to implment same logic in shell script,it is failing i.e $rec is empty ... (6 Replies)
Discussion started by: ns64110
6 Replies

6. Shell Programming and Scripting

awk: Multiple search patterns & print in an one liner

I would like to print result of multiple search pattern invoked from an one liner. The code looks like this but won't work gawk -F '{{if ($0 ~ /pattern1/) pat1=$1 && if ($0 ~ /pattern2/) pat2=$2} ; print pat1, pat2}' Can anybody help getting the right code? (10 Replies)
Discussion started by: sdf
10 Replies

7. Shell Programming and Scripting

Search multiple patterns in multiple files

Hi, I have to write one script that has to search a list of numbers in certain zipped files. For eg. one file file1.txt contains the numbers. File1.txt contains 5,00,000 numbers and I have to search each number in zipped files(The number of zipped files are around 1000 each file is 5 MB) I have... (10 Replies)
Discussion started by: vsachan
10 Replies

8. Shell Programming and Scripting

Perl - How to search a text file with multiple patterns?

Good day, great gurus, I'm new to Perl, and programming in general. I'm trying to retrieve a column of data from my text file which spans a non-specific number of lines. So I did a regexp that will pick out the columns. However,my pattern would vary. I tried using a foreach loop unsuccessfully.... (2 Replies)
Discussion started by: Sp3ck
2 Replies

9. Shell Programming and Scripting

Perl: Match a line with multiple search patterns

Hi I'm not very good with the serach patterns and I'd need a sample how to find a line that has multiple patterns. Say I want to find a line that has "abd", "123" and "QWERTY" and there can be any characters or numbers between the serach patterns, I have a file that has thousands of lines and... (10 Replies)
Discussion started by: Juha
10 Replies

10. UNIX for Dummies Questions & Answers

How to parameterize multiple search patterns and generate a new file

I have one file: 123*100*abcd*10 123*101*abcd*-29*def 123*100*abcd*-10 123*102*abcd*-105*asd I would like to parameterize the search patterns in the following way so that the user could dynamically change the search pattern. *100* and *- (ie *minus) *102* and *- The output that is... (6 Replies)
Discussion started by: augustinep
6 Replies
Login or Register to Ask a Question