Reputation: 5435
I getting space delimited list of titles based on this code:
TITLES=`awk -F'|' '{print $1}' titles.txt | cut -d'=' -f2 | sort
Then I use that list in a for loop to print out the results:
for T in $TITLES
do
echo "$T"
done
The problem is that when a title has more than one word and the words are separated by a space then my for loop prints them out as separate things.
I tried adding double qoutes using sed to each title but that just caused the loop to print each word in double quotes rather then the two word titles together.
How can I fix this?
Upvotes: 1
Views: 333
Reputation: 342819
you could use the shell without external tools
while IFS="|" read -r a b
do
IFS="="
set -- $a
T=$2
echo "T is $T"
unset IFS
done <"file"
Upvotes: 1
Reputation: 838896
Have you tried putting quotes around $TITLES?
TITLES=`awk -F'|' '{print $1}' titles.txt | cut -d'=' -f2 | sort`
for T in "$TITLES"
do
echo "$T"
done
Or you could use while:
awk -F'|' '{print $1}' titles.txt | cut -d'=' -f2 | sort | while read T
do
echo "$T"
done
Upvotes: 2
Reputation: 143304
One way to do this is to set IFS:
IFS='
'
for T in $(awk -F'|' '{print $1}' titles.txt | cut -d'=' -f2 | sort)
do
echo "$T"
done
This sets IFS to just newline (there's a newline between the open and close single-quotes).
I know this works in bash, but I'm not sure how portable it is to other shells.
If you do this you'll almost certainly want to set IFS back to its old value once you're done the loop.
Upvotes: 1