LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 05-10-2009, 03:06 PM   #1
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Rep: Reputation: 15
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!
 
Old 05-10-2009, 04:30 PM   #2
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
cat /usr/share/dict/words |grep "^[$list]*$" | xargs -n 6 echo | perl -ne 's/ /,/g; print;'
 
Old 05-10-2009, 04:46 PM   #3
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
is this for perl? i forgot to say im doing this in bash
 
Old 05-10-2009, 04:50 PM   #4
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
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"
 
Old 05-10-2009, 06:05 PM   #5
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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.
 
Old 05-10-2009, 07:48 PM   #6
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
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
 
Old 05-10-2009, 08:10 PM   #7
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
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,/'`
 
Old 05-10-2009, 10:31 PM   #8
kellinwood
Member
 
Registered: Jan 2006
Location: Culver City, California
Distribution: Fedora
Posts: 64

Rep: Reputation: 21
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).
 
Old 05-11-2009, 12:12 AM   #9
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Another simple solution using awk:
Code:
words=`cat /usr/share/dict/words | grep "^[$list]*$" | xargs -n 6 echo | awk 'BEGIN{OFS=","}$1=$1'
 
Old 05-11-2009, 01:08 AM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
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,
 
Old 05-11-2009, 02:10 PM   #11
xskycamefalling
Member
 
Registered: May 2009
Posts: 43

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by kellinwood View Post
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..
 
Old 05-12-2009, 05:44 AM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
Quote:
Originally Posted by xskycamefalling View Post
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}'
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] edit multiple lines of a text file into 1 line: schneidz Programming 2 04-09-2009 12:22 PM
data type help needed and multiple inputs in single line shank.nikhil Programming 3 04-04-2009 09:04 PM
firefox does not include <param name=...> beyond the first line if multiple lines reiki33 Linux - Desktop 0 04-17-2008 07:27 PM
sed/awk/grep for multiple line data hotrodmacman Programming 8 10-18-2007 12:06 PM
merge multiple lines of a single file into one line groverrajiv Linux - Newbie 4 05-26-2004 03:38 AM


All times are GMT -5. The time now is 11:38 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration