Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unix scripting: check file content

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

Background Still having to process all these year-end "csv" files received from all these sources (including Government entities), sometimes we have the need to examine the contents due to data error, etc... As a "Unix" shop, our only readily …
Why Shell Scripting? Shell scripting is a powerful method of accessing UNIX systems and it is very flexible. Shell scripts are required when we want to execute a sequence of commands in Unix flavored operating systems. “Shell” is the command line i…
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 recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Suggested Courses

715 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