Providing variable contents to awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Providing variable contents to awk
# 1  
Old 03-06-2012
Providing variable contents to awk

Hello, All. This is my first post here, and I expect that the answer is simple, but I can't find it. Might be the way I'm searching. I'm fairly new to Unix/Linux, and I'm writing a Korn Shell Script. I am trying to provide a value that is already in a variable to awk so that awk can pull out the second column.

An example of what I'm trying is here:

Code:
 
for g in `cat $outfilepath/$outfile`
do
 
$g | awk -F, '{print $2}'
 
done

The line should be comma-separated, and I'm trying to pull out the second entry, but all this does is print out the contents of $g, which doesn't do me any good.

Once I can pull out the second entry, I plan on using it for some decision making, but that's secondary right now.

Any help would be appreciated.
# 2  
Old 03-06-2012
That is a useless use of cat, to avoid unwanted side-effects and splitting for things in backticks you would generally do

Code:
while read LINE
do
        echo "Read $LINE"
done < inputfile

...but in this case I'm not sure why you're using a while loop at all. awk is quite capable of reading more than one line by itself.

Anyway.

When you do "$g" by itself, it assumes that's a command. For instance:

Code:
$ G="cat"
$G file

file contents.
...
...

$

So you're not actually printing the line into awk here -- you're running it as a command, and piping its output into awk.

What you're trying to do is more like echo $G | awk ...

...but I think your program would be best rewritten as:

Code:
awk -F, '{ print $2 }' filename

This User Gave Thanks to Corona688 For This Post:
# 3  
Old 03-06-2012
awk is its own entire programming language, incidentally. If all you wanted to do was to extract the second column for further processing, that further processing could probably be done inside awk too.

Or if you wanted to keep it in shell, you can do this wholly in shell without using awk at all:

Code:
while IFS="," read COL1 COL2 COL3
do
        echo "column 2 is ${COL2}"
done < inputfile

IFS is a special variable controlling what the shell does variable splitting on. Its value is only temporarily changed here, while read is being run.
# 4  
Old 03-06-2012
Thanks, Corona. As I said, I'm relatively new to Unix, and I'm still learning what can and can't be done, and the best way to do things. I'm still reading up on awk, and learning how to use it, but as a programming language in itself, it's a lot to take in.

I'm trying your solution now, and I'll reply the status when it's done. The script takes a bit of time to run.

---------- Post updated at 02:30 PM ---------- Previous update was at 12:57 PM ----------

Thanks again, Corona. I reworked my script using just awk, and removing the while loop, and it worked as I'd hoped.

I appreciate the help.
This User Gave Thanks to compguy74 For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sorting the contents of a variable

i want to reduce the OPTIONS variable which is a list of email addresses to just the unique entries. The following works, but, i would like to accomplish it without using the temporary file (dest.txt) $echo $OPTIONS a b c A B D ... (5 Replies)
Discussion started by: jgt
5 Replies

2. UNIX for Beginners Questions & Answers

Storing file contents to a variable

Hi All, I was trying a shell script. I was unable to store file contents to a variable in the script. I have tried the below but unable to do it. Input = `cat /path/op.diary` Input = $(<op.diary) I am using ksh shell. I want to store the 'op.diary' file contents to the variable 'Input'... (12 Replies)
Discussion started by: am24
12 Replies

3. Shell Programming and Scripting

Folder contents getting appended as strings while redirecting file contents to a variable

Hi one of the output of the command is as below # sed -n "/CCM-ResourceHealthCheck:/,/---------/{/CCM-ResourceHealthCheck:/d;/---------/d;p;}" Automation.OutputZ$zoneCounter | sed 's/$/<br>/' Resource List : <br> *************************** 1. row ***************************<br> ... (2 Replies)
Discussion started by: vivek d r
2 Replies

4. Shell Programming and Scripting

awk: compose regex in variable and then use its contents like $0 ~ var

A question to the awk pundits: I was thinking about composing a regex in a variable and then use its contents like $0 ~ var instead of $0 ~ /r/. Sort of indirection. Did someone run into this? Is it possible at all? (3 Replies)
Discussion started by: RudiC
3 Replies

5. Homework & Coursework Questions

How to read contents of a file into variable :(

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted! 1. The problem statement, all variables and given/known data: I have to read the contents of each field of a file creating user accounts. The file will be of format : ... (6 Replies)
Discussion started by: dude_me5
6 Replies

6. Shell Programming and Scripting

how to grep the contents of a variable..

Hello All, I've written a script to collect all audit logs files; now i want to see only the files from it which contains certain x tablenames. I've stored all the tablenames in a log file and using it through variable in a script (ex:below) $ more tablenames.log (this is just a samle... (2 Replies)
Discussion started by: suri.tyson
2 Replies

7. Shell Programming and Scripting

Storing the contents of a file in a variable

There is a file named file.txt whose contents are: +-----------------------------------+-----------+ | Variable_name | Value | +-----------------------------------+-----------+ | Aborted_clients | 0 | | Aborted_connects | 25683... (6 Replies)
Discussion started by: proactiveaditya
6 Replies

8. Shell Programming and Scripting

unique sort contents of a variable

Hi , I have #echo $var1 #hdisk2 hdisk3 hdisk0 hdisk2 Now I need to remove duplicate entries from this . ie. after sorting it should only have hdisk2 hdisk3 hdisk0 . I can have these values in a array as well . I understand we can use sort -u to remove the duplicates in a... (2 Replies)
Discussion started by: praveenbvarrier
2 Replies

9. UNIX for Dummies Questions & Answers

Save contents of a Variable

I want to save the contents of a variable to a file. How can that be achieved? I have tried with: echo $varname > textfile.txt but for some reason it does not print anything. (1 Reply)
Discussion started by: carl_vieyra
1 Replies

10. Shell Programming and Scripting

adding contents of a variable to a command

hi all, i'm new to shell scripting, so i'm not sure how to work this. Is it possible to read in the contents of a variable and add it to a command? for example: ------------------------ #!/bin/sh set example = -dfr rm ${example} ------------------------ when i run the script, i want... (2 Replies)
Discussion started by: gammarays
2 Replies
Login or Register to Ask a Question