Solved

unix scripting: check file content

Posted on 2010-09-17
4
410 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Recently, an awarded photographer, Selina De Maeyer (http://www.selinademaeyer.com/), completed a photo shoot of a beautiful event (http://www.sintjacobantwerpen.be/verslag-en-fotoreportage-van-de-sacramentsprocessie-door-antwerpen#thumbnails) in An…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
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.

751 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