Print lines where variables occur more than once using grep


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Print lines where variables occur more than once using grep
# 1  
Old 10-18-2010
Print lines where variables occur more than once using grep

Hello,
I want to only print lines where variables occur more than once using grep.

For eg:

Input:
Code:
$this is a comment
int a,b,c,b;
int b,c;
int d,e;
int f,g,f;
x=y+5;

For the above input, the output would be
Code:
int a,b,c,b;
int f,g,f;


I have done grep '^[ ]*int[ ]*\([a-z][a-z0-9]*,\?\)\{1,\};$' to match all the declaration statements, and print all of them. But I am not able to print only those where a variable occurs more than once. I know I've to tweak my regex a bit, but I've tried most options!

Thanks for any help.


Thanks,
Prasanna

Last edited by Scott; 10-18-2010 at 04:56 AM.. Reason: Code tags, please...
# 2  
Old 10-18-2010
hmm.

tweaking your regex to

Code:
#  grep '^[ ]*int[ ]*\([a-z][a-z0-9]*\).*\1' infile 
int f,g,f;

will match where any single char further down the line matches the first char after "int "

I find it easier to think in awk:

Code:
nawk -F"[ \;]" '
/^int/{f=0;n=split($2,a,",")
  for (x=1;x<n;x++) {
    for (y=x+1;y<=n;y++){
      if (a[x]==a[y]){f=1}
    }
  }
if (f==1) {print}}' infile 

int a,b,c,b;
int f,g,f;

HTH
# 3  
Old 10-18-2010
Thanks a lot for your reply.

Yes, I got to that too. But I would want to match any variable(not only the first), if it occurs more than once.

Thanks,
Prasanna
# 4  
Old 10-18-2010
Code:
$ sed '/\<int\>.*\<\([a-zA-Z0-9_]\{1,33\}\)\>.*\<\1\>/!d' <<!
\$this is a comment
int a,b,c,b;
int b,c;
int d,e;
int f,g,f;
x=y+5;
!
int a,b,c,b;
int f,g,f;
$

Narrative: Find lines with the word int followed somewhere by a variable name word followed somewhere by the same variable name. The \< and \> like ^ and $ must be outside the \(\). The \< and \> work in sed but not always in grep, and stop working with later regex libs, and they become \b.

Regex Tutorial - \b Word Boundaries
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to print N number of lines before and after the grep?

Hi , My record file , need to print up to above (DATA array)(there may be n no lines ) , grep "myvalue" row now .....suggest me some options --- DATA Array--- record type xxxxx sequence type yyyyy 2 3---> data1 /dev/ --- DEVICE --- MAXIMUM_People= data_blocks= MY_value=2 xyz abc ... (0 Replies)
Discussion started by: Huvan
0 Replies

2. Shell Programming and Scripting

awk to find lines containing word that occur multiple times

i have a script that scans a log file every 10 minutes. this script remembers the last line of the log and then uses it to continue monitoring the log when it runs again 10 minutes later. the script searches the log for a string called MaxClients. now, how can i make it so that when the... (7 Replies)
Discussion started by: SkySmart
7 Replies

3. Shell Programming and Scripting

awk: Print fields between two delimiters on separate lines and send to variables

I have email headers that look like the following. In the end I would like to accomplish sending each email address to its own variable, such as: user1@domain.com='user1@domain.com' user2@domain.com='user2@domain.com' user3@domain.com='user3@domain.com' etc... I know the sed to get rid of... (11 Replies)
Discussion started by: tay9000
11 Replies

4. Shell Programming and Scripting

Grep and print next 10 Lines separated by ,

Hi All, I need to grep through a file for a string and print the next ten lines to a file separating the lines with a , and save it as a csv file to open it as a XL file. The 10 lines should be on a sigle row in xl. Any suggesstions please. Note; I dont have a GNU Grep to use -A flag. ... (6 Replies)
Discussion started by: Nani369
6 Replies

5. Shell Programming and Scripting

Print lines before and after..not grep -A

Hi I have this in my file 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11 system-alert-00012: UDP flood! From xxxxxx to yyyyyyyyyy, int ethernet0/2). Occurred 1 times. 2011-04-18 15:32:11... (9 Replies)
Discussion started by: zorrox
9 Replies

6. Shell Programming and Scripting

Print the above and below lines for the grep pattern.

Hi, i would like to get the above and below lines of the grep pattern . For ex : file as below: chk1- aaaa 1-Nov chk2 -aaaa ########## chk1-bbbbbb 1-Nov chk2-bbbbbb ######### my search pattern is date : 1-Nov i need the o/p as below chk1- aaaa 1-Nov (6 Replies)
Discussion started by: expert
6 Replies

7. Shell Programming and Scripting

Print lines between two lines after grep for a text string

I have several very large file that are extracts from Oracle tables. These files are formatted in XML type syntax with multiple entries like: <ROW> some information more information </ROW> I want to grep for some words, then print all lines between <ROW> AND </ROW>. Can this be done with AWK?... (7 Replies)
Discussion started by: jbruce
7 Replies

8. Shell Programming and Scripting

AIX equivalent to GNU grep's -B and -A [print lines after or before matching lines]

Hi folks I am not allowed to install GNU grep on AIX. Here my code excerpt: grep_fatal () { /usr/sfw/bin/gegrep -B4 -A2 "FATAL|QUEUE|SIGHUP" } Howto the same on AIX based machine? from manual GNU grep ‘--after-context=num’ Print num lines of trailing context after... (4 Replies)
Discussion started by: slashdotweenie
4 Replies

9. Shell Programming and Scripting

Print lines after grep

Hi all, I need help in following scenario. I have a file with about 10,000 lines. There are several lines which have word "START" (all upper case) in them. I want to grep line with word "START" and then do the following 1. Print the line number having word "START" 2. Print the next 11 lines. ... (4 Replies)
Discussion started by: jakSun8
4 Replies
Login or Register to Ask a Question