Redirect output to the same input file in awk


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Redirect output to the same input file in awk
# 15  
Old 02-26-2015
Don - when I tried Ravichander post#9, print statement only printed the line what I edited
Code:
awk '/kernel.sem/ { if ($3 < "300") { sub($3,"300",$0); print >> "/tmp/test1" } else {print >> "/tmp/test1" }}' ACTUAL_FILE

please find the o/p below

Code:
 
/tmp/test1
kernel.sem = 300   32000   32      128250

So if I move this test1 to ACTUAL file, the file will be replaced with the content of test1.

DO you have any other option to achieve my requirement
# 16  
Old 02-26-2015
First, be sure that you have a back-up copy of sysctl.conf before you try this. Then try something like:
Code:
nawk '
/kernel.sem/ {
	if(($3 + 0) < 300)
		sub($3, "300")
	else	exit(1)
}
1' sysctl.conf > sysctl.conf.$$ && \
	mv sysctl.conf.$$ sysctl.conf || \
	rm sysctl.conf.$$


Last edited by Don Cragun; 02-26-2015 at 04:31 AM..
This User Gave Thanks to Don Cragun For This Post:
# 17  
Old 02-26-2015
Thanks Don. I have two doubts in your code

($3 + 0) - why you used + 0 in here
} 1' filename - why used 1 here, is it for prinitng purpose.
# 18  
Old 02-26-2015
Quote:
Originally Posted by stew
Thanks Don. I have two doubts in your code

($3 + 0) - why you use[ICODE]d + 0 in here
Because ($3 < "300") performs a string comparison and would give you the wrong result if in some cases. For example, if the current contents of field 3 was 1000, it would reduce it to 300; and if the current contents of field 3 was 40, it wouldn't increase it to 300.
Quote:
} 1' filename - why used 1 here, is it for prinitng purpose.
Yes, the default action in awk when a condition evaluates to true (non-zero) is to print the current line.

Did you try what I suggested? Did it give you the wrong results for any value on the kernel.sem line in your sysctl.conf file(s)?

Last edited by Don Cragun; 02-26-2015 at 05:49 AM.. Reason: Fix tags.
# 19  
Old 02-26-2015
yes. your code is working perfectly. I am using this sample peice for puppet automation. so for awk part I don't hard code anything. I will write all vlaues to some other yaml file and will ivoke that value variable into puppet awk function.

so my code would be

Code:
awk '/kernel.sem/ { if(($3 + 0) < $value) sub($3, "$value") } 1' /etc/sysctl.conf >> /etc/sysctl.conf.bkp

so what my doubt is, when I use $value parameter should I still use +0 on this
# 20  
Old 02-26-2015
Quote:
Originally Posted by stew
yes. your code is working perfectly. I am using this sample peice for puppet automation. so for awk part I don't hard code anything. I will write all vlaues to some other yaml file and will ivoke that value variable into puppet awk function.

so my code would be

Code:
awk '/kernel.sem/ { if(($3 + 0) < $value) sub($3, "$value") } 1' /etc/sysctl.conf >> /etc/sysctl.conf.bkp

so what my doubt is, when I use $value parameter should I still use +0 on this
I have no idea what peice, puppet automation, vlaues, nor yaml mean, and there is no puppet() function in this awk script??? The awk variable value is not defined in this script and comparing the number in field 3 of a line to the entire line as a string (containing something like:
Code:
kernel.sem = 250   32000   32      128250

which is what $undefined_variable will expand to in your awk script with your sample input file) is not what you want. If you want to parameterize the threshold value for changes instead of hard coding it to 300, you need something like:
Code:
value=300
awk -v val="$value" '
/^kernel.sem/ {
	if(($3 + 0) < val)
		sub($3, val "")
	else	exit(1)
}
1' sysctl.conf > sysctl.conf.mod && \
	mv sysctl.conf.mod sysctl.conf || \
	rm sysctl.conf.mod

Please DO NOT use the name sysctl.conf.bkp in this script. It is not a backup file; it is a temp file used as a work area for this script. And, if the number found in the 3rd field on the "kernel.sem" line is greater than or equal to your value variable, it will only contain a subset of your file (since this awk script returns an error status and stops processing input if it is not going to change that line. This file will NEVER contain the original contents of your input file unless your input file does not contain a line that contains the string "kernel.sem"! This leaves the current sysctl.conf in place (instead of replacing it with a copy of itself) if no changes are being made, and runs faster than processing the rest of the file and replacing an unmodified file.

Note that I added an anchor to the ERE selecting the line to be changed in case your configuration file might contain a comment that explains why kernel.conf is being set.
# 21  
Old 02-26-2015
I am okay with your code, but my concern is move command. shall we use
Code:
cp sysctl.conf.mod sysctl.conf

rather than
Code:
mv sysctl.conf.mod sysctl.conf

 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to reformat output if input file is empty, but not if file has data in it

The below awk improved bu @MadeInGermany, works great as long as the input file has data in it in the below format: input chrX 25031028 25031925 chrX:25031028-25031925 ARX 631 18 chrX 25031028 25031925 chrX:25031028-25031925 ARX 632 14... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. UNIX for Dummies Questions & Answers

awk - Rename output file, after processing, same as input file

I have one input file ABC.txt and one output DEF.txt. After the ABC is processed and created output, I want to rename ABC.txt to ABC.orig and DEF to ABC.txt. Currently when I am doing this, it does not process the input file as it cannot read and write to the same file. How can I achieve this? ... (12 Replies)
Discussion started by: High-T
12 Replies

3. Shell Programming and Scripting

Read file from input and redirect to output file

Hi , i am having an file which contains 5 file_name data, i need to read the file name and will perform certain operation and generate out file names with named as 5 individual file_names for eg: file.txt contains file_name1.txt|hai file_name2.txt|bye file_name3.txt|how... (3 Replies)
Discussion started by: rohit_shinez
3 Replies

4. Shell Programming and Scripting

AWK Script to convert input file(s) to output file

Hi All, I am hoping someone can help me with some scripting I need to complete using AWK. I'm trying to process multiple fixed files to generate one concatenated fixed file in a standard format. The Input file is:- aaaa bbbbb ccccc 1 xxxx aaa bbb aaaa bbbbb ccccc 2 abcd aaa CCC... (9 Replies)
Discussion started by: jason_v_brown
9 Replies

5. UNIX for Advanced & Expert Users

Complex Input/Output Redirect

Hi All, Sorry if the title is not good but I did not know how to explain with only some words! What I meant is: I have a unix command built from a private application vendor that when executed it prompts for two entries by the keyboard, let's say, for example: ... (1 Reply)
Discussion started by: felipe.vinturin
1 Replies

6. Shell Programming and Scripting

redirect an awk string output to a script input with pipes

Hi, I have a function in a bash script that returns a string after some operations using awk. The following code returns 555 $VARIABLE="EXAMPLE" get_number $VARIABLE this value I'd like to pass it as a second argument of another script with the following usage myscript.sh <param1>... (7 Replies)
Discussion started by: rid
7 Replies

7. Programming

Redirect input and output to a shell script?

Dear All: I am trying to do something that (I thought) was relatively straightforward, but my code snippet does not seem to work. Any suggestions? Thank you Sincerely yours Misha Koshelev #include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <errno.h> #include... (0 Replies)
Discussion started by: misha680
0 Replies

8. Shell Programming and Scripting

Pass input and output file as parameter to awk script

Hi, i am new to awk. I am using csv2pipe script(shown below) BEGIN { FS=SUBSEP; OFS="|" } { result = setcsv($0, ",") print } # setcsv(str, sep) - parse CSV (MS specification) input # str, the string to be parsed. (Most likely $0.) # sep, the separator between the values. # #... (6 Replies)
Discussion started by: bhaskarjha178
6 Replies

9. Shell Programming and Scripting

awk should output if one input file doesnt have matching key

nawk -F, 'FNR==NR{a= $3 ;next} $2 in a{print $1, 'Person',$2, a}' OFS=, filea fileb Input filea Input fileb output i am getting : (2 Replies)
Discussion started by: pinnacle
2 Replies

10. Shell Programming and Scripting

Input file redirect in output path and want name as inputfilename_new.txt

not required this time (6 Replies)
Discussion started by: Sandeep_Malik
6 Replies
Login or Register to Ask a Question