Using Bash Script to Find Line Number of String in File

Using Bash Script to Find Line Number of String in File

Given that your example only prints the line number of the first occurrence of the string, perhaps you are looking for:

awk '/line/{ print NR; exit }' input-file

If you actually want all occurrences (eg, if the desired output of your example is actually "2\n3\n"), omit the exit.

Bash find line number

You don't need to use find and xargs here. You can use recursive grep like this:

grep -RHn --colour=auto "$1" --include='$2' .

Options:

-n  # for printing line numbers
-R # for recursive grep
-H # for printing file names

How to print the line number where a string appears in a file?


Using grep

To look for word in file and print the line number, use the -n option to grep:

grep -n 'word' file

This prints both the line number and the line on which it matches.

Using awk

This will print the number of line on which the word word appears in the file:

awk '/word/{print NR}' file

This will print both the line number and the line on which word appears:

awk '/word/{print NR, $0}' file

You can replace word with any regular expression that you like.

How it works:

  • /word/

    This selects lines containing word.

  • {print NR}

    For the selected lines, this prints the line number (NR means Number of the Record). You can change this to print any information that you are interested in. Thus, {print NR, $0} would print the line number followed by the line itself, $0.

Assigning the line number to a variable

Use command substitution:

n=$(awk '/word/{print NR}' file)

Using shell variables as the pattern

Suppose that the regex that we are looking for is in the shell variable url:

awk -v x="$url" '$0~x {print NR}' file

And:

n=$(awk -v x="$url" '$0~x {print NR}' file)

How to get the line number of a match?

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"

How to get the line number?

you can use the -n option with grep to make grep output the line number. So, it would be something like this:

 grep -n ^-----$ filename.txt

How to determine the line number of the last occurrence of a string in a file using a shell script?

Using awk:

awk '/Fedora/ { ln = FNR } END { print ln }'

Using grep:

grep -n 'Fedora' | tail -n1 | cut -d: -f1

Using the shell (tested in bash only):

unset ln lnr
while read -r; do
((lnr++))
case "$REPLY" in
*Fedora*) ln="$lnr";;
esac
done < grub.cfg
echo $ln

Get specific line from text file using just shell script

sed:

sed '5!d' file

awk:

awk 'NR==5' file

How to get the line number of a string in another string in Shell

You may use this awk + printf in bash:

awk -v s="$str" '$0 == s {print NR; exit}' <(printf "%b\n" "$sourceStr")

3

Or even this awk without any bash support:

awk -v s="$str" -v source="$sourceStr" 'BEGIN {
split(source, a); for (i=1; i in a; ++i) if (a[i] == s) {print i; exit}}'

3

You may use this sed as well:

sed -n "/^$str$/{=;q;}" <(printf "%b\n" "$sourceStr")

3

Or this grep + cut:

printf "%b\n" "$sourceStr" | grep -nxF -m 1 "$str" | cut -d: -f1

3


Related Topics



Leave a reply



Submit