You are on page 1of 6

17/01/2020 linux - Get specific line from text file using just shell script - Stack Overflow

Get specific line from text file using just shell script
Asked 6 years, 3 months ago Active 2 months ago Viewed 201k times

I am trying to get a specific line from a text file.

So far, online I have only seen stuff like sed, (I can only use the sh -not bash or sed or anything
91 like that). I need to do this only using a basic shell script.

cat file | while read line


do
#do something
29
done

I know how to iterate through lines, as shown above, but what if I just need to get the contents of
a particular line

linux shell unix sh

edited Sep 8 '15 at 8:37 asked Oct 11 '13 at 21:29


Josip Rodin GangstaGraham
528 4 11 6,339 8 37 53

do you know the line number? – Mehul Rathod Oct 11 '13 at 21:30

1 Then you get to count. – Ignacio Vazquez-Abrams Oct 11 '13 at 21:30

yes, the line number is 5 @MehulRathod –  GangstaGraham Oct 11 '13 at 21:30

3 Why is cat okay but sed is not? That makes no sense. – William Pursell Oct 11 '13 at 23:50

5 Because no-one can say no to cat . Aw... cute cat ! – user1974640 Aug 6 '15 at 15:58

9 Answers

sed:

184 sed '5!d' file

awk:

awk 'NR==5' file

answered Oct 11 '13 at 21:40


Kent
157k 28 175 239

What about with the sh command, I cannot use sed, awk. I should make this more clear in the question. –
By using our site, you acknowledge
  GangstaGraham Octthat youathave
11 '13 read and understand our Cookie Policy, Privacy Policy, and
21:45
our Terms of Service.

https://stackoverflow.com/questions/19327556/get-specific-line-from-text-file-using-just-shell-script 1/6
17/01/2020 linux - Get specific line from text file using just shell script - Stack Overflow
@GangstaGraham you said you know how to iterate through lines, how about adding a counter? if the
counter reaches your target line number, get the line and break the loop. does it help? – Kent Oct 11 '13
at 21:51

4 @KanagaveluSugumar read sed's info page. 5!d means delete all lines except 5. shell var is possible,
you need double quotes. – Kent Apr 24 '14 at 10:15

12 I would suggest adding another variant: sed -n 5p This seems more logical to remember for newbies,
because -n means "no output by default" and p stands for "print", and there's no potentially confusing
mention of deleting (when people talk of files, deleting lines tends to mean something different). –
 Josip Rodin Sep 8 '15 at 8:13

1 @JosipRodin you are right, -n '5p' works for this problem too. The difference here is, with 5!d you
can add -i to write the change back to the file. however, with -n 5p you have to sed -n '5p' f >
f2&& mv f2 f again, for this question, I am agree with your opinion. – Kent Sep 8 '15 at 9:45

Assuming line is a variable which holds your required line number, if you can use head and
tail , then it is quite simple:

16
head -n $line file | tail -1

If not, this should work:

x=0
want=5
cat lines | while read line; do
x=$(( x+1 ))
if [ $x -eq "$want" ]; then
echo $line
break
fi
done

edited Aug 2 '17 at 10:29 answered Oct 11 '13 at 21:52


Victor Zamanian micromoses
2,741 20 28 3,730 1 13 20

This -eq comparison is for integers, so it wants a line number, not line content ( $line ). This has to be
fixed by defining e.g. want=5 prior to the loop, and then using the -eq comparison on $want . [moved
from a rejected edit] – Josip Rodin Oct 31 '15 at 14:32

1 @JosipRodin I made an independent edit suggestion based on your comment, as I agree with it. Hopefully
this time it won't be rejected. – Victor Zamanian Aug 2 '17 at 10:30

You could use sed -n 5p file .

You can also get a range, e.g., sed -n 5,10p file .


15
edited Mar 19 '16 at 13:59 answered Mar 4 '16 at 13:36
nomasprime
885 8 15

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/19327556/get-specific-line-from-text-file-using-just-shell-script 2/6
17/01/2020 linux - Get specific line from text file using just shell script - Stack Overflow

Best performance method

7
sed '5q;d' file

Because sed stops reading any lines after the 5th one

Update experiment from Mr. Roger Dueck

I installed wcanadian-insane (6.6MB) and compared sed -n 1p /usr/share/dict/words and sed


'1q;d' /usr/share/dict/words using the time command; the first took 0.043s, the second only
0.002s, so using 'q' is definitely a performance improvement!

edited Nov 8 '19 at 7:32 answered Jun 5 '15 at 2:01


faithonour
81 1 3

1 This is also commonly written: sed -n 5q – William Pursell Sep 9 '15 at 15:59

3 I like this solution because sed stops reading any lines after the 5th one. –
 Anthony G - justice for Monica Mar 9 '16 at 16:21

