Linux - Get Specific Line From Text File Using Just Shell Script - Stack Overflow
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
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.
I know how to iterate through lines, as shown above, but what if I just need to get the contents of
a particular line
do you know the line number? – Mehul Rathod 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:
awk:
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
x=0
want=5
cat lines | while read line; do
x=$(( x+1 ))
if [ $x -eq "$want" ]; then
echo $line
break
fi
done
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
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
7
sed '5q;d' file
Because sed stops reading any lines after the 5th one
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
If for example you want to get the lines 10 to 20 of a file you can use each of these two methods:
or
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
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.)
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.
Easy with perl! If you want to get line 1, 3 and 5 from a file, say /etc/passwd:
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
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