Difference between revisions of "Show certain lines by line number"
(Created page with "Show certain lines by line number of string file == Testing text file == <pre> echo "line 1" > test.txt echo "line 2" >> test.txt echo "line 3" >> test.txt echo "line 4" >> t...") |
m (→Prepare the testing text file) |
||
(2 intermediate revisions by the same user not shown) | |||
Line 1: | Line 1: | ||
Show certain lines by line number of string file | Show certain lines by line number of string file | ||
− | == | + | == Prepare the testing text file == |
<pre> | <pre> | ||
echo "line 1" > test.txt | echo "line 1" > test.txt | ||
Line 9: | Line 9: | ||
echo "line 5" >> test.txt | echo "line 5" >> test.txt | ||
echo "line 6" >> test.txt | echo "line 6" >> test.txt | ||
+ | </pre> | ||
+ | |||
+ | key-in the command {{kbd | key=cat test.txt}} to view the file content | ||
+ | <pre> | ||
+ | line 1 | ||
+ | line 2 | ||
+ | line 3 | ||
+ | line 4 | ||
+ | line 5 | ||
+ | line 6 | ||
</pre> | </pre> | ||
== Approaches == | == Approaches == | ||
− | === Using cat, head & tail commands === | + | === Approach 1: Using cat, head & tail commands === |
1. key-in the command<ref>[https://unix.stackexchange.com/questions/288521/with-the-linux-cat-command-how-do-i-show-only-certain-lines-by-number text processing - With the Linux "cat" command, how do I show only certain lines by number - Unix & Linux Stack Exchange]</ref> | 1. key-in the command<ref>[https://unix.stackexchange.com/questions/288521/with-the-linux-cat-command-how-do-i-show-only-certain-lines-by-number text processing - With the Linux "cat" command, how do I show only certain lines by number - Unix & Linux Stack Exchange]</ref> | ||
<pre> | <pre> | ||
Line 47: | Line 57: | ||
</pre> | </pre> | ||
− | === Using sed command === | + | === Approach 2: Using sed command === |
key-in the command<ref>[https://www.gnu.org/software/sed/manual/sed.html#Common-Commands sed, a stream editor]</ref> | key-in the command<ref>[https://www.gnu.org/software/sed/manual/sed.html#Common-Commands sed, a stream editor]</ref> | ||
<pre> | <pre> |
Latest revision as of 12:25, 2 December 2019
Show certain lines by line number of string file
Contents
Prepare the testing text file[edit]
echo "line 1" > test.txt echo "line 2" >> test.txt echo "line 3" >> test.txt echo "line 4" >> test.txt echo "line 5" >> test.txt echo "line 6" >> test.txt
key-in the command cat test.txt to view the file content
line 1 line 2 line 3 line 4 line 5 line 6
Approaches[edit]
Approach 1: Using cat, head & tail commands[edit]
1. key-in the command[1]
# X: start line number # Y: end line number X=2 Y=4 Z=$(($Y-$X+1)) cat test.txt | head -n $Y | tail -n $Z
result
line 2 line 3 line 4
2. key-in the command if you want to show the line number of each line
# X: start line number # Y: end line number X=2 Y=4 Z=$(($Y-$X+1)) cat -n test.txt | head -n $Y | tail -n $Z
result
2 line 2 3 line 3 4 line 4
Approach 2: Using sed command[edit]
key-in the command[2]
# X: start line number # Y: end line number X=2 Y=4 Z=$(($Y-$X+1)) sed -n -e "$X,$Y p" -e "$Y q" test.txt
result
line 2 line 3 line 4