have a Variable with multiple lines of data need to have it all on one line seperated
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
i need to know how i can display the results as a set of comma separated values (ie word1, word2, word3, word4, ...) 6 words per line, then wrap and start another line. Otherwise, display the resulting matched word list to the screen one word per line
#!/bin/sh
end="done"
while [ "$letters" != "$end" ]
do
echo "Enter a letter or done to quit"
read "letters"
if [ "$letters" != "$end" ]
then
list="${list}${letters}"
fi
done
echo "the letters you gave were:"
echo "$list"
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | perl -ne 's//,/g; print;'`
echo "$words"
Enter a letter or done to quit
a
Enter a letter or done to quit
b
Enter a letter or done to quit
c
Enter a letter or done to quit
d
Enter a letter or done to quit
done
the letters you gave were:
abcd
a
ad
add
b
baa
bad
c
cab
cad
d
dab
dad
thats what im getting with that line ive actually changed what he had around a little bit and used sed but i can only get a comma after the first word any ideas on this would be helpfull
Code:
#!/bin/sh
end="done"
while [ "$letters" != "$end" ]
do
echo "Enter a letter or done to quit"
read "letters"
if [ "$letters" != "$end" ]
then
list="${list}${letters}"
fi
done
echo "the letters you gave were:"
echo "$list"
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed 's/^\([a-zA-Z]*\)/\1,/'`
echo "$words"
the output i get for that one looks like this
Enter a letter or done to quit
a
Enter a letter or done to quit
b
Enter a letter or done to quit
c
Enter a letter or done to quit
d
Enter a letter or done to quit
done
the letters you gave were:
abcd
a, ad add b baa bad
c, cab cad d dab dad
this is along the write lines of what i need just a comma after all of the words
ok so i kinda figured it out using sed
the problem im having now with what ive wrote is that if there are less than 6 words on the last line they dont have "," after them any help would be appreciated
the code i wrote is as follows
Code:
#!/bin/sh
end="done"
while [ "$letters" != "$end" ]
do
echo "Enter a letter or done to quit"
read "letters"
if [ "$letters" != "$end" ]
then
list="${list}${letters}"
fi
done
echo "the letters you gave were:"
echo "$list"
words=`cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | sed 's/^\([a-zA-Z]*\) \([a-zA-Z]*\) \([a-zA-Z]*\) \([a-zA-Z]*\) \([a-zA-Z]*\) \([a-zA-Z]*\)/\1,\2,\3,\4,\5,\6,/'`
Note that one or more spaces matched by ' \+' is replaced with a comma, and this is performed globally on the input string (trailing g is global replace).
# ./test.sh
Enter letter,(q|Q)uit: a
Enter letter,(q|Q)uit: b
Enter letter,(q|Q)uit: c
Enter letter,(q|Q)uit: d
Enter letter,(q|Q)uit: q
abaca,acc,baa,baba,bad,bd
cc,dab,dad,
Note that one or more spaces matched by ' \+' is replaced with a comma, and this is performed globally on the input string (trailing g is global replace).
this dosent work!! at least i couldent get it to work.. it shows up with no commas..
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.