Choose how many lines you want to show in the output

Asked

Viewed 73 times

0

I am trying to put a flag -n to be able to show in the output the number of lines desired by the user, having so far only been able to show the line number beside

case "$1" in                ## definir o pósprocessador
  -r)  pp="shuf" ;;         # -r    shuffle
  -s)  pp="sort" ;;         # -s    sort
   *)  pp="cat -n"  ;;      # default numera linhas
esac
  • Explain better, I couldn’t understand your purpose.

  • I’m trying to put a flag that shows the number of lines intended by the user, for example. /bash.sh -n 5 and with this output will only appear 5 lines or 5 items

  • if you want to show the first 5 use the command head -n5 if it is the last 5 use the tail -n5. Would that be?

1 answer

0

Based on your reasoning, it follows script modified:

#!/bin/bash

case "$1" in
    -r) shuf "$2" ;;
    -s) sort "$2" ;;
    *) cat -n "$1" ;;
esac

Assuming you have an input file called frutas.txt with the following content:

Banana
Laranja
Melancia
Limão
Maracujá
Maçã
Uva
Goiaba
Tangerina
Pêssego

1) Shuffling the lines of the input file:

$ ./processador.sh -r frutas.txt
Melancia
Maçã
Pêssego
Maracujá
Goiaba
Banana
Tangerina
Uva
Laranja
Limão

2) Sorting the lines of the input file:

$ ./processador.sh -s frutas.txt
Banana
Goiaba
Laranja
Limão
Maçã
Maracujá
Melancia
Pêssego
Tangerina
Uva

3) Displaying the numbered lines contained in the input file:

$ ./processador.sh frutas.txt
     1  Banana
     2  Laranja
     3  Melancia
     4  Limão
     5  Maracujá
     6  Maçã
     7  Uva
     8  Goiaba
     9  Tangerina
    10  Pêssego

Browser other questions tagged

You are not signed in. Login or sign up in order to post.