A simple awk command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting A simple awk command
# 1  
Old 10-04-2015
A simple awk command

Hi there,

given the file

Code:
FIELD1    FIELD2  FIELD3   FIELD4 FIELD5 FIELD6    FIELD7  FIELD8 FIELD9  FIELD10 FIELD11

Why does not

Code:
awk '$6 == "FIELD6" {$6=="GREEN"}1' file

do the work and replace"FIELD6" by "GREEN"?

And second question, what is the purpose of the "1" at the end of the string before "file"?

Cheers

Last edited by Scrutinizer; 10-04-2015 at 02:49 PM.. Reason: code tags
# 2  
Old 10-04-2015
Try:
Code:
$6="GREEN"

--

Everything in awk has the form condition{action} . If the condition evaluates to 1 then the action is performed. If the condition is omitted then the default condition is 1, so the action is always performed. If the action is omitted then the default action is performed, which is {print $0} . In this case the condition is 1 (true) and the action is omitted, therefore {print $0} is performed, which is "print the entire record".
# 3  
Old 10-04-2015
what a silly mistake. Thank you!
PS,
any hint what is the purpose of that "1"?
# 4  
Old 10-04-2015
Quote:
Originally Posted by la2015
what a silly mistake. Thank you!
PS,
any hint what is the purpose of that "1"?
As Scrutinizer described in his last post, it causes your output to be printed. Without it, you change data on some lines in an internal buffer, but never print the results.
# 5  
Old 10-04-2015
Hello la2015,

Let me try to make it simple and explain you. awk works on a theme which is <:condition:><:action/operations:> so lets say if you have given one condition as follows:
Code:
awk '($1>2){print $0}'  Input_file

So in above example you are checking condition if $1 is greater than 2 then if condition is TRUE then you are asking it to perform action {print $0}, which will print the $0(means complete line) whose $1 is grater than value 2. Now if we are giving like as follows.
Code:
 awk '($1>2)' Input_file

So in above example whenever condition is going to be TRUE then as there is NO action given by us to perform so awk will perform the default action which is printing the line. Similarly by giving 1 in code we are making condition TRUE and not giving any further actions so when condition is TRUE and no action given so awk will do default action which is print. Hope this helps you, enjoy learning Smilie


EDIT: Like an example as follows.
Code:
 awk '($1>2){next} 1' Input_file

So in above example you are giving condition like if $1 is grater than 2 then perform action which is next, means don't do any further action but if condition is FALSE then it will not perform any action and will go to 1 and as 1 will make condition TRUE with no action further given so it will print the line, so basically whichever line's $1 is grater than 2 we are skipping them here.

Thanks,
R. Singh

Last edited by RavinderSingh13; 10-04-2015 at 03:46 PM.. Reason: Added an example for same now.
# 6  
Old 10-04-2015
thank you everyone for such detailed explanations!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Simple awk command to compare two files and print first difference

Hello, I have two text files, each with a single column, file 1: 124152970 123899868 123476854 54258288 123117283 file 2: 124152970 123899868 54258288 123117283 122108330 (5 Replies)
Discussion started by: LMHmedchem
5 Replies

2. Shell Programming and Scripting

Simple awk

Hi, this must be a simple but this is my first interaction with shell and awk. following is a log file needed to parse (2 lines separated by a line break for clarity): 2013-07-27 13:32:09,043 - ERROR - PerformanceUtility - Thread-14 - Performance - 9b348407-4f57-4983-a057-a55669821f68 |... (12 Replies)
Discussion started by: liv2luv
12 Replies

3. Shell Programming and Scripting

Simple awk help

Hey all, so I'm using AWK in a project at work, to generate xml from csv. So far things are going relatively smoothly, but I have one thing I can't figure out. For every field in each row, I must generate <entry name=KWNamex>Field</entry> Then I will need to pull data from a second file... (6 Replies)
Discussion started by: Parrakarry
6 Replies

4. Shell Programming and Scripting

Simple awk use

Hi, I have a file such that: 40454,31,48,4,1304.967741935484,1 31708,25,48,4,1268.32,1 20900,64501,671,788,0.3240259840932699,0 20137,51358,834,743,0.3920908135051988,0 I want to replace the 6th column by "ones" if it is 1, and with "zeros" if it is 0. Thanks. (6 Replies)
Discussion started by: shekhar2010us
6 Replies

5. Shell Programming and Scripting

awk command in script gives error while same awk command at prompt runs fine: Why?

Hello all, Here is what my bash script does: sums number columns, saves the tot in new column, outputs if tot >= threshold val: > cat getnon0file.sh #!/bin/bash this="getnon0file.sh" USAGE=$this" InFile="xyz.38" Min="0.05" # awk '{sum=0; for(n=2; n<=NF; n++){sum+=$n};... (4 Replies)
Discussion started by: catalys
4 Replies

6. Shell Programming and Scripting

awk command for simple join command but based on 2 columns

input1 a_a a/a 10 100 a1 a_a 20 200 b1 b_b 30 300 input2 a_a a/a xxx yyy a1 a1 lll ppp b1 b_b kkk ooo output a_a a/a 10 100 xxx yyy (2 Replies)
Discussion started by: ruby_sgp
2 Replies

7. Shell Programming and Scripting

simple awk help

Whats the syntax to find all lines that matches a text and print out specific fields after the match? ex: 1: some random text 2: Full name: John E. Smith 3: some random text 4: Full name: Mary J. Lue 5: some random text So I'd like to print out First names or last names or everything... (2 Replies)
Discussion started by: linuxdude
2 Replies

8. Shell Programming and Scripting

simple awk

when I execute this awk stmt .. awk "/log_directory/ { print $5}" /opt/dba/oraadmin/tools/tmp_purge_op.log it's returning the whole line as .. IRMD118_LISTENER1 parameter "log_directory" set to /opt/oracle/10.2/network/log/ my expected output is : /opt/oracle/10.2/network/log what... (7 Replies)
Discussion started by: talashil
7 Replies

9. Shell Programming and Scripting

Simple AWK command?

I am not that good with AWK. Is there a simple awk command I could use to get the word "this" from the following text besides using "awk -F ":" '{print $2} | awk -F " " '{print $1}"? :this is:that is: (6 Replies)
Discussion started by: 2dumb
6 Replies

10. UNIX for Dummies Questions & Answers

Need help with a simple command

You don't know the name of the command that could do a particular task. What command could you run to find the name of the command? (4 Replies)
Discussion started by: kurtbudd1
4 Replies
Login or Register to Ask a Question