LinuxQuestions.org
Register a domain and help support LQ
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 01-30-2013, 03:57 PM   #1
Drigo
Member
 
Registered: Jun 2009
Posts: 111

Rep: Reputation: 17
Cool Bash Script to tell if a word is not! in a txt file??


So, I have around 550 *.txt files that go under a process.
The output of that process has to finish with "all done"

So I want to create a bash script that checks each file, if the string "all done" is not found, I want to flag that file and output it.
Thanks in advance,
 
Old 01-30-2013, 04:29 PM   #2
frieza
Senior Member
 
Registered: Feb 2002
Location: harvard, il
Distribution: Ubuntu 11.4,DD-WRT micro plus ssh,lfs-6.6,Fedora 15,Fedora 16
Posts: 3,200

Rep: Reputation: 397Reputation: 397Reputation: 397Reputation: 397
try
Code:
valuetocompare=`cat file.txt | grep "all done" | wc -l`
then use an if then else statement to act on the file if the value is a zero
 
Old 01-30-2013, 04:57 PM   #3
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 170

Rep: Reputation: 56
Could also use

Code:
grep -c "all done" file.txt
 
Old 01-30-2013, 07:10 PM   #4
allend
Senior Member
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware-current
Posts: 4,437

Rep: Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356Reputation: 1356
Code:
#!/bin/bash
shopt -s globstar

for file in **/*.txt; do
  if [[ ! $(grep -q "all done" "$file") ]]; then
    echo "$file";
  fi
done
 
Old 01-31-2013, 05:01 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
No need for the test brackets:

Code:
#!/bin/bash
shopt -s globstar

for file in **/*.txt; do
  if ! grep -q "all done" "$file" ; then
    echo "$file"
  fi
done
In fact, there's no need for if, either.

Code:
for file in **/*.txt; do
  grep -q "all done" "$file" || echo "$file"
done

For more complex file matching, use a while+read loop and find:

How can I recursively search all files for a string?
http://mywiki.wooledge.org/BashFAQ/008


PS: globstar will choke up your shell if there happen to be any recursively-pointing symlinks in the subdirectories.

Last edited by David the H.; 01-31-2013 at 05:04 PM.
 
1 members found this post helpful.
  


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
Bash script to compare numbers in a txt file leopard86 Programming 6 09-11-2012 01:10 AM
bash shell script read file word by word part 2 justina Programming 7 01-25-2011 02:19 PM
[SOLVED] bash shell script read file word by word. justina Programming 15 01-22-2011 11:12 AM
how to get the specific text from a txt file in bash script deepakdeore2004 Programming 8 04-30-2010 07:35 AM
Does anyone know of a bash script can search & replace txt in a file. jimwelc Linux - Newbie 6 09-15-2008 01:13 AM


All times are GMT -5. The time now is 07:03 PM.

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