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:
thanks to anyone that has the time to reply! |
cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | perl -ne 's/ /,/g; print;'
|
is this for perl? i forgot to say im doing this in bash
|
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:
|
Please, can you post an example of the output of
Code:
cat /usr/share/dict/words |grep "^[$list]*$" |
ok
Code:
Enter a letter or done to quit Code:
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 |
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). |
Another simple solution using awk:
Code:
words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | awk 'BEGIN{OFS=","}$1=$1' |
just awk
Code:
Code:
# ./test.sh |
Quote:
|
Quote:
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. |