Find a string in all files and echo a message


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Find a string in all files and echo a message
# 1  
Old 02-09-2016
Find a string in all files and echo a message

Hi. I m trying to figure out how to do this.

I have a directory full of files (100 files) and I want to be able to search for a string called "end" at the end of the files (last line or last 5 lines) and echo each file to say "incomplete" if not found.

This is what I have so far.

---
Code:
for file in 'ls';do
    Incomplete='tail -5 $file | grep end';
    if [[ $file -ne "$Incomplete" ]];then
        echo "$file is incomplete.";
    fi
done

----

When i run the script, it says:

-bash: [[: tail -5 $file | grep end: syntax error: invalid arithmetic operator (error token is "$file | grep end")

I have no clue yet. Can someone help to explain? Thanks a ton.
# 2  
Old 02-09-2016
Code:
[[  $file -ne "$Incomplete" ]]

-ne is meant for number comparisons
!= is meant for string comparisons

So,
Code:
[[  $file != "$Incomplete" ]]

Note - your corrected code does not make sense to me. Why would the $file value ever equal the $Incomplete value? The file name is not "end"

Investigate the use of the grep -q command It sets the return value to 0 (OK) if "end" is found in the last five lines, snippet:
Code:
tail -5 $file | grep -q "end"
if [ $? -eq 0 ] ; then
  echo "$file is okay"
else
  echo "$file is not ok"
fi

This User Gave Thanks to jim mcnamara For This Post:
# 3  
Old 02-09-2016
Thanks Jim. I m still learning. I need to read up some more.
# 4  
Old 02-09-2016
The syntax error in the script is due to using apostrophes ' in lieu of backticks ` , so that Incomplete has the string contents tail -5 .... Please note that backticks are deprecated, use the $(...) construct instead for "command substitution".
The comments on the semantics by jim mcnamara still apply.
# 5  
Old 02-09-2016
You could use this in bash as well:
Code:
for f in * ; do
	if tail -5 "$f" | grep -q -i end$
	then	echo "File $f: valid"
	else	echo "File $f: incomplete"
	fi
done

hth
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Find all .sh files in file system and need to replace the string inside .sh files

Hi All, I need to write a script to find all "*.sh" files in /home file system and if any string find "*.sh" files with the name vijay@gmail.com need to replace with vijay.bhaskar@gmail.com. I just understood about the find the command to search .sh files. Please help me on this. find / -name... (3 Replies)
Discussion started by: bhas85
3 Replies

2. Ubuntu

Trying to find files on a system that contain a certain string

I have tried just about every find and grep command possible and I cannot find these damn files!! This is the problem: On the node you just swapped in, there are 5 JPEG files whose names contain the word "intro" in some form. Find all five files from on the entire disk (i.e. from root /). ... (2 Replies)
Discussion started by: dukeu69
2 Replies

3. Shell Programming and Scripting

find duplicate string in many different files

I have more than 100 files like this: SVEAVLTGPYGYT 2 SVEGNFEETQY 10 SVELGQGYEQY 28 SVERTGTGYT 6 SVGLADYNEQF 21 SVGQGYEQY 32 SVKTVLGYEQF 2 SVNNEQF 12 SVRDGLTNSPLH 3 SVRRDREGLEQF 11 SVRTSGSYEQY 17 SVSVSGSPLQETQY 78 SVVHSTSPEAF 59 SVVPGNGYT 75 (4 Replies)
Discussion started by: xshang
4 Replies

4. Shell Programming and Scripting

tcsh - understanding difference between "echo string" and "echo string > /dev/stdout"

I came across and unexpected behavior with redirections in tcsh. I know, csh is not best for redirections, but I'd like to understand what is happening here. I have following script (called out_to_streams.csh): #!/bin/tcsh -f echo Redirected to STDOUT > /dev/stdout echo Redirected to... (2 Replies)
Discussion started by: marcink
2 Replies

5. Shell Programming and Scripting

Need to find a string in files

Hi, I have a directory nsk, files a.sql, b.sql, c.sql, d.sql, assuming that I have line 'INSERT ALL INTO abc' in a.sql and c.sql I need to list a filename, line number and complete line which contains ''INSERT ALL INTO abc' irrespective of case. also in my files mentioned 'INSERT ALL... (4 Replies)
Discussion started by: sunnix
4 Replies

6. Shell Programming and Scripting

How to echo message when file is empty.

If a file is empty then it must print the message "no data found" eg: if then echo "no data found" your help is really appreciated. (3 Replies)
Discussion started by: javeedkaleem
3 Replies

7. Shell Programming and Scripting

How to find particular string in multiple files

Hello friends, I have find a paticular string from the files present in my user for example: a username and password is hardcoded in multiple files which present in the my user.so I have to search about username in which files it is available.there are several dirctories are there,so... (5 Replies)
Discussion started by: sivaranga001
5 Replies

8. Shell Programming and Scripting

find and replace string in a directory files

Hi, I have a directory has DIR1 and the D1 directory has 200+ files. I want change the string from "Bangalore" to "Bangaluru" in all files in the D1 directory. Thanks (2 Replies)
Discussion started by: koti_rama
2 Replies

9. Shell Programming and Scripting

How to find a string inside files

Hi, I would like to know how to get a list of files that contain a specific string inside them. Thanks (12 Replies)
Discussion started by: yoavbe
12 Replies

10. Shell Programming and Scripting

How to capture status code and echo a message

Im trying to execute application and its return code is below IF Status code=o echo "........" else Staus Code =-2 DJRE then echo "......" Can any one help me how to handle the status code and echo some message. (12 Replies)
Discussion started by: laknar
12 Replies
Login or Register to Ask a Question