LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Trying to grab a record when column meets condition. (https://www.linuxquestions.org/questions/linux-newbie-8/trying-to-grab-a-record-when-column-meets-condition-4175434795/)

smturner1 10-30-2012 02:31 PM

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

David the H. 10-30-2012 03:17 PM

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/

smturner1 10-30-2012 04:08 PM

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.

David the H. 11-03-2012 10:54 AM

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.


All times are GMT -5. The time now is 03:49 AM.