Spaced input causing awk error


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaced input causing awk error
# 1  
Old 12-16-2011
Spaced input causing awk error

Hi all, Just want to say thanks for the great forum you have here, the old topics and posts have helped tremendously. So much so that I have managed to figure a lot out just by researching. However, I'm having a small issue that I simply can't find the answer to.

Last edited by whyte_rhyno; 12-30-2011 at 05:15 PM.. Reason: Forgot to mention what shell I'm using
# 2  
Old 12-16-2011
Code:
[root@hostname ~]# grep "root" /etc/passwd
root:x:0:0:root:/root:/bin/bash
[root@hostname ~]# username=root
[root@hostname ~]# awk -v val=$username -F: '$1==val{print $3}' /etc/passwd
0

Works for me.
# 3  
Old 12-16-2011
You should quote the $username variable:

Code:
user_id=`awk -v val="$username" -F: '$1==val{print $3}' /etc/passwd`

This User Gave Thanks to pflynn For This Post:
# 4  
Old 12-16-2011
it would have something to do with that but used single quotes instead of double. I'm such an idiot.

Last edited by whyte_rhyno; 12-30-2011 at 05:15 PM..
# 5  
Old 12-16-2011
Yeap...the problem with using single quotes is that the shell will not interpolate the variables inside it.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

User input and run awk using the input

I am trying to allow a user to enter in text and then store that text in a variable $gene to run in an awk command in which those values are used to run some calculations. I am getting syntax errors however, when I try. Thank you :). The awk runs great if it is a pre-defined file that is used,... (7 Replies)
Discussion started by: cmccabe
7 Replies

2. Shell Programming and Scripting

Percentage sign causing awk problems

looks like awk gets confused when there's a % next to a number. command im running: awk -F" " '/phxnaz001b/ && /vol/ && NF { if (($NF >= 80) && ($NF < 83)) { print ; print ; w++ } else if ($NF >= 83) { print ; c++ } } END { printf("%d:OK %d:WARNING %d:CRITICAL\n", o, w, c) }' /tmp/test.log ... (3 Replies)
Discussion started by: SkySmart
3 Replies

3. Shell Programming and Scripting

Pipe causing last command error to not function

Hi I am quite new to scripting and cannot work out how to do the following - I want to pipe to a log file and then use the "last statement error" in an if statement after, and this doesn't work because it checks the pipe statement instead of the script. Example: executteTheScript $var |... (4 Replies)
Discussion started by: erjorgito
4 Replies

4. Shell Programming and Scripting

Help with FTP Script which is causing "syntax error: unexpected end of file" Error

Hi All, Please hav a look at the below peice of script and let me know if there are any syntax errors. i found that the below peice of Script is causing issue. when i use SFTP its working fine, but there is a demand to use FTP only. please find below code and explain if anything is wrong... (1 Reply)
Discussion started by: mahi_mayu069
1 Replies

5. Shell Programming and Scripting

Error Message in function causing failure.....

I have a long busybox ash script that has 3 stages. 1. Identify and Capture information on variable data sources, output the information to text file on each data source. 2. Using data from 1 above now actually do data processing on each individual dataset. 3. Produce report. So... (6 Replies)
Discussion started by: tesser
6 Replies

6. Shell Programming and Scripting

Awk script problem - Variables Causing Issue

can someone please explain to me what i'm doing wrong with this code: WELT=$(awk '(($1 ~ "^${caag}$") || ($2 ~ "^${caag}$"))' /tmp/Compare.TEXT) when run from the command line, it works. but it seems to be having a problem doing the comparison when variables are involved. i tested from... (1 Reply)
Discussion started by: SkySmart
1 Replies

7. Shell Programming and Scripting

Sed and white spaced string problem

Consider a string with white space "abc 123" string="abc 123" echo $string abc 123 sed "s/$string/$string 456/" file abc 123 456 cat file abc 123 OK for now, but if you put this one in a shell script it gives the result: abc 456 123 somehow string end in first white space ?? ... (3 Replies)
Discussion started by: xramm
3 Replies

8. Linux

how to cut column that is not equally spaced by delimeters

hi friends, when i run this command on my machine; df -k /export/home output of this command i gets as: /Filesystem kbytes used avail capacity Mounted on /dev/dsk/c0t1d0s7 5043168 4520584 472153 91% /export/home i want to use column avail from above output: i... (1 Reply)
Discussion started by: johnray31
1 Replies

9. Solaris

Memory error causing reboot

Hi there I have a box that at 4pm started recieving soft errors on a DIMM, normally this is ok and we have time to swap it out. But I got the following error which caused the box to reboot NOTE: there were abount 6 or 7 normal "soft error encountered" messages before this one Nov 7... (1 Reply)
Discussion started by: hcclnoodles
1 Replies

10. Solaris

Explorer causing syslog error

Hi there, I have upgraded my explorer (SUNWexplo) on a solaris 10 Sparc box from version 3.4 to the latest version (5.5) . However im a little concerned, whenever I run the new explorer either manually or scheduled, I get a syslog event as follows 1 in 0:08:31: Sep 22 17:00:15 fmy.machine.com... (8 Replies)
Discussion started by: hcclnoodles
8 Replies
Login or Register to Ask a Question