Sriram P
Sriram P

Reputation: 309

How to take only numbers in file by Shell script

My problem is to find persons attendance , but he has given his date twice with wrong patterns as jul22, 22jul. I want to prove that both are same.

The attendF file data looks like -

PersonsName,month
sriram,jul22 
sriram,22jul 

My idea: first I take the pattern for the months he given in a attendF file
#grep -i "personName" attendF | cut -t"," -k2,2 > mnthFile

mnthFile has data like:

 jul22
 jul23
 22jul
 jul24
 23 jul

I want output to be:
jul22
jul23
jul24
(or)
22jul
23 jul
jul24
there by I can get his correct attendance.

Upvotes: 0

Views: 478

Answers (1)

paxdiablo
paxdiablo

Reputation: 881153

If you're asking how to normalise different dates into a single format so you can compare them, the following normalise function (along with a small test harness) will do that for you.

It first lower-cases the string then removes all characters that are neither alpha nor numeric.

Then, if it's neither alpha-number nor number-alpha, it just returns a suitable error value ?.

However, assuming it is of one of those formats, it separates it into day and month then returns it in a consistent format:

#!/usr/bin/env bash

normalise() {
        # Lowercase it and remove non-alphanumerics.

        str="$(echo "$1" | tr '[A-Z]' '[a-z]' | sed 's/[^a-z0-9]//g')"

        # Check one of the allowed formats.

        if [[ ! "${str}" =~ ^[0-9]+[a-z]+$ ]] ; then
                if [[ ! "${str}" =~ ^[a-z]+[0-9]+$ ]] ; then
                        echo '?'
                        return
                fi
        fi

        # Extract the day andd month, return normalised value.

        day="$(echo "$str" | sed 's/[a-z]//g')"
        mon="$(echo "$str" | sed 's/[0-9]//g')"
        echo "${day}-${mon}"
}

echo $(normalise "Jul 22")
echo $(normalise "jUl-22")
echo $(normalise "juL22")
echo $(normalise "Jul.22")
echo $(normalise "22 jUl")
echo $(normalise "22-juL")
echo $(normalise "22Jul")
echo $(normalise "22.jUl")
echo $(normalise "22.jUl.1977")

The output of that script is:

22-jul
22-jul
22-jul
22-jul
22-jul
22-jul
22-jul
22-jul
?

Upvotes: 1

Related Questions