How do I isolate quoted text from a file to a shell variable?
How do I isolate quoted text from a file to a shell variable?
Also: how do I ignore lines starting with'#'
?
Let's say I have the text file
8723 "full name" "more data here" #5002 "another name" "bla bla bla" 7129 "single" blablabla 3910 noquote "bla bla bla"
and I want to write a script that executes
#!/bin/sh
myscript <KEY> "<FULL NAME>" "<MORE DATA>"
for each line in the text file.
If it makes it easier, you can assume FULLNAME and MOREDATA in the text file are always enclosed in quotes, even when they're composed of a single word.I tried the simple
#!/bin/sh
while read key name extra
do
echo "myscript $key \"$name\" -- \"$extra\""
#myscript $key "$name" "$extra"
done < list.txt
but it doesn't group the value inside quotes in a single shell variable.
The output ismyscript 8723 ""full" -- "name" "more data here"" myscript #5002 ""another" -- "name" "bla bla bla"" myscript 7129 ""single"" -- "blablabla" myscript 3910 "noquote" -- ""bla bla bla""
Instead, I'd 开发者_开发问答like to obtain
myscript 8723 "full name" -- "more data here" myscript 7129 "single" -- "blablabla" myscript 3910 "noquote" -- "bla bla bla"
Of course I can add myscript
to every line of the original text file and make that file a script or write a C
program to do that ...
#!/bin/sh
while read key name extra
do
eval myscript $key $name $extra
done <(grep -v "^#" list.txt)
sed -e '/^[^#]/s/^/myscript /' list.txt
EDIT: To run it:
sed -e '/^[^#]/s/^/myscript /' list.txt | bash
精彩评论