Solved

Linux: Only if line begins with valid date

Posted on 2013-11-18
1
270 Views
Last Modified: 2013-11-18
#!/bin/bash
NOW=$( date +"%s" )
while read MONTH DAY TIME DATA
do
    THEN=$( date -d "$MONTH $DAY $TIME" +"%s" )
        COUNT=$((COUNT+1))
done < $1
echo $COUNT

Open in new window

I want to count all the lines that begin with a valid date.  This code works if a line begins with a valid date but if it does not then it returns an error.
0
Comment
Question by:hankknight
1 Comment
 
LVL 68

Accepted Solution

by:
woolmilkporc earned 500 total points
ID: 39657162
You could check the returncode of the "date" command issued in line 5

#!/bin/bash
NOW=$( date +"%s" )
while read MONTH DAY TIME DATA
do
    THEN=$( date -d "$MONTH $DAY $TIME" +"%s" 2>/dev/null )
       [[ $? -ne 0 ]] && continue
        COUNT=$((COUNT+1))
done < $1
echo $COUNT

Open in new window

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
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…
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…

828 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