LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 11-21-2014, 01:24 AM   #1
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Rep: Reputation: 15
Need help comparing output of grep -c to a value using -gt and -lt in bash


Hi all,

I have a folder of .fa files. I want to get rid of files that have more than 50 or fewer than four occurrences of the greater-than symbnol (">"). Here is the code I've come up with so far but I don't think the output of grep -c is being read as a number. I feel like bc needs to come into play somehow but I can't figure out how to make bash see this variable as a number. Any help would be greatly appreciated!

Code:
mkdir more_than_50_seqs
mkdir fewer_than_4_seqs
for FILENAME in *.fa
do
NUMBER_OF_OGs=`grep -c \> $FILENAME`
echo $NUMBER_OF_OG
if [$NUMBER_OF_OGs -gt 50]
then
	mv $FILENAME ./more_than_50_seqs/
elseif [$NUMBER_OF_OGs -lt 4]
then
	mv $FILENAME ./fewer_than_4_seqs/
else
	echo
fi
done
Thanks!
Kevin
 
Old 11-21-2014, 03:13 AM   #2
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 664

Rep: Reputation: 80
Quote:
Code:
mkdir more_than_50_seqs
mkdir fewer_than_4_seqs    
for FILENAME in *.fa   
do
NUMBER_OF_OGs=`grep -c \> $FILENAME`  
echo $NUMBER_OF_OG
if [$NUMBER_OF_OGs -gt 50]
then
	mv $FILENAME ./more_than_50_seqs/
elseif [$NUMBER_OF_OGs -lt 4]
then
	mv $FILENAME ./fewer_than_4_seqs/
else
	echo
fi
done
# making dir but not checking if they already exist... not a wise idea

Are you checking the count of duplicate .fa files or counting all .fa files ?
in both cases i dont see grep -c or the code you wrote is much of a use here rather using wc -l while listing files can give you count of occurences of the file.

better see man page first.

here is what you can do once you decide what you want to count:
get the count , compare it and check if teh dir you want exists
if not make one else move the file to a dir depending on the count.

Last edited by SAbhi; 11-21-2014 at 03:14 AM.
 
Old 11-21-2014, 03:22 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,243

Rep: Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684Reputation: 2684
I see a few issues:

1. No space either side of [ and ], remember that [ is a command, so you would not write lsdir, you would write ls dir

2. elseif is incorrect in bash, you want to use elif

3. If you use (()) instead of [] you can perform arithmetic comparisons with familiar symbols
Code:
if (( NUMBER_OF_OGs > 50 ))
4. Personal choice, but when using non-alphanumeric characters I prefer to place quotes around strings to help protect it against shell expansion
Code:
NUMBER_OF_OGs=$(grep -c '\>' $FILENAME)
5. I do not believe the escape is required inside the single quotes either

6. $() is clearer than `` and can be nested easily

7. Based on your description, I am not 100% sure you are performing the correct test?? You say you want files with greater than 50 occurrences of '>', however the -c option for grep will return the number of lines
that match your criteria and not the number of '>' that appear, ie. you could have a file with one line and 51 '>' but this would not return as the -c will return only 1
 
1 members found this post helpful.
Old 11-21-2014, 07:22 PM   #4
kmkocot
Member
 
Registered: Dec 2007
Location: Queensland, Australia
Posts: 122

Original Poster
Rep: Reputation: 15
Thanks guys!

Grail, thanks for noticing that I'm only counting lines with a greater than symbol but FYI the file format that I'm using will always have one or zero greater than symbols per line. This code with your suggestions did the trick:

Code:
mkdir more_than_50_seqs
mkdir fewer_than_4_seqs
for FILENAME in *.fa
do
NUMBER_OF_OGs=$(grep -c '>' $FILENAME)
echo $NUMBER_OF_OG
if (( $NUMBER_OF_OGs > 50 ))
then
	mv $FILENAME ./more_than_50_seqs/
elif (( $NUMBER_OF_OGs < 4 ))
then
	mv $FILENAME ./fewer_than_4_seqs/
else
	echo $FILENAME has >4 but <50 sequences
fi
done
Thanks!
Kev
 
Old 11-21-2014, 11:45 PM   #5
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 86

Rep: Reputation: Disabled
Code:
#!/bin/bash

if ! test -d "./more_than_50_seqs"; then
    mkdir ./more_than_50_seqs;
fi

if ! test -d "./less_than_4_seqs"; then
    mkdir ./less_than_4_seqs;
fi

for i in *.fa; do
    if [[ $(grep -c ">" $i) -gt 50 ]]; then
        mv $i ./more_than_50_seqs/;
    elif [[ $(grep -c ">" $i) -lt 4 ]]; then
        mv $i ./less_than_4_seqs/;
    fi
done
 
Old 11-21-2014, 11:54 PM   #6
nbritton
Member
 
Registered: Jun 2013
Location: Dubuque, IA
Distribution: Red Hat Enterprise Linux, Mac OS X, Ubuntu, Fedora, FreeBSD
Posts: 86

Rep: Reputation: Disabled
You shouldn't use upper case variable names.

http://wiki.bash-hackers.org/scripti...variable_names
http://wiki.bash-hackers.org/scripting/style
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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, SED, GREP or ? (Comparing Files) cinntech Programming 2 02-20-2013 04:26 PM
Bash loop using output of grep not working as needed Jim Pye Programming 7 01-16-2008 11:27 PM
grep output on stdout and grep output to file don't match xnomad Linux - General 3 01-13-2007 05:56 AM
parsing a string from grep output in bash xpromisex Programming 2 11-12-2006 10:12 AM
comparing two documents using grep? dr_zayus69 Linux - Software 5 12-16-2004 11:28 PM


All times are GMT -5. The time now is 05:59 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration