LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 02-20-2011, 01:38 PM   #1
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Rep: Reputation: 0
Remove Characters From Within Variable


I am doing a mysql query with a bash shell script like:


mysql translator -u root --password=******** -e "SELECT word FROM tagalog ORDER BY RAND() LIMIT 1" | while read line; do
echo $line

so when i echo the value of $line i get:
word
magandang umaga

"word" is the name of the row in the table and maganda umaga is a randomly selected choice from the row


is there a way i can remove the name of the row from the variable $line

with a result that will allow me to echo $line and output only the randomly selected entry in from the row e.g.
magandang umaga

Last edited by dnoob; 02-20-2011 at 01:49 PM.
 
Old 02-20-2011, 02:36 PM   #2
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
You can try using the cut command man.

http://linux.die.net/man/1/cut

That is the best explanation; the man page. Hope it helps, good luck.
 
Old 02-20-2011, 02:49 PM   #3
sys64738
Member
 
Registered: May 2008
Location: NRW/Germany
Posts: 105

Rep: Reputation: 30
Hi,
how about:
Code:
tmpname=$!
if [ -e $tmpname ] then
  rm $tmpname
fi
mysql translator -u root --password=******** -e "SELECT word FROM tagalog ORDER BY RAND() LIMIT 1" | while read line; do echo $line >> $tmpname
tail -n 1 $tmpname
In other words just the last line will be output.

"$!" is the pid of the running process.
Should work. Post again if not .
 
Old 02-20-2011, 03:05 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
This will remove everything up to and including the first newline contained in the variable:
Code:
$ line='word
magandang umaga'

$ echo "$line"
word
magandang umaga

$ shopt +s extquote
$ echo "${line#$'*\n'}"
magandang umaga
The extquote shell option must be enabled in order to use the $'string' quoting pattern inside parameter substitution. It should be enabled by default.


Edit: By the way, be careful about falling prey to bash pitfall #8 in your code above.

Last edited by David the H.; 02-20-2011 at 03:08 PM. Reason: As stated
 
Old 02-20-2011, 03:22 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Actually, looking at it again more carefully, read works on a per-line basis (at least by default), so your problem isn't that the variable holds two lines, it's that the loop is running twice, echoing each line separately. So you'll have to devise some test or filter, or otherwise rewrite your script to avoid the unwanted echo.
 
1 members found this post helpful.
Old 02-20-2011, 03:27 PM   #6
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
Originally Posted by David the H. View Post
Actually, looking at it again more carefully, read works on a per-line basis (at least by default), so your problem isn't that the variable holds two lines, it's that the loop is running twice, echoing each line separately. So you'll have to devise some test or filter, or otherwise rewrite your script to avoid the unwanted echo.
That's why I suggested using the cut command, to try to cut out the characters or columns needed.
 
Old 02-20-2011, 03:39 PM   #7
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Arch + Xfce
Posts: 6,852

Rep: Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037Reputation: 2037
Yes, and there are certainly many other options available too, such as grep -v, tail, or even sed or awk. The main thing is that either the line has to be eliminated somehow before the output gets passed to the loop, or some kind of test has to be done to the variable inside the loop to catch it.
 
Old 02-20-2011, 03:49 PM   #8
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Or possibly using tr to remove the new line/carriage returns:

Code:
tr -d '\n'  -  to delete the new line
tr '\n' ' '  -  to replace the new line with a space
 
Old 02-20-2011, 03:53 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Have you contemplated using the -N option to mysql?


Cheers,
Tink
 
1 members found this post helpful.
Old 02-20-2011, 06:33 PM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
You beat me Tink
 
Old 02-20-2011, 10:21 PM   #11
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Original Poster
Rep: Reputation: 0
tinker "Have you contemplated using the -N option to mysql?"

does anyone have example syntax of the -N option tinker and grail are referring too?
 
Old 02-20-2011, 10:46 PM   #12
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Quote:
--skip-column-names, -N
Do not write column names in results.
For example: mysql -N -e 'select * from user' mysql

---------- Post added 02-21-11 at 05:46 AM ----------

Oops, forgot the manpage for you - http://linux.die.net/man/1/mysql
 
Old 02-21-2011, 01:07 AM   #13
dnoob
LQ Newbie
 
Registered: Feb 2011
Posts: 17

Original Poster
Rep: Reputation: 0
thanks tinkster,grail, and corp 769. it is working !


mysql translator -u root --password=********* -N -e "SELECT word FROM tagalog ORDER BY RAND() LIMIT 1" | while read line; do

echo "$line"

now when i echo $line it no longer includes the name of the column

Last edited by dnoob; 02-21-2011 at 01:09 AM.
 
Old 02-21-2011, 01:12 AM   #14
corp769
LQ Guru
 
Registered: Apr 2005
Location: /dev/null
Posts: 5,818

Rep: Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007Reputation: 1007
Awesome, glad to hear you got it working!
 
  


Reply



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] Script to pull certain characters from a filename and use in a variable? kmkocot Linux - Newbie 5 10-29-2009 09:10 PM
BASH: Extract First Seven Characters To Variable Grizzlyman Programming 4 10-19-2009 12:41 AM
New line characters from command output in variable. Coolmax Linux - Newbie 1 07-26-2009 09:56 AM
removing characters from variable? hateseven Programming 6 02-24-2009 09:34 PM
bash replacing characters within a variable baks Programming 5 03-19-2007 12:33 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

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

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration