0

I want to show the 3th and the 7th lines in a file only using commands head and tail (I don't want to show the lines between the 3th and the 7th).

5
  • Yeah. I did it with sed but I want the same result using head and tail.
    – Perplexe
    Commented Nov 8, 2019 at 22:25
  • Well I can't make heads or tails of why you want that but I believe the solution below should do what you need
    – jesse_b
    Commented Nov 8, 2019 at 22:26
  • It's just a question from a teacher
    – Perplexe
    Commented Nov 8, 2019 at 22:26
  • that makes it a school assignment ... please include your attempts ... also include an explanation of how your attempts failed
    – jsotola
    Commented Nov 8, 2019 at 22:30
  • I did it on two commands (like in the answer below) but I want it in one command
    – Perplexe
    Commented Nov 8, 2019 at 22:35

2 Answers 2

2

Using the MULTIOS facility in the zsh shell:

$ head -n 7 file | tail -n 5 > >( head -n 1 ) > >( tail -n 1 )
line 3
line 7

That is, extract lines 3 through to 7 with head -n 7 file | tail -n 5 and then get the first and last line of that.

In bash, this would be equivalent of

$ head -n 7 file | tail -n 5 | tee >( head -n 1 ) | tail -n 1 
line 3
line 7

which additionally uses tee to duplicate the data.

1
  • Ok I think function is the solution for the single command thank you .
    – Perplexe
    Commented Nov 8, 2019 at 22:55
2
head -n3 input | tail -n1; head -n7 input | tail -n1

use head to get the first three lines then tail only the last 1. Then use head to get the first seven lines and tail only the last 1.

Note that is actually two commands separated by ;, it may be possible in a single command but I'm not sure how.

Using sed would probably be better:

sed -n '3p;7p' input

If it needs to be a single command, make your own command (function):

get_lines () {
    local input=$1
    shift
    for line; do
        head -n "$line" "$input" | tail -n 1
    done
}

You would call this like:

$ get_lines input 3 7
This is line 3
This is line 7

Where input is the name of your file. This will also accept as many or as few line numbers as you want:

$ get_lines input 1 3 5 7 9
This is line 1
This is line 3
This is line 5
This is line 7
This is line 9
5
  • Yes sed is better I forgot to say that I did the attempt using head and tail and it works but the question is how to do it using only one command
    – Perplexe
    Commented Nov 8, 2019 at 22:34
  • Thank you Jesse
    – Perplexe
    Commented Nov 8, 2019 at 22:36
  • @PerplexeÉmotionnellement: I've added a hack, do you think that would be accepted?
    – jesse_b
    Commented Nov 8, 2019 at 22:41
  • Sorry, can you explain more ?
    – Perplexe
    Commented Nov 8, 2019 at 22:54
  • 1
    @PerplexeÉmotionnellement He's basically written a function that extracts a specific line from a file. The function uses only the head and tail external commands. The rest is pure shell (built-in functionality). You could also obviously do it without using any external command, not even head or tail, by simply reading the lines in the shell while counting them.
    – Kusalananda
    Commented Nov 8, 2019 at 22:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .