LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   have a Variable with multiple lines of data need to have it all on one line seperated (https://www.linuxquestions.org/questions/programming-9/have-a-variable-with-multiple-lines-of-data-need-to-have-it-all-on-one-line-seperated-725027/)

xskycamefalling 05-10-2009 02:06 PM

have a Variable with multiple lines of data need to have it all on one line seperated
 
ok so i have a bunch of letters input by a user that i match with a dictionary and store in a variable $words

Code:


words=`cat /usr/share/dict/words |grep "^[$list]*$"`

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

thanks to anyone that has the time to reply!

kellinwood 05-10-2009 03:30 PM

cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | perl -ne 's/ /,/g; print;'

xskycamefalling 05-10-2009 03:46 PM

is this for perl? i forgot to say im doing this in bash

xskycamefalling 05-10-2009 03:50 PM

ok i entered the above but ended up getting this as output

,a, ,a,d, ,a,d,d, ,b, ,b,a,a, ,b,a,d,
,,c, ,c,a,b, ,c,a,d, ,d, ,d,a,b, ,d,a,d,

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 | perl -ne 's//,/g; print;'`

echo "$words"


colucix 05-10-2009 05:05 PM

Please, can you post an example of the output of
Code:

cat /usr/share/dict/words |grep "^[$list]*$"
? The solution posted by kellinwood should work if the output consists only of blank separated words over multiple lines.

xskycamefalling 05-10-2009 06:48 PM

ok

Code:

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

xskycamefalling 05-10-2009 07:10 PM

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,/'`


kellinwood 05-10-2009 09:31 PM

Here's a shorter version that uses global replace in sed....

words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | sed 's/ \+/,/g'

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).

colucix 05-10-2009 11:12 PM

Another simple solution using awk:
Code:

words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | awk 'BEGIN{OFS=","}$1=$1'

ghostdog74 05-11-2009 12:08 AM

just awk
Code:


awk 'BEGIN{ 
 s=""
 while(1){ 
  printf "Enter letter,(q|Q)uit: "
  getline choice < "-"
  if (choice ~ /^[Qq]$/) {break}
  else{ s=s choice } 
 }
  s="^["s"]*$" 
}
$0 ~ s{
  i++
  if(i==6){printf "%s\n",$0; i=0 }
  else{ printf "%s,",$0 }
}' /usr/share/dict/words

output:
Code:

# ./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,


xskycamefalling 05-11-2009 01:10 PM

Quote:

Originally Posted by kellinwood (Post 3536551)
Here's a shorter version that uses global replace in sed....

words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | sed 's/ \+/,/g'

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..

colucix 05-12-2009 04:44 AM

Quote:

Originally Posted by xskycamefalling (Post 3537296)
this works well allthough it dosent add the comma to the last word on any of the lines!
any more feedback would be helpufll thanks


words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | awk 'BEGIN{OFS=","}$1=$1'

If you want to add a comma at the end of each line, just do
Code:

words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | awk 'BEGIN{OFS=","}$1=$1{print $0 OFS}'


All times are GMT -5. The time now is 05:19 PM.