0

I'm having trouble passing the variable $APN to sed, tried lots of combinations of ' " \ \

First column of .csv files is a number, this code works:

cat *.csv > "$APN".csv              #combine

sort -u "$APN".csv -o "$APN".csv    #sort, remove duplicate APNs

sed -i '/^715/!d' combined2.csv     #remove not 715

This code third line does nothing:

cat *.csv > "$APN".csv             #combine

sort -u "$APN".csv -o "$APN".csv    #sort, remove duplicate APNs

sed -i '/^$APN/!d'                  #remove not $APN
DSL
  • 15
  • 5

1 Answers1

1

The variables inside single quotes are not expanded, you need double quotes.

sed -i "/^$APN/!d" "${APN}.csv"

Learn how to quote properly in shell, it's very important :

"Double quote" every literal that contains spaces/metacharacters and every expansion: "$var", "$(command "$var")", "${array[@]}", "a & b". Use 'single quotes' for code or literal $'s: 'Costs $5 US', ssh host 'echo "$HOSTNAME"'. See
http://mywiki.wooledge.org/Quotes
http://mywiki.wooledge.org/Arguments
http://wiki.bash-hackers.org/syntax/words

Gilles Quénot
  • 173,512
  • 41
  • 224
  • 223