Differences between revisions 21 and 29 (spanning 8 versions)
Revision 21 as of 2011-03-01 15:14:40
Size: 1803
Editor: sn18
Comment:
Revision 29 as of 2015-03-05 00:26:49
Size: 1921
Editor: izabera
Comment: syntax hl
Deletions are marked like this. Additions are marked like this.
Line 2: Line 2:
== How can I print the n'th line of a file? ==
One dirty (but not quick) way is:
Line 3: Line 5:
= How can I print the n'th line of a file? =
The dirty (but not quick) way would be:
{{{#!highlight bash
sed -n "${n}p" "$file"
}}}
Line 6: Line 9:
{{{
    sed -n ${n}p "$file"
But this reads the entire file even if only the third line is desired, which can be avoided by printing line `$n` using the `p` command, followed by a `q` to exit the script:

{{{#!highlight bash
sed -n "$n{p;q;}" "$file"
Line 9: Line 14:
but this reads the whole input file, even if you only wanted the third line.
Line 11: Line 15:
This one avoids that problem: Another method is to grab the last line from a listing of the first `n` lines:
Line 13: Line 17:
{{{
    sed -n "$n{p;q;}" "$file"
{{{#!highlight bash
head -n "$n" "$file" | tail -n 1
Line 16: Line 20:
At line $n the command "p" is run, printing it, with a "q" afterwards: quit the program.
Line 18: Line 21:
Another way, more obvious to some, is to grab the last line from a listing of the first ''n'' lines:

{{{
   head -n $n $file | tail -n 1
}}}
Line 25: Line 23:
{{{
   awk "NR==$n{print;exit}" file
{{{#!highlight bash
awk "NR==$n{print;exit}" "$file"
Line 28: Line 26:
If you want more than one line, it's pretty easy to adapt any of the previous methods:
Line 30: Line 27:
{{{
   
x=3 y=4
   sed -n "$x,${y}p;${y}q;" "$file" # Print lines $x to $y; quit after $y.
   head -n $y "$file" | tail -n $((y - x + 1))   # Same
   head -n $y "$file" | tail -n +$x     # If your tail supports it
   awk "NR>=$x{print} NR==$y{exit}" "$file" # Same
If more than one line is needed, it's easy to adapt any of the previous methods:

{{{#!highlight bash
x=3 y=4
sed -n "$x,${y}p;${y}q;" "$file" # Print lines $x to $y; quit after $y.
head -n "$y" "$file" | tail -n $((y - x + 1)) # Same
head -n "$y" "$file" | tail -n "+$x" # If your tail supports it
awk "NR>=$x{print} NR==$y{exit}" "$file" # Same
Line 37: Line 36:
In Bash 4, this can also be done with mapfile builtin:
Line 39: Line 37:
{{{
   mapfile -ts $((n-1)) -n 1
   echo "$MAPFILE"
In Bash 4, `mapfile` can be used similarly to `head` while avoiding buffering issues in the event input is a pipe, because it guarantees the [[ FileDescriptor|fd ]] will be seeked to where you left it:

{{{#!highlight bash
# Bash4
{ mapfile -n "$n"; head -n 1; } <"$file"
Line 43: Line 43:
By changing the value of argument for -n, you can get more than one line in the array MAPFILE.
Line 45: Line 44:
=== Note ===
In most cases, you should sanitize your variable n to be sure, that it's not containing any of non-digits, before feeding it to sed or awk. You can do it with such simple code:
Or a counter with a simple `read` loop:
Line 48: Line 46:
{{{
   # Bash
   n=${n//[!0-9]/}
{{{#!highlight bash
# Bash/ksh
m=0
while ((m++ < n)) && read -r _; do
    :
done
Line 52: Line 53:
   # POSIX
   n=$(printf "%s" "$n"|tr -cd '0-9')
head -n 1
Line 55: Line 55:
 . One can argue that an `n` value of `Five brown horses with 3 feet and 9 little piggies.` should yield an error rather than silently behave as though it was really `39`. If you want to code safe, quote your expansions and be done with it. --[[Lhunath]]
Line 57: Line 56:
To read into a variable, it is preferable to use `read` or `mapfile` rather than an external utility. More than one line can be read into the given array variable or the default array `MAPFILE` by adjusting the argument to mapfile's -n option:

{{{#!highlight bash
# Bash4
mapfile -ts $((n-1)) -n 1 x <"$file"
printf '%s\n' "$x"
}}}

=== See Also ===
 * [[BashFAQ/001]]
 * http://wiki.bash-hackers.org/commands/builtin/mapfile

How can I print the n'th line of a file?

One dirty (but not quick) way is:

   1 sed -n "${n}p" "$file"

But this reads the entire file even if only the third line is desired, which can be avoided by printing line $n using the p command, followed by a q to exit the script:

   1 sed -n "$n{p;q;}" "$file"

Another method is to grab the last line from a listing of the first n lines:

   1 head -n "$n" "$file" | tail -n 1

Another approach, using AWK:

   1 awk "NR==$n{print;exit}" "$file"

If more than one line is needed, it's easy to adapt any of the previous methods:

   1 x=3 y=4
   2 sed -n "$x,${y}p;${y}q;" "$file"                # Print lines $x to $y; quit after $y.
   3 head -n "$y" "$file" | tail -n $((y - x + 1))   # Same
   4 head -n "$y" "$file" | tail -n "+$x"            # If your tail supports it
   5 awk "NR>=$x{print} NR==$y{exit}" "$file"        # Same

In Bash 4, mapfile can be used similarly to head while avoiding buffering issues in the event input is a pipe, because it guarantees the fd will be seeked to where you left it:

   1 # Bash4
   2 { mapfile -n "$n"; head -n 1; } <"$file"

Or a counter with a simple read loop:

   1 # Bash/ksh
   2 m=0
   3 while ((m++ < n)) && read -r _; do
   4     :
   5 done
   6 
   7 head -n 1

To read into a variable, it is preferable to use read or mapfile rather than an external utility. More than one line can be read into the given array variable or the default array MAPFILE by adjusting the argument to mapfile's -n option:

   1 # Bash4
   2 mapfile -ts $((n-1)) -n 1 x <"$file"
   3 printf '%s\n' "$x"

See Also


CategoryShell

BashFAQ/011 (last edited 2020-05-07 08:35:17 by intranet)