1

I have a script, that display "n" lines and display form "c" line.

#!/bin/bash
hn=5
tn=1
while getopts ":n:c:" opt
do
    case $opt in
        h) echo Pomocy
            exit 1
            ;;
        n) hn=$OPTARG ;;
        c) tn=$OPTARG ;;
        \?) echo Nieznana opcja $OPTARG;;
        :) echo Brakuje argumentu opcji $OPTARG ;;
    esac
done

shift $(($OPTIND-1))
for i do
    if [[ -f $1 ]];
    then

        cat $i | head -n $hn
    else
        echo "plik nie istnieje"
    fi
done

exit 0

how to make it display from c line? display "n" lines iw work. what command to do "c" line? it displays from 1 line by default

Thanks for help. This command works good

cat $i |head -n $hn | tail -n +$tn
Simon C.
  • 1,058
  • 15
  • 33
skuska
  • 53
  • 6
  • 2
    Duplicate on ServerFault : https://serverfault.com/questions/133692/how-to-display-certain-lines-from-a-text-file-in-linux – Aaron Jan 25 '19 at 10:29
  • Possible duplicate of [Extract range of lines using sed](https://stackoverflow.com/questions/51449715/extract-range-of-lines-using-sed) – KamilCuk Jan 25 '19 at 10:54
  • Thanks for link, command cat $i | head -n $hn | tail -n $tn work, but not for "c" line, only to "c" line. – skuska Jan 25 '19 at 10:54

1 Answers1

1

A simple way with tail, wc and awk:

a=`wc -l $file | awk '{print $1}'`
tail -n $(( a - tn)) $hn

And if I put that in your original file:

#!/bin/bash
hn=5
tn=1
while getopts ":n:c:" opt
do
    case $opt in
        h) echo Pomocy
            exit 1
            ;;
        n) hn=$OPTARG ;;
        c) tn=$OPTARG ;;
        \?) echo Nieznana opcja $OPTARG;;
        :) echo Brakuje argumentu opcji $OPTARG ;;
    esac
done

shift $(($OPTIND-1))
for i do
    if [[ -f $1 ]];
    then
        a=`wc -l $file | awk '{print $1}'`
        tail -n $(( a - tn)) $hn
    else
        echo "plik nie istnieje"
    fi
done
exit 0
Simon C.
  • 1,058
  • 15
  • 33
  • Command cat $i | head -n $hn | tail -n $tn work, but only to "c" line. Can you help me paste this command into my code? – skuska Jan 25 '19 at 10:57
  • I update with hn and tn and put my code within your script, let me know if t works – Simon C. Jan 25 '19 at 11:46