LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
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 01-30-2013, 02:57 PM   #1
Drigo
Member
 
Registered: May 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, 03: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,233

Rep: Reputation: 406Reputation: 406Reputation: 406Reputation: 406Reputation: 406
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, 03:57 PM   #3
thesnow
Member
 
Registered: Nov 2010
Location: Minneapolis, MN
Distribution: Ubuntu, Red Hat, Mint
Posts: 172

Rep: Reputation: 56
Could also use

Code:
grep -c "all done" file.txt
 
Old 01-30-2013, 06:10 PM   #4
allend
LQ 5k Club
 
Registered: Oct 2003
Location: Melbourne
Distribution: Slackware64-15.0
Posts: 6,382

Rep: Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761Reputation: 2761
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, 04:01 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
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 04:04 PM.
 
1 members found this post helpful.
  


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

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

All times are GMT -5. The time now is 06:04 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