I installed wcanadian-insane (6.6MB) and compared sed -n 1p /usr/share/dict/words and sed


'1q;d' /usr/share/dict/words using the time command; the first took 0.043s, the second only
0.002s, so using 'q' is definitely a performance improvement! – Roger Dueck Jul 4 '19 at 15:17

If for example you want to get the lines 10 to 20 of a file you can use each of these two methods:

5 head -n 20 york.txt | tail -11

or

sed -n '10,20p' york.txt

p in above command stands for printing.

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/19327556/get-specific-line-from-text-file-using-just-shell-script 3/6
17/01/2020 linux - Get specific line from text file using just shell script - Stack Overflow

Here's what you'll see:

answered May 23 '16 at 18:08


Mona Jalal
17.6k 38 137 249

The standard way to do this sort of thing is to use external tools. Disallowing the use of external
tools while writing a shell script is absurd. However, if you really don't want to use external tools,
2 you can print line 5 with:

i=0; while read line; do test $((++i)) = 5 && echo "$line"; done < input-file

Note that this will print logical line 5. That is, if input-file contains line continuations, they will
be counted as a single line. You can change this behavior by adding -r to the read command.
(Which is probably the desired behavior.)

edited Oct 12 '13 at 0:16 answered Oct 11 '13 at 23:56


William Pursell
151k 39 222 253

1 $((++i)) appears to be a bashism; if the OP is restricted in using external tools, I wouldn't assume
they'll have access to more than a plain /bin/sh – Josip Rodin Sep 8 '15 at 8:20

@JosipRodin No, it's a POSIX feature (but support for ++ increments is specifically marked as optional).
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
– tripleee Sep 8 '15 at 9:56
our Terms of Service.

https://stackoverflow.com/questions/19327556/get-specific-line-from-text-file-using-just-shell-script 4/6
17/01/2020 linux - Get specific line from text file using just shell script - Stack Overflow
@tripleee it doesn't work with modern dash as /bin/sh, so I would not rely upon it. – Josip Rodin Sep 8 '15
at 18:37

But a simple workaround like $((i+=1)) works in Dash, too. – tripleee Sep 9 '15 at 4:00

$(($i+1)) is the simple workaround I was thinking of. – Josip Rodin Sep 13 '15 at 21:55

In parallel with William Pursell's answer, here is a simple construct which should work even in
the original v7 Bourne shell (and thus also places where Bash is not available).
1
i=0
while read line; do
i=`expr "$i" + 1`
case $i in 5) echo "$line"; break;; esac
done <file

Notice also the optimization to break out of the loop when we have obtained the line we were
looking for.

edited May 23 '17 at 11:54 answered Sep 9 '15 at 4:04


Community ♦ tripleee
1 1 111k 17 166 214

Easy with perl! If you want to get line 1, 3 and 5 from a file, say /etc/passwd:

-1 perl -e 'while(<>){if(++$l~~[1,3,5]){print}}' < /etc/passwd

edited Mar 23 '16 at 15:21 answered Mar 23 '16 at 13:38


dagelf
804 1 5 11

seq 5 | perl -ne 'print if $. ~~ [1, 4, 5]' but smartmatch is experimental and it's use
discouraged – Sorin Nov 21 '19 at 8:47

Not a single one of the other solutions are this concise, or allows this much flexibility. (Why does it seem
that everything that saves time and makes things easier, is "discouraged" by "smart people", are we all
supposed to stare at screens all day?) – dagelf Dec 3 '19 at 10:45

line=5; prep=`grep -ne ^ file.txt | grep -e ^$line:`; echo "${prep#$line:}"

-1 edited Nov 24 '16 at 23:35 answered Nov 24 '16 at 23:34


Benjamin W. Oder
26k 15 59 67 1 1

3 could you describe a little at least why this work to make it clearer to the person who asked the question?
– ted Nov 24 '16 at 23:44
By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
So, the .first grep selects all the lines adding line numbers at their beginnings. Then the second grep
our Terms of Service
selects a specific line by matching the line number at start. And finally the line number is trimmed from the
https://stackoverflow.com/questions/19327556/get-specific-line-from-text-file-using-just-shell-script 5/6
17/01/2020 linux - Get specific line from text file using just shell script - Stack Overflow
line start in echo. – Oder Nov 24 '16 at 23:58

This is both complex and inefficient compared to sed -n 5p , which of course can still be optimized to
something like sed -n '5!d;p;q' – tripleee Dec 15 '19 at 13:07

By using our site, you acknowledge that you have read and understand our Cookie Policy, Privacy Policy, and
our Terms of Service.

https://stackoverflow.com/questions/19327556/get-specific-line-from-text-file-using-just-shell-script 6/6

You might also like