Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the line number of a match?

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

like image 595
Jérémy Avatar asked Jul 15 '15 13:07

Jérémy


People also ask

How to get line number in grep?

Append the -n operator to any grep command to show the line numbers. We will search for Phoenix in the current directory, show two lines before and after the matches along with their line numbers.

How to check line number in file linux?

The wc command is used to find the number of lines, characters, words, and bytes of a file. To find the number of lines using wc, we add the -l option. This will give us the total number of lines and the name of the file.

How to grep particular line from file?

The grep command searches through the file, looking for matches to the pattern specified. To use it type grep , then the pattern we're searching for and finally the name of the file (or files) we're searching in. The output is the three lines in the file that contain the letters 'not'.


2 Answers

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"
like image 112
fedorqui 'SO stop harming' Avatar answered Oct 02 '22 23:10

fedorqui 'SO stop harming'


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

like image 36
Alberto Salvia Novella Avatar answered Oct 02 '22 22:10

Alberto Salvia Novella