LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-15-2012, 03:26 PM   #1
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
Using cut or awk to strip a field from a variable.. HELP


Hi All,

I am trying to write a script that will execute a mysql query based upon user input. I am running into issues with the following:

VERIFYQUERY=`mysql -u$DBUSER -p$DBPW -D$DB -e"select user_name from user where user_id='$REMOVEID'";`

VERIFYUSER=`echo $VERIFYQUERY | cut -f2`

The $VERIFYQUERY variable is setting properly and the output is something like "01 username".

All I need to do is cut the last field out to strip away the userid portion of the output but I am doing something wrong.

Any input is appreciated, I don't do much BASH scripting and am sure that this is an easy fix for someone with a bit more experience.

Thank You!
 
Old 03-15-2012, 03:35 PM   #2
Kustom42
Senior Member
 
Registered: Mar 2012
Distribution: Red Hat
Posts: 1,604

Original Poster
Rep: Reputation: 415Reputation: 415Reputation: 415Reputation: 415Reputation: 415
I had a typo in my script and have fixed it. I thought I was going crazy for a minute there..
 
Old 03-15-2012, 03:47 PM   #3
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
Please use [code][/code] tags around your code and data, to preserve formatting and to improve readability. Please do not use quote tags, colors, or other fancy formatting.


No need to use an external tool for this. Unless your shell is very primitive, there are parameter substitution patterns that can do the job nicely.

Code:
VERIFYUSER=${VERIFYQUERY#* }

More bash string manipulations.

In addition:

1)
$(..) is highly recommended over `..`

2)
Environment variables are generally all upper-case. So while not absolutely necessary, it's good practice to keep your own user variables in lower-case or mixed-case, to help differentiate them.

3)
QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a variable expansion unless you explicitly want the resulting string to be word-split by the shell. This is a vitally important concept in scripting, so train yourself to do it correctly now. You can learn about the exceptions later.

http://mywiki.wooledge.org/Arguments
http://mywiki.wooledge.org/WordSplitting
http://mywiki.wooledge.org/Quotes
 
  


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] how to cut the last field without using awk anandg111 Linux - Newbie 6 03-13-2012 02:29 PM
internal awk variable Field seperator casperdaghost Linux - Newbie 5 10-23-2011 11:12 AM
problem while comparing awk field variable with input variable entered using keyboard vinay007 Programming 12 08-23-2011 12:44 AM
[SOLVED] awk: how to print a field when field position is unknown? elfoozo Programming 12 08-18-2010 03:52 AM
How to use command grep,cut,awk to cut a data from a file? hocheetiong Linux - Newbie 7 09-11-2008 07:16 PM

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

All times are GMT -5. The time now is 09:52 AM.

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