Solved

unix scripting: check file content

Posted on 2010-09-17
4
405 Views
Last Modified: 2012-08-13
Hi Experts,
I need a way  to checks a file on content;  it should check on either the number of lines (e.g. 3) or on 3 specific strings in that file.
If that check returns that the file has three lines (or has those 3 specific strings in it), then next cmd can be executed.
Shell is ksh.
Thanks for  your help.
0
Comment
Question by:Watnog
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Dushyant Sharma
ID: 33701123
this one is for bash

i=0
while read line
do
      i=`expr $i + 1`
done < test
echo "i is $i"
j=0
line=`less test | grep "string pattern to match" | wc -c`
echo $line
if [ $i -gt 2 -o $line -gt 0 ]
then
      echo "execute command"
fi
0
 
LVL 29

Expert Comment

by:MikeOM_DBA
ID: 33701576
Try this:


if [[ `wc -l <MyFile.txt` -eq 3 || `egrep -c '[Word1&&Word2&&Word1]' MyFile.txt ` -gt 0 ]] 
then
  echo "Exec_My_Script"
fi

Open in new window

0
 
LVL 29

Accepted Solution

by:
MikeOM_DBA earned 250 total points
ID: 33701695
Another option:


if [[ `wc -l <MyFile.txt` -eq 3 ]] 
then
  echo "Exec_My_Script(1)"
else
  n=0
  for w in String1 String2 String3
  do
    ((n+=`grep -c $w MyFile.txt`))
  done 
  [ $n -eq 3 ] && echo "Exec_My_Script(2)"
fi

Open in new window

0
 

Author Closing Comment

by:Watnog
ID: 33715641
Thanks a lot.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Where to get php 5.6 for AIX 7.1? 5 81
Can I delete authorized_keys in FreeBSD 1 78
Control Number of Log Files -Perl 7 55
Shell Script on AIX 7 63
When you do backups in the Solaris Operating System, the file system must be inactive. Otherwise, the output may be inconsistent. A file system is inactive when it's unmounted or it's write-locked by the operating system. Although the fssnap utility…
I have been running these systems for a few years now and I am just very happy with them.   I just wanted to share the manual that I have created for upgrades and other things.  Oooh yes! FreeBSD makes me happy (as a server), no maintenance and I al…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now