LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
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 09-29-2011, 12:20 PM   #1
francy_casa
LQ Newbie
 
Registered: Sep 2011
Posts: 12

Rep: Reputation: Disabled
grep in pattern matching in a loop


Hi,

I am trying to go through a large file and check if some values exist using the 'grep' command, and I would like to output a file listing the values that exist.
This is what I have….but it does not work obviously…could you please help me figure out why?


Code:
#!/bin/bash
>matches.txt
for i in 'pattern.txt'
do
echo $i
$j='grep -c "$i" myfile.txt'
echo $j
if [$j -ne 0]
then
echo $i >> matches.txt
fi 
done

Last edited by colucix; 09-29-2011 at 12:32 PM. Reason: Added code tags to improve readability
 
Old 09-29-2011, 12:32 PM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
There are many errors in your script. First attempt? Here is a working version:
Code:
#!/bin/bash
> matches.txt
for i in $(cat pattern.txt)
do
  echo $i
  j=$(grep -c "$i" file1)
  echo $j
  if [ $j -ne 0 ]
  then
    echo $i >> matches.txt
  fi
done
Basically you have to use command substitution to put the result of grep into the j variable: the syntax uses backticks ` (not single quotes as in your code) or even better:
Code:
$(command)
Then pay attention to the spaces inside the if/then expression: they are required to separate the square brackets from the expression. Check the differences with your version for other issues and feel free to ask if something is still not clear.
 
Old 09-29-2011, 12:46 PM   #3
francy_casa
LQ Newbie
 
Registered: Sep 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
Thank you colucix!! Yes…it was my first attempt…I had no idea about the command substitution,
The script works great!
Just one more question to understand this further, out of technical curiosity…Is it easy to store in the output file also the results from $j's number of times the match is found? If this is complicated it does not matter (I would not understand this anyway so please don't waste your time…)

Thank you very much!
 
Old 09-29-2011, 01:07 PM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192Reputation: 3192
Just include $j in the echo
 
Old 09-29-2011, 07:03 PM   #5
francy_casa
LQ Newbie
 
Registered: Sep 2011
Posts: 12

Original Poster
Rep: Reputation: Disabled
yes of course…makes sense. Thank you!
 
  


Reply



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
[SOLVED] awk with pipe delimited file (specific column matching and multiple pattern matching) lolmon Programming 4 08-31-2011 12:17 PM
grep or sed to delete matching pattern rbalaa Linux - General 2 07-07-2011 03:28 PM
[SOLVED]matching a pattern with grep kdelover Programming 2 09-18-2009 06:07 AM
Using "OR" type matching pattern with grep command riteshanand40 Linux - Newbie 3 05-06-2009 04:48 AM
pattern matching question - grep cbriscoejr Programming 1 02-09-2006 08:30 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 09:55 AM.

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
Open Source Consulting | Domain Registration