awk conditions failing (special characters?)


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting awk conditions failing (special characters?)
# 1  
Old 01-29-2015
awk conditions failing (special characters?)

This is really frustrating because I can't figure it out.

I'm running a health check script. One of the items I'm checking is the amount of memory on a server. I use the free command, which outputs something like this (excerpt)

Code:
Mem: 100 100 100 100
Swap: 100 100 100 100

In my debugging I see that $1 is "Mem:" or "Swap:" (note the ":" does come out of `free`)

My if condition is if ($1 == "Swap:") then do something

On the screen the condition matches perfectly. But to awk it does not. It never falls into that condition. What am I doing wrong? How do I build my condition so that I can check $1 for "Swap:"? Is it the colon ":" that's throwing me off?

Last edited by vgersh99; 01-29-2015 at 01:36 PM.. Reason: code tags, please!
# 2  
Old 01-29-2015
Publishing more code maybe give us more understanding.

Both rule works if input is as you have written.
Code:
# awk rule using default FS
$1 == "Swap:" { # do something
                     }
# or
{
      if ( $1 == "Swap:" ) {
             # do something
             }

}

If you processing some commands then maybe shell script is better as awk:
Code:
while read key val1 val2 val3 val4 valxxx
do
             case "$key" in
                   Swap:) #do_some 
                              ;;
                    Mem:) #do_some 
                              ;;
             esac
done < inputfile

This User Gave Thanks to kshji For This Post:
# 3  
Old 01-29-2015
$1=="Swap:" works for me.
To check, print out $1 from within awk.
Pipe the free result through hd or od -ctx1.
This User Gave Thanks to RudiC For This Post:
# 4  
Old 01-29-2015
Thanks a lot for the help!
Here's what I have. The file it's parsing is generated from an
"
Code:
free -m:|egrep 'Mem|Swap' | awk '{ print $1, $2, $3, $4} > file.out

The contents of file.out are:
Code:
Mem: 1001 800 200
Swap: 2015 98 1917

So:
Code:
awk '{
printf "$1 $2 $3 $4 are " $1 " " $2 " " $3 " " $4 "\n" #(this is my debug statement)
if ($1 == "Swap:" && $3 != 0)  # I comment out the && and it's the $1 failing for sure
  printf "WARNING - SWAP IN USE \n"
else
  some other checks which all work fine...
}' file.out >> health_check.out
cat health_check.out #another debug, actually I've got a mailx built here

Produces this:
Code:
$1 $2 $3 $4 are Mem: 1001 800 200
Output for falling through the if statement
$1 $2 $3 $4 are Swap: 2015 98 1917

same type stuff from above for falling through the else clause. Definitely not the "WARNING - SWAP IN USE" I was hoping for.

---------- Post updated at 02:17 PM ---------- Previous update was at 02:10 PM ----------

Excellent call on the od command. I forgot about that. It pans out as "Swap:", just like it's supposed to, but at least I know there's no special characters or anything else in there that might throw the condition expression off.

Last edited by Don Cragun; 01-29-2015 at 04:17 PM.. Reason: Add CODE and ICODE tags.
# 5  
Old 01-29-2015
Not the slightest idea what's happening. On my linux:
Code:
free -m | awk '/^Swap/ && $3 != 0 {print "Swap in use"}'
Swap in use

---------- Post updated at 21:47 ---------- Previous update was at 21:41 ----------

Wait a second - try to continue the line after the if (...) either with a "\" or an opening "{" or put the print right after it ...
# 6  
Old 01-29-2015
I've run into this before. For some reason my printf $4 which is the last variable on the print line did some sort of carriage return on the same line and covered up my output with the rest of the print statement. When I changed it to $3 instead or any other variable the output was fine. $4 is just a numeric like the rest, but it screwed with my output. So I changed the verbiage a little to get the same output. Perhaps the last output on the line has an embedded <CR> in it. Hey, that gives me another check with Rudi's od!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Awk: split column if special characters

Hi, I've data like these: Gene1,Gene2 snp1 Gene3 snp2 Gene4 snp3 I'd like to split line if comma and then print remaining information for the respective gene. My code: awk '{ if($1 ~ /,/){ n = split($0, t, ",") (7 Replies)
Discussion started by: genome
7 Replies

2. Shell Programming and Scripting

Handle special characters in awk -F

Hello Folks, Need to bisect strings based on a subset. Below works good. echo /a/b/c/d | awk -F"/c/d$" '{print $1}' /a/b However, it goes awry with special characters. echo /a/b/c+/d | awk -F"/c+/d$" '{print $1}' /a/b/c+/d Desired output: /a/b Escaping the special characters... (11 Replies)
Discussion started by: vibhor_agarwali
11 Replies

3. Shell Programming and Scripting

awk match shell variable that contains special characters?

How to match a shell variable that contains parenthesis (and other special characters like "!") file.txt contains: Charles Dickens Matthew Lewis (writer) name="Matthew Lewis (writer)"; awk -v na="$name" ' $0 ~ na' file.txt Ideally this would match $name in file.txt (in this... (3 Replies)
Discussion started by: Mid Ocean
3 Replies

4. Shell Programming and Scripting

Sed failing for variable with special characters

This has been covered many times earlier but couldnt figure the issue myself. Can you please advise whats wrong on the below code I have a variable with special character ($) and am using that variable to replace another variable in file but however sed is failing to parse it correctly ... (7 Replies)
Discussion started by: sasiharitha
7 Replies

5. UNIX for Dummies Questions & Answers

awk for removing special characters and extra commas

Hi, I have a .csv file which as empty lines with comma and some special characters in 3rd column as below. Source data 1,2,3,4,%#,6 ,,,,,, 1,2,3,4,5,6 Target Data 1,2,3,4,5,6I need to remove blank lines and special charcters I am trying to get this using the below awk awk -F","... (2 Replies)
Discussion started by: shruthidwh
2 Replies

6. Shell Programming and Scripting

awk loop: display special characters

Hi everybody; I have a code and this fetches data from first.txt,modify it and outputs it to second.txt file. l awk 'NR>1 {print "l ./gcsw "$1" lt all lset Data="$2" Value "$3}' /home/gcsw/first.txt > /home/gcsw/second.txt this outputs as: l ./gcsw 123 lt all lset Data=456 Value 789 ... (1 Reply)
Discussion started by: gc_sw
1 Replies

7. Shell Programming and Scripting

awk print $1 escape all special characters

I'm using awk '{print $1}' and it works most of the time to print the contents of a mysql query loop, but occationally I get a field with some special character in it, is there a way to tell awk to ignore all special characters between my FS? I have >186K records, so building a list of ALL special... (6 Replies)
Discussion started by: unclecameron
6 Replies

8. Shell Programming and Scripting

awk search pattern with special characters passed from CL

I'm very new to awk and sed and I've been struggling with this for a while. I'm trying to search a file for a string with special characters and this string is a command line argument to a simple script. ./myscript "searchpattern" file #!/bin/sh awk "/$1/" $2 > dupelistfilter.txt sed... (6 Replies)
Discussion started by: cue
6 Replies

9. Shell Programming and Scripting

Handling special characters using awk

Hi all, How do I extract a value without special characters? I need to extract the value of %Used from below and if its greater than 80, need to send a notification. I am doing this right now..Its giving 17%..Is there a way to extract the value and assign it to a variable in one step? df |grep... (3 Replies)
Discussion started by: sam_78_nyc
3 Replies

10. Shell Programming and Scripting

awk/sed with special characters

i have this script that searches for a pattern. However it fails if the pattern includes some special characters. So far, it fails with the following strings: 1. -Cr 2. $Mj 3. H'412 would a sed or awk be more effective? i don't want the users to put the (\) during the search (they... (5 Replies)
Discussion started by: apalex
5 Replies
Login or Register to Ask a Question