Artificial intelligent assistant

Print line numbers of files I am searching through I am looking for a way to print the line number of files my script is going through. I have found `$LINENO` but when I do `echo 'Found foo in file' $(basename $foo) 'on line' $LINENO >> foo.csv` It prints the results "Found foo in file Foo on line 97 in the `.csv` file. I would like it to print the line in the file it is looking at and not the line the script it is on. How can I echo the line number in the files?

You can use `nl` to number the lines of the file before going through them:


$ cat testfile
a
b
c
$ nl -b a testfile
1 a
2 b
3 c


Note that `-b a` is required because, by default, `nl` doesn't number blank lines.

Of course, this will be inefficient if your file is very large as it will go through the file twice.

Perhaps a better alternative is to use your own line counter so that you need only go through the file once:


COUNT=0
while read -r line; do
COUNT=$(( $COUNT + 1 ))
if [ ... ];then
# Do things to the line
# Make use of COUNT to show line number
fi
done < your_file_here


This will only work if you're processing the file line-by-line. It won't work if you're using `grep` for example.

xcX3v84RxoQ-4GxG32940ukFUIEgYdPy bc0bd791e703ac19a6cc47be8762d389