Get a specific line number from a text file

Hello! All,
Could you please tell me how to get a specific line number from a text file?

For example below,

ABC
DEF ---> Get this line number, return to an variable
GHI

My OS is Linux.

Thank you so much for your help in advance!

VAR=`grep -n "text" <file>`

1 Like
#!/bin/bash
i=0
while read -r LINE
do
  ((i++))
  case "$LINE" in
   "DEF")  echo "line num: $i";;
  esac
done <"file"

---------- Post updated at 10:43 PM ---------- Previous update was at 10:42 PM ----------

grep -n "DEF" file

---------- Post updated at 10:44 PM ---------- Previous update was at 10:43 PM ----------

sed -n '/DEF/=' file #GNU
1 Like

Hi! bankai & kurumi,
Thanks for your reply, yes, "grep -n" is good.

I am sorry for my unclear question.
Actually here is situation...
DEF is a procedure and will be called for several times.....
What I am going to do is,
I would like to add codings into "that file", and if any result from DEF is wrong, it will display that line number....

For example,
if the result from 2nd DEF is wrong, I will display that line number.....
which is line#5.

ABC
DEF --> 1st DEF
LLL
KKK
DEF --> 2nd DEF
PPP
DEF --> 3rd DEF

Also, in a test file, if we right click "View" and select "What Line Number?" then a line number will display.
Can we do this?

Thanks a lot!