I have few lines in a text file. I am trying to get the line number of uncommented line from the text file using unix shell script.
For example : I want the line number of Orange from the below text file. Here expected answer is 4 since the line 2 is commented.
Apple
#Orange
grapes
Orange
sea
April 18, 2015, 2:50pm
2
Hello and welcome to the forums.
Is this part of homework?
If so, please use the specific section: http://www.unix.com/homework-and-coursework-questions/
and follow its sticky guide that apply to such requests.
If it is not homework, what have you tried so far?
Have a nice weekend
One way of doing it:
$ awk '$0 == "Orange" {print NR }' infile
4
1 Like
Thanks fpmurphy.
I also tried and got it using sed.
sed -n '/^Orange/=' file
Hello Sea
This is not for homework. I need this for my official scripting. I just gave a sample to try out.
Thanks for giving homework link.
Note that the awk
script fpmurphy suggested will give you the line number of lines that contain an exact match for the desired text. Your sed
script will give you the line number of lines that start with the desired text. If you want an exact match with sed
, you might want to try:
sed -n '/^Orange$/=' file