How to pass cat file in awk statement?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to pass cat file in awk statement?
# 1  
Old 04-16-2015
How to pass cat file in awk statement?

Hi,

I am working on kernel parameters, want to check values under /proc/sys/kernel

below I tried for kernel.sem

Code:
 
SEMMNS:    4096              cat /proc/sys/kernel/sem | awk '{print $2}'
 
awk '{ if ($2 < 33000) print }' /proc/sys/kernel/sem |awk '{print $2}'
32000


The above awk worked.

I am also trying for

Code:
 
SHMMAX:     2145745cat /proc/sys/kernel/shmmax


I am trying below awk, but it is not working

Code:
 
awk '{if ($(cat /proc/sys/kernel/shmmax) > 200) print}' /proc/sys/kernel/shmmax
awk: (FILENAME=/proc/sys/kernel/shmmax FNR=1) fatal: division by zero attempted


# 2  
Old 04-16-2015
What operating system are you using?

Why the 2nd awk is failing is clear:
Code:
$(cat /proc/sys/kernel/shmmax)

in an awkscript is an attempt to look at the contents of the field specified by dividing the value of the awk variable cat by the value of the awk variable proc ... divided by the value of the awk variable shmmax. And, since none of those awk variables are defined in your awk script, they all evaluate to 0 (i.e., $(0/0/0/0)) which dies on the attempt to divide by 0.

What you are trying to do is not clear. What output do you get from the command:
Code:
cat /proc/sys/kernel/shmmax

when you type it into your shell?
# 3  
Old 04-16-2015
when I do
Code:
cat /proc/sys/kernel/shmmax

got
Code:
2458761

(kernel.shmmax parametre value. and Os is RHEL6

Last edited by stew; 04-16-2015 at 04:49 AM..
# 4  
Old 04-16-2015
So why are you using awk for either of these?

Code:
read x semmns < //proc/sys/kernel/sem
read shmmax < /proc/sys/kernel/shmmax
[ "$semmns" -lt 33000 ] && printf 'semmns: %d\n' "$semmns"
[ "$shmmax" -gt 200 ] && printf 'shmmax: %d\n' "$shmmax"

seems to do what you want.

If you insist on using awk and for some reason you don't want to do it the same way you checked SEMMNS (using $1 instead of $2), you could also try something like:
Code:
awk 'BEGIN{get line shmmax < "/proc/sys/kernel/shmmax"; if((shmmax + 0) > 200) print shmmax}'


Last edited by Don Cragun; 04-16-2015 at 05:19 AM.. Reason: Add +0 to guarantee numeric comparison and fix autospellcheck induced errors.
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 04-16-2015
Thank you Smilie It worked. I guess get line should be "getline"

---------- Post updated at 04:31 AM ---------- Previous update was at 03:47 AM ----------

I have one more doubt, I ran below command the value
Code:
kernel.shmmax = 2458761

is added under /etc/sysctl.conf
Code:
 
 awk ' BEGIN{getline shmmax < "/proc/sys/kernel/shmmax"; if((shmmax + 0) > 200) print "kernel.shmmax = " shmmax}' >>/etc/sysctl.conf

In few servers" cat /proc/sys/kernel/shmma"x value would be already present in "/etc/sysctl.conf", in that case it should not add nedd to update the vlaue

Code:
 
"/usr/bin/awk ''BEGIN{getline shmmax < "/proc/sys/kernel/shmmax"; if((shmmax + 0) > 200) sub(existingvalue,shmmax}' >>/etc/sysctl.conf

want to substitute instead of new entry, is it possible
# 6  
Old 04-16-2015
When tampering with system parameters, you should know EXACTLY wha you are doing! So, please be very careful and make backups.
Try
Code:
awk     '
BEGIN                   {getline shmmax < "/proc/sys/kernel/shmmax"
                         if((shmmax + 0) > 200)
                        }
/^kernel.printk/        {$3 = shmmax
                         done=1
                        }
1
END                     {if (done) exit
                         print "kernel.shmmax = ", shmmax
                        }
        ' /etc/sysctl.conf

and, when happy, redirect the output to a temp file and mv it to the target, then.
# 7  
Old 04-16-2015
Quote:
Originally Posted by stew
Thank you Smilie It worked. I guess get line should be "getline"
Yes. I apologize. I installed a security update to my browser last weekend, and the spell checker is apparently much more aggressive at fixing (supposed) mistakes (even after I have told it not to change something I typed). For some reason it thought it should change shmmax to shammy and when I went back in to fix that, it silently also changed getline to get line. I'm going to have to watch it more closely (or disable it).

It looks like RudiC's suggestion was also mangled by something... The line he suggested:
Code:
/^kernel.printk/        {$3 = shmmax

should be:
Code:
/^kernel.shmmax/        {$3 = shmmax

This User Gave Thanks to Don Cragun 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

Need to add prefix using sed or awk from cat the file

I need the use sed or AWK using cat the file Node1 TDEV RW 1035788 TDEV RW 1035788 Server1 TDEV RW 69053 Server2 TDEV RW 69053 TDEV RW 103579 Server3 TDEV RW 69053 server4 RDF1+TDEV RW 69053 RDF1+TDEV RW 517894 RDF1+TDEV RW 621473 server6 TDEV RW 34526 TDEV RW 34526 (22 Replies)
Discussion started by: ranjancom2000
22 Replies

2. Shell Programming and Scripting

Cat a script, pipe it, then pass arguments to it?

suppose i have a perl script that is normally run this way: ./checkdisk.pl -H hostname -w 40 -c 80 but, for whatever reason, i cannot run the script directly as it should. But i can cat it through pipe. How can i pass the arguments "-H hostname -w 40 -c 80"? so this is what i'm doing,... (6 Replies)
Discussion started by: SkySmart
6 Replies

3. Shell Programming and Scripting

Convert Update statement into Insert statement in UNIX using awk, sed....

Hi folks, I have a scenario to convert the update statements into insert statements using shell script (awk, sed...) or in database using regex. I have a bunch of update statements with all columns in a file which I need to convert into insert statements. UPDATE TABLE_A SET COL1=1 WHERE... (0 Replies)
Discussion started by: dev123
0 Replies

4. Shell Programming and Scripting

Pass values to case statement in a function korn shell

I'm in the process of writng a function that consists of a case statement is there a way of calling the function and passing a value to it? ie function1 () { case opt1 do ..... opt2 do..... esac } function opt1 I'm aware the syntax is not correct, but you get the general idea. (1 Reply)
Discussion started by: squrcles
1 Replies

5. Shell Programming and Scripting

How to pass tablenames from a file to shell script to execute create statement in DB2

Hi, I am new to Shell Scripting, and I need to create nicknames for 600 tables in db2. I have the file names in a text file and i have to pass these table names to a shell script create nicknames in db2. Can some one please help me in this regard. (1 Reply)
Discussion started by: kamalanaatha
1 Replies

6. Shell Programming and Scripting

Pass a DDL statement to a KSH script

I need to pass a DDL statement into a ksh script & parse the statement. What is the best way to pass a DDL statement into a KSH script. ---------- Post updated at 09:28 AM ---------- Previous update was at 07:35 AM ---------- if the name of the script is test.ksh test.ksh "ALTER TABLE... (12 Replies)
Discussion started by: gayathree
12 Replies

7. Shell Programming and Scripting

how to pass a variable to an update sql statement inside a loop

hi all, i am experiencing an error which i think an incorrect syntax for the where clause passing a variable was given. under is my code. sqlplus -s ${USERNAME}/${PASSWORD}@${SID} << END1 >> $LOGFILE whenever sqlerror exit set serveroutput on size 1000000 declare l_rc ... (0 Replies)
Discussion started by: ryukishin_17
0 Replies

8. Shell Programming and Scripting

want to pass sentence with SED and CAT

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' > runx... (1 Reply)
Discussion started by: patilrakesh1984
1 Replies

9. Shell Programming and Scripting

How to pass parameters to an awk file?

I have an awk file where I need to pass a filename and a value as a parameter from a sh script. I need to know how to pass those values in the sh script and how to use the same in the awk file. Thanks in advance!!! Geetha (3 Replies)
Discussion started by: iamgeethuj
3 Replies

10. Shell Programming and Scripting

I can't seem to pass variables properly into a nawk statement

Ok, So up front I'm going to say that I'm a very elementary scripter, and I tend to use tools I don't fully understand, but I shotgun at something until I can get it to work...that said, I can't for the life of me understand why I can't get this to go down the way I want it to. The goal: -to... (6 Replies)
Discussion started by: DeCoTwc
6 Replies
Login or Register to Ask a Question