11

My aim is to get the line number ($lineof) of a string which matches a line in /etc/crontab.

To give 0 8 * * * Me echo "start working please" and get this is the line number 13 from /etc/crontab.

Given this file /tmp/crontab :

# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
#
0 17 * * * Me echo "end of work"
0 8 * * * Me echo "start working please"
1 3 2 4 2 Me ls -la

I do something like that for the moment:

cat /etc/crontab | grep -v "#" | grep "Me" > /tmp/task.cron
i=1
while read -r content
do
        line=$content
        # lineof=$LINENO
        nbline=${i}
        minute=$(echo "$line" | awk '{print $1}')  #0-59
        hour=$(echo "$line" | awk '{print $2}')    #0-23
        dom=$(echo "$line" | awk '{print $3}')     #1-31
        month=$(echo "$line" | awk '{print $4}')   #1-12
        dow=$(echo "$line" | awk '{print $5}')     #0-6 (0=Sunday)
        cmd=$(echo "$line" | awk '{$1=$2=$3=$4=$5=$6=""; print $0}')    #command
        cmd=$(echo "$cmd" | tr ' ' _)
        str=$str' '$nbline' "'$minute'" "'$hour'" "'$dom'" "'$month'" "'$dow'" "'$user'" "'$cmd'" '
        i=$(($i+1))
done < /tmp/task.cron

$nbline give me the line of the content in /tmp/task.cron

$LINENO give me the line of the current script (which execute the program)

I want $lineof give me the number of the line in /etc/crontab

6
  • 1
    The full script is a bit overkilling: why not while read -r minute hour dom month ... instead of catching the full line and then slicing it? Try to explain clearly what is your final goal - given input, desired output - so we can assist better.
    – fedorqui
    Jul 15, 2015 at 13:41
  • 1
    If you ever find yourself writing x=$(... cut ...); y=$(... cut ...) or x=$(... awk ...); y=$(... awk ...) you should stop because you are probably doing something wrong already. Jul 15, 2015 at 13:49
  • Do you have to take into consideration commented lines? That is, if a user says line 3 and you have two commented lines in the top of the file, what line should you remove?
    – fedorqui
    Jul 15, 2015 at 14:02
  • I did this complicated logic because I don't want to care about comments. There is few line before the preplanned tasks that I don't want too.
    – Jérémy
    Jul 15, 2015 at 14:02
  • I don't want to skip any line, I just want to count them. Line 11 come from /etc/crontab
    – Jérémy
    Jul 15, 2015 at 14:56

3 Answers 3

8

To print the line number of your match, use the -n option of grep. Since the pattern contains some special characters, use -F to make them be interpreted as fixed strings and not a regular expression:

grep -Fn 'your_line' /etc/crontab

However, since you want to print some message together with the line number, you may want to use awk instead:

awk -v line='your_line' '$0 == line {print "this is the line number", NR, "from", FILENAME}' /etc/crontab

Test

$ cat a
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# m h dom mon dow user  command
#
0 17 * * * Me echo "end of work"
0 8 * * * Me echo "start working please"
1 3 2 4 2 Me ls -la

With awk:

$ awk -v line='0 8 * * * Me echo "start working please"' '$0 == line {print "this is the line number", NR, "from", FILENAME}' a
this is the line number 13 from a

With grep:

$ grep -Fn '0 8 * * * Me echo "start working please"' a13:0 8 * * * Me echo "start working please"
13:0 8 * * * Me echo "start working please"
3
  • What I want is to give 0 8 * * * Me echo "start working please" and get this is line number 3
    – Jérémy
    Jul 15, 2015 at 14:16
  • It's hard to explain clearly what I want in an other language, I do my best. sorry
    – Jérémy
    Jul 15, 2015 at 14:26
  • 1
    You should give a read to How to Ask, specially on giving a representative sample input with its desired output. I am sure you can do that!
    – fedorqui
    Jul 15, 2015 at 14:27
3

grep --fixed-strings --line-number "${match}" | cut --delimiter=":" --fields=1

0

I finally did like this, found alone :

nbline=1
        while read -r content
        do
                line=$content
                if [ "${line:0:1}" != "#" ]; then #if this is not a comment
                        line=$(echo -e "$line" | grep "$user") #$line keep only lines with the $user choose
                        if [ ! -z "$line" ];then #if this is not a void $line
                                minute=$(echo -e "$line" | awk '{print $1}') #0-59
                                hour=$(echo -e "$line" | awk '{print $2}')   #0-23
                                dom=$(echo -e "$line" | awk '{print $3}')    #1-31
                                month=$(echo -e "$line" | awk '{print $4}')  #1-12
                                dow=$(echo -e "$line" | awk '{print $5}')    #0-6 (0=Sunday)
                                cmd=$(echo -e "$line" | awk '{$1=$2=$3=$4=$5=$6=""; print $0}')    #command
                                cmd=$(echo -e "$cmd" | tr ' ' _) #replace space by '_' because it's annoying later
                                str=$str' "'$nbline'" "'$minute'" "'$hour'" "'$dom'" "'$month'" "'$dow'" "'$user'" "'$cmd'" '
                        fi
                fi
                nbline=$(($nbline+1))
        done < /etc/crontab

I don't need to create an other file and get in $nbline the number of current line in loop. And count all line, even if they are void or commented. That's what I wanted.Gcron: a graphical preplanned task manager.

'#' is the line number of the right content in /etc/crontab.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.