Hi There
Hi There

Reputation: 187

Outputting etc/passwd, removing comments and filtering it

I'm trying to finish an assignment where I have to write a command line that displays the output of a cat /etc/passwd command with a bunch of filtering I have tried the following

#!/bin/sh
cat /etc/passwd | grep -v '\#' | sed '1!n;d' | cut -d':' -f1 | rev | sort -r | awk 'NR>= ENVIRON["FT_LINE1"] && NR<= ENVIRON["FT_LINE2"]' | paste -s -d"," - | sed 's/,/, /g' | sed 's/$/./' | tr -d '\n'

but it's not giving any results when I execute it ( I simply do ./name.sh ) Your help would be very appreciated.

Upvotes: 0

Views: 5018

Answers (1)

Nic3500
Nic3500

Reputation: 8601

From the comments we added, looks like you are just missing the environment variables set to some value for your code to work. Modify your script like this:

#!/bin/sh
#
# Modify these values as required
export FT_LINE1=3
export FT_LINE2=8    

cat /etc/passwd | \
    grep -v '\#' | \
    sed '1!n;d' | \
    cut -d':' -f1 | \
    rev | \
    sort -r | \
    awk 'NR>= ENVIRON["FT_LINE1"] && NR<= ENVIRON["FT_LINE2"]' | \
    paste -s -d"," - | \
    sed 's/,/, /g' | \
    sed 's/$/./' | \
    tr -d '\n'

The \ is to continue the command on the next line. I find it easier to read and debug this way.

The values of FT_LINE1 and FT_LINE2 could be read from arguments, or set in the shell environment outside of the script as well.

Upvotes: 1

Related Questions