如何获取匹配的行号?
How to get the line number of a match?
我的目标是获取与 /etc/crontab
中的一行匹配的字符串的行号 ($lineof)。
给予0 8 * * * Me echo "start working please"
,得到this is the line number 13 from /etc/crontab
。
鉴于此文件 /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
我目前正在做类似的事情:
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 }') #0-59
hour=$(echo "$line" | awk '{print }') #0-23
dom=$(echo "$line" | awk '{print }') #1-31
month=$(echo "$line" | awk '{print }') #1-12
dow=$(echo "$line" | awk '{print }') #0-6 (0=Sunday)
cmd=$(echo "$line" | awk '{======""; print [=11=]}') #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
要打印匹配的行号,请使用 grep
的 -n
选项。由于模式包含一些特殊字符,使用 -F
使它们被解释为固定字符串而不是正则表达式:
grep -Fn 'your_line' /etc/crontab
但是,由于您想打印一些消息和行号,您可能想使用 awk
代替:
awk -v line='your_line' '[=11=] == line {print "this is the line number", NR, "from", FILENAME}' /etc/crontab
测试
$ 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
与awk
:
$ awk -v line='0 8 * * * Me echo "start working please"' '[=13=] == line {print "this is the line number", NR, "from", FILENAME}' a
this is the line number 13 from a
与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"
我终于这样了,自己找的:
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 }') #0-59
hour=$(echo -e "$line" | awk '{print }') #0-23
dom=$(echo -e "$line" | awk '{print }') #1-31
month=$(echo -e "$line" | awk '{print }') #1-12
dow=$(echo -e "$line" | awk '{print }') #0-6 (0=Sunday)
cmd=$(echo -e "$line" | awk '{======""; print [=10=]}') #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
我不需要创建其他文件并在 $nbline 中获取循环中的当前行数。并计算所有行,即使它们无效或已注释。这就是我想要的。
'#'为/etc/crontab中右边内容的行号。
grep --fixed-strings --line-number "${match}" | cut --delimiter=":" --fields=1
我的目标是获取与 /etc/crontab
中的一行匹配的字符串的行号 ($lineof)。
给予0 8 * * * Me echo "start working please"
,得到this is the line number 13 from /etc/crontab
。
鉴于此文件 /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
我目前正在做类似的事情:
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 }') #0-59
hour=$(echo "$line" | awk '{print }') #0-23
dom=$(echo "$line" | awk '{print }') #1-31
month=$(echo "$line" | awk '{print }') #1-12
dow=$(echo "$line" | awk '{print }') #0-6 (0=Sunday)
cmd=$(echo "$line" | awk '{======""; print [=11=]}') #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
要打印匹配的行号,请使用 grep
的 -n
选项。由于模式包含一些特殊字符,使用 -F
使它们被解释为固定字符串而不是正则表达式:
grep -Fn 'your_line' /etc/crontab
但是,由于您想打印一些消息和行号,您可能想使用 awk
代替:
awk -v line='your_line' '[=11=] == line {print "this is the line number", NR, "from", FILENAME}' /etc/crontab
测试
$ 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
与awk
:
$ awk -v line='0 8 * * * Me echo "start working please"' '[=13=] == line {print "this is the line number", NR, "from", FILENAME}' a
this is the line number 13 from a
与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"
我终于这样了,自己找的:
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 }') #0-59
hour=$(echo -e "$line" | awk '{print }') #0-23
dom=$(echo -e "$line" | awk '{print }') #1-31
month=$(echo -e "$line" | awk '{print }') #1-12
dow=$(echo -e "$line" | awk '{print }') #0-6 (0=Sunday)
cmd=$(echo -e "$line" | awk '{======""; print [=10=]}') #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
我不需要创建其他文件并在 $nbline 中获取循环中的当前行数。并计算所有行,即使它们无效或已注释。这就是我想要的。
'#'为/etc/crontab中右边内容的行号。
grep --fixed-strings --line-number "${match}" | cut --delimiter=":" --fields=1