Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

unix scripting: check file content

Posted on 2010-09-17
4
Medium Priority
?
425 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 1000 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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…
Using libpcap/Jpcap to capture and send packets on Solaris version (10/11) Library used: 1.      Libpcap (http://www.tcpdump.org) Version 1.2 2.      Jpcap(http://netresearch.ics.uci.edu/kfujii/Jpcap/doc/index.html) Version 0.6 Prerequisite: 1.      GCC …
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…
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.
Suggested Courses
Course of the Month11 days, 18 hours left to enroll

916 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