0

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.

6
  • what exactly do you want the output to be? Why not type it up like you typed up mnthFile? Commented Aug 8, 2015 at 1:31
  • This looks like a homework problem or interview question. What have you tried so far that doesn't work? Commented Aug 8, 2015 at 2:40
  • i am new to shell script and unix commands. i read all man pages of uniq and sort and grep. but i did't find the solution Commented Aug 8, 2015 at 3:07
  • We don't need to know what you've read, we need to know what you've tried. Commented Aug 8, 2015 at 3:57
  • The question is somewhat wrong since there can be 2 Sriram coming on same date there should be something unique in them atleast roll no or something of that sort which will be in some pattern. The best way to do would be reject such input. Commented Aug 8, 2015 at 7:59

1 Answer 1

1

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
?
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.