N. Hof
N. Hof

Reputation: 31

Why doesn't "sed" pick up on command?

`When using sed and trying to pull out a specific line, it looses the p portion of "sed -n (x)p test.txt"

I'm trying to look at a line and see if its A or B.

sed -n 3p test.txt

works fine, but i'm trying to do:

sed -n $(Count) test.txt

This doesn't work

sed -n $($Count)p test.txt

Doesn't work

Count=$(cat -n test.txt | grep -o [0-9]* | tail -1)

until [ $Count = 0 ]; do
    if [[ $(sed -n $(Count)p test.txt) = Him ]] || [[ $(sed -n $(Count)p model.txt) = He ]]
        then
        echo "This is a Boy Word"
    elif [[ $(sed -n $(Count)p model.txt) = Her ]] || [[ $(sed -n $(Count)p model.txt) = She ]]
        then
        echo "This is an Girl Word"
    fi
    let Count=Count-1
    sleep 1
done

I'm expecting : This is a Boy Word

This is a Boy Word

This is a Girl Word

This is a Girl Word... Until it has gone through all the lines,

However I'm getting (with sed -n $($Count)p test.txt)
Line 17: 3: command not found
Line 20: 3: command not found
Line 17: 2: command not found
Line 17: 2: command not found


Or (with sed -n $(Count)p test.txt
Line 17: Count: command not found
Line 20: Count: Command not found
Line 17: Count: Command not Found
Line 20: Count: command not found

Upvotes: 1

Views: 78

Answers (1)

chepner
chepner

Reputation: 531868

You need to use the full form ${Count} to separate the variable name from an adjacent character.

sed -n ${Count}p test.txt

Or, simply quote the parameter expansion:

sed -n "$Count"p test.txt

Upvotes: 1

Related Questions