LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 10-30-2012, 02:31 PM   #1
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Rep: Reputation: 1
Trying to grab a record when column meets condition.


Code:
$1=file
case
var=(a;; b;; c;; d;;)
esac
for i in $file
grep ^1 $i | awk '{ if ($4==$var) {print $0} }' 
done
In the above code I am using a case statement to set up my values for $var. The for loop is grabbing all records that begin with 1 and piping it through a condition; if column 4 equals $var(a||b||c||d) print that record.

As the seasoned Admins know it is not doing what I want it to. Instead once the condition is met all the record are being printed out.

Any help or direction would be greatly appreciated. I look forward to your input.

s
 
Old 10-30-2012, 03:17 PM   #2
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
First of all, just to be certain, we are talking about shell scripting here?

Code:
$1=file
This is backwards. You can't set a numbered parameter inside the script, and you don't prefix a variable with $ when setting it. I believe you want file=$1 instead.

Code:
case
var=(a;; b;; c;; d;;)
esac
This is not a correct case syntax. A case statement is a kind of testing construct. It needs an input value to test, a list of conditions to test it against, and a set of operations to perform.

Code:
case $1 in
	a) echo "this is value a" ;;
	b) echo "this is value b" ;;
	c) echo "this is value c" ;;
esac
As it stands the part inside the case/esac would actually be treated as an array setting step instead, if the rest of it didn't error out. Could you please explain in detail what you are really trying to do here?


Code:
for i in $file
grep ^1 $i | awk '{ if ($4==$var) {print $0} }' 
done
The correct syntax for a for loop is:

Code:
for var in <list>; do
	commands using "$var"
done
Note that you forgot the "do" keyword.

Also, QUOTE ALL OF YOUR VARIABLE SUBSTITUTIONS. You should never leave the quotes off a parameter expansion unless you explicitly want the resulting string to be word-split by the shell (globbing patterns are also expanded). 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

awk cannot generally use shell variables directly. You have to import them into awk variables using the -v option.

Finally, there's the Useless Use Of Grep, but that's a minor problem compared to the rest.

Here are a few useful bash scripting references:
http://mywiki.wooledge.org/BashGuide
http://wiki.bash-hackers.org/start
http://www.linuxcommand.org/index.php
http://wiki.bash-hackers.org/scripting/newbie_traps
http://mywiki.wooledge.org/BashPitfalls
http://mywiki.wooledge.org/BashFAQ
http://tldp.org/LDP/Bash-Beginners-G...tml/index.html
http://www.tldp.org/LDP/abs/html/index.html
http://www.gnu.org/software/bash/manual/bashref.html
http://ss64.com/bash/

I suggest reading through the first one carefully, at least.

Here are a few useful awk references:
http://www.grymoire.com/Unix/Awk.html
http://www.gnu.org/software/gawk/man...ode/index.html
http://www.pement.org/awk/awk1line.txt
http://www.catonmat.net/blog/awk-one...ined-part-one/
 
Old 10-30-2012, 04:08 PM   #3
smturner1
Member
 
Registered: Oct 2009
Location: MI
Distribution: Arch 2.6.35
Posts: 107

Original Poster
Rep: Reputation: 1
Here is where we are after your edits.

Code:
file=$1
I need to grab all records that begin with 1 | I need column 4 to equal one of the values in the case statement, if it does print only that record >> append to a file within the current dir.
Code:
case var in 
            a) echo "value1"
            b) echo "value2"
            c) echo "value3"
            d) echo "value4"
esac

for i in $file
grep ^1 $i | awk '{ if ($4=="$var") {print $0} }' >> a_file

done
What happens is that when I run this on the CLI it returns every record, instead of only those records that meet the criteria within the case statement.

Hopefully that clears somethings up.
 
Old 11-03-2012, 10:54 AM   #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
You appear to be quite confused about how awk and the various shell features actually work.

Let's start over from the beginning. Forget about what you've written so far and simply explain exactly what you are trying to do, in reasonable detail, along with a representative example of the input text. Then explain exactly what you want to extract from it, according to what criteria and in what format, and where the matching values are to come from, if any.

I'm pretty sure that your requirements can be met with just a simple awk command, perhaps with a small shell wrapper if you need to match multiple values. But it all comes down to the details.
 
  


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] add - (minus) in front of a column with value condition of another column cazanadrian Linux - Newbie 1 10-30-2012 01:51 PM
How to display 2 different column field values as one column value in mysql VijayaRaghavanLakshman Linux - General 2 04-16-2012 09:56 AM
compare second column of a file then print the first column of it in a ne fil if true java_girl Linux - Newbie 2 03-16-2012 04:50 AM
How to delete/grab a line which matchs a pattern of a particular column only ? mauludi Linux - Newbie 6 01-18-2010 05:52 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:22 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