Choose how many rows to display in the output

0

I'm trying to put a flag -n to be able to show in the output the number of lines wanted by the user, having until now only managed to show the line number next to it

case "$1" in                ## definir o pósprocessador
  -r)  pp="shuf" ;;         # -r    shuffle
  -s)  pp="sort" ;;         # -s    sort
   *)  pp="cat -n"  ;;      # default numera linhas
esac
    
asked by anonymous 08.01.2018 / 22:27

1 answer

0

Based on your reasoning, it follows modified script :

#!/bin/bash

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

Assuming you have an input file named 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 lines from the input file:

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

3) Displaying the enumerated rows 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
    
16.01.2018 / 13:59