Hos to get the line number of uncommented line from the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hos to get the line number of uncommented line from the file
# 1  
Old 04-18-2015
Hos to get the line number of uncommented line from the file

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.

Code:
Apple
#Orange
grapes
Orange


Last edited by Don Cragun; 04-19-2015 at 03:36 PM.. Reason: Add CODE tags.
# 2  
Old 04-18-2015
Hello and welcome to the forums.

Is this part of homework?
If so, please use the specific section: https://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
# 3  
Old 04-19-2015
One way of doing it:
Code:
$ awk '$0 == "Orange" {print NR }'  infile
4

This User Gave Thanks to fpmurphy For This Post:
# 4  
Old 04-19-2015
Thanks fpmurphy.

I also tried and got it using sed.
Code:
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.

Last edited by Don Cragun; 04-19-2015 at 03:37 PM.. Reason: Add CODE tags.
# 5  
Old 04-19-2015
Quote:
Originally Posted by Brennon
Thanks fpmurphy.

I also tried and got it using sed.
Code:
sed -n '/^Orange/=' file

... ... ...
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:
Code:
sed -n '/^Orange$/=' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to replace line through sed , but without uncommented?

I am looking to replace minimum password value in login.defs file. it does change , but it change the commented one also. please 1. my script to be change 12 in the below line... sed -i '/PASS_MIN_LEN/c\PASS_MIN_LEN 12' /etc/login.defs action- It does change, but it change in 2 place. ... (3 Replies)
Discussion started by: yash_message
3 Replies

2. Shell Programming and Scripting

sed command to replace a line in a file using line number from the output of a pipe.

Sed command to replace a line in a file using line number from the output of a pipe. Is it possible to replace a whole line piped from someother command into a file at paritcular line... here is some basic execution flow.. the line number is 412 lineNo=412 Now i have a line... (1 Reply)
Discussion started by: vivek d r
1 Replies

3. Shell Programming and Scripting

sed command to replace a line at a specific line number with some other line

my requirement is, consider a file output cat output blah sdjfhjkd jsdfhjksdh sdfs 23423 sdfsdf sdf"sdfsdf"sdfsdf"""""dsf hellow there this doesnt look good et cetc etc etcetera i want to replace a line of line number 4 ("this doesnt look good") with some other line ... (3 Replies)
Discussion started by: vivek d r
3 Replies

4. Linux

Problem editting the first occurence of a pattern in the first uncommented line

Hi I have to replace a pattern found in the first uncommented line in a file. The challenge I'm facing is there are several such similar lines but I have to edit only the first uncommented line. Eg: #this is example #/root/xyz:Old_Pattern /root/xyz:Old_Pattern /root/xyz:Old_Pattern ... (10 Replies)
Discussion started by: Stoner008
10 Replies

5. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

6. Shell Programming and Scripting

extract a line from a file by line number

Hi guys, does anyone know how to extract(grep) a line from the file, if I know the line number? Thanks a lot. (9 Replies)
Discussion started by: aoussenko
9 Replies

7. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

8. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

9. Shell Programming and Scripting

Appending the line number and a seperator to each line of a file ?

Hi, I am a newb as far as shell scripting and SED goes so bear with me on this one. I want to basically append to each line in a file a delimiter character and the line's line number e.g Change the file from :- aaaaaa bbbbbb cccccc to:- aaaaaa;1 bbbbbb;2 cccccc;3 I have worked... (4 Replies)
Discussion started by: pjcwhite
4 Replies

10. Shell Programming and Scripting

Get line form file by line number

I want to get a specific line form file by line number. For example I have file named bla File looks like this cat bla Madrid Paris Berlin Vladivostok I want to put line 3 (Berlin) into variable X. How to do that? (3 Replies)
Discussion started by: corny
3 Replies
Login or Register to Ask a Question