Solved

unix scripting: check file content

Posted on 2010-09-17
4
404 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:ddsh79
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

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…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

707 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

13 Experts available now in Live!

Get 1:1 Help Now