Get the line number in shell script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Get the line number in shell script
# 1  
Old 04-05-2017
Get the line number in shell script

I have one text file

Code:
1 2 3
   a 5
4  4 3

where i want to print the line number

Code:
while read line 
do
line_no=`awk '{print NR, $0}'`
echo 'In line no $line_no'
done <$txt_file

If i run the above code, it will print
'In line no 1 1 2 3'

It prints the line number with the whole line.But I want to print only the line number

Code:
In line no 1
In line no 2
In line no 3


Last edited by vgersh99; 04-05-2017 at 11:44 AM.. Reason: fixed icode tags
# 2  
Old 04-05-2017
Code:
awk '{print "In line no " FNR}' $txt_file

# 3  
Old 04-05-2017
Quote:
Originally Posted by RJG
.
.
.
If i run the above code, it will print
'In line no 1 1 2 3'
.
.
.
May I doubt that? Running that code as is will print In line no $line_no. Changing the single quotes around the echo command to double quotes will print In line no 1 a 5
2 4 4 3
, as those enable variable expansion. $line holds 1 2 3 from the first read, $line_no contains
Code:
1    a 5
2 4  4 3

, i.e. NR and $0 from the rest of the input file redirected from stdin. The while loop is exited after the first iteration. This is because read and awk both read from the input file, and awk loops through it until it's exhausted.

Use tools consistently, either awk only as proposed by vgersh99, or pure shell like
Code:
while read line; do echo "in line $((++CNT))"; done < file1
in line 1
in line 2
in line 3

Here an additional variable needs to be deployed as the shell doesn't provide a line count for stdin by itself.
# 4  
Old 04-05-2017
You may find nl command useful
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to change a number on a specific line with cshell or shell?

Hello all, I need to change a number in a file by adding some residuals respectively To make it clear, I need to add 0.11 to the number between 24-28 (which is below the SECON) for all the lines starting with FRR1 or I need to add 0.13 to the number between 24-28 (which is below the... (9 Replies)
Discussion started by: miriammiriam
9 Replies

2. Shell Programming and Scripting

Tabbing a line a variable number of times in shell

Hi, I had another question. I was wondering if there was a way to tab a line a variable number of times in tcsh. To go into details, I want to tab a line by how deep a file is in its path. So here is an example code: set filea=/blah1/blah2/blah3 set fileb=/blah1/blah2/blah3/blah4 set... (4 Replies)
Discussion started by: chu816
4 Replies

3. Shell Programming and Scripting

How to read from specific line number in shell script?

I have a text file which is having 30000 lines in it. I have to create a xml file for each 10000 lines until all the lines in the text files are written. Also please help how can i get number of lines in the text file in a shell variable? (19 Replies)
Discussion started by: vel4ever
19 Replies

4. Shell Programming and Scripting

sed script - print the line number along with the line

Hi, I want to print the line number with the pattern of the line on a same line using multi-patterns in sed. But i don't know how to do it. For example, I have a file abc def ghi I want to print 1 abc 2 def 3 ghi I know how to write it one line code, but i don't know how to put... (11 Replies)
Discussion started by: ntpntp
11 Replies

5. Shell Programming and Scripting

Shell script to count number of ~ from each line and compare with next line

Hi, I have created one shell script in which it will count number of "~" tilda charactors from each line of the file.But the problem is that i need to count each line count individually, that means. if line one contains 14 "~"s and line two contains 15 "~"s then it should give an error msg.each... (3 Replies)
Discussion started by: Ganesh Khandare
3 Replies

6. 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

7. Shell Programming and Scripting

current line number in shell script

Hi I am using korn shell is there a built in or ny other way to get the current line number so i can write the current line number to the logfile to aid in debugging like logmsg $lineno $date logmsg is a helper function that helps in logging messages to a log file regards (3 Replies)
Discussion started by: xiamin
3 Replies

8. Shell Programming and Scripting

Shell Script to identify the line number containing a particular "string"

Hi, I have a log file, where i am required to identify the line number, where a particular string/line appears in the log file. And then copy 200 lines above that line number to a new file. Can someone provide pointers on how to write this script or what command to be used ? Any... (2 Replies)
Discussion started by: kk2202
2 Replies

9. Shell Programming and Scripting

calling 'n' number of shell scripts based on dependency in one shell script.

Hello gurus, I have three korn shell script 3.1, 3.2, 3.3. I would like to call three shell script in one shell script. i m looking for something like this call 3.1; If 3.1 = "complete" then call 3.2; if 3.2 = ''COMPlete" then call 3.3; else exit The... (1 Reply)
Discussion started by: shashi369
1 Replies

10. Shell Programming and Scripting

Unix Script with line number at beginning of each line.

Could anybody help me. I need to create a script that reads a text file from STDIN and prints out the file to STDOUT with line numbers at the beginning of each line. Thanks. (5 Replies)
Discussion started by: mascorro
5 Replies
Login or Register to Ask a Question