Confusing of some basic awk


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Confusing of some basic awk
# 1  
Old 03-29-2016
Confusing of some basic awk

1. increase file space
first, double space a file:
Code:
awk '1;{print ""}'

I probably can understand itSmilierint a blank line every time.But when I read triple space a file I am confused:
Code:
awk '1;{print "\n"}'

doesn't it meaning print a blank line every time too?

2. number each line of file, but only print numbers if line is not blank:
Code:
awk 'NF{$0=++a":"$0};1'

what the a mean here?? does that just a avirable with default value of 0?and what the difference of the position of the 1:at the beginning and at the end?


3.IN DOS environment convert Unix newlines to DOS format
Code:
awk 1

I am confused because in Unix exchange format like this:
Code:
awk '{sub(/\r$/),""};1'
awk '{sub(/$/,"\r")};1'

those make sense, but awk 1 doesn't make sense..

4.remove duplicate,consecutive lines:
Code:
awk 'a !~ $0;{a=$0}'

I can't understand it at all.
remove duplicate,nonconsecutive lines:
Code:
awk '!a[$0]++'

this seems very useful,but can't understand it either!
hope you can helpSmilie
# 2  
Old 03-29-2016
So for number 1, by default the print command within awk prints a new line at the end of the line it is printing. As the line here is blank (nothing enclosed within the quotes) it simply prints a blank line, with a new line at the end. When you put \n within the print quotes, the line to print becomes a new line - as well as the new line printed by default at the end!

For number 2 - firstly what is happening here is the code within the action block enclosed within curly braces is being executed only when NF is greater than 0 (i.e. When there is data on the line!). The value of $0 (which contains the entire line) is being re-evaluated to: The value of "a" (the line number), then a single colon and then the actual value of $0 (the entire line). The ++ in front of "a" means the value of "a" will be incremented each time the action block is executed.

To address your point about the position of the number 1 within both awk statements, this is a little trick to invoke awks default action of printing all lines. So putting it before an action block will print the lines prior to the action block being executed and putting it at the end of an action block will print the lines after the action block has been executed. Try doing the below and you will see what I mean:
Code:
awk 1 filename

For number 3, as you can see from the above mention about 'awk 1' - all this will do is invoke awk's default action of printing all of the lines within a file.

For number 4, the first awk statement is simply checking to see if the value of "a" is NOT like the value of $0 (which contains the entire line), if this is true then perform awk's default action of printing the line. After this is done, it will then set a to the value of $0, ready to test the next line.
The second awk statement appends to an array called "a" for every line read. It sets the index for the array as $0 (the line) and the value increments by 1. It works as if it reads a line that has already been added into the array, the value will become greater than 1. The exclamation mark will negate the default action of printing the line being performed as a result as the value is not 0.

Hope that helps, sorry if my explanation is long winded and more confusing!
This User Gave Thanks to pilnet101 For This Post:
# 3  
Old 03-29-2016
Unsure what you are asking/commenting

Are you asking for someone to explain the logic of your examples?
Do you have any knowledge or experience with awk?
# 4  
Old 03-29-2016
Quote:
Originally Posted by joeyg
Are you asking for someone to explain the logic of your examples?
Do you have any knowledge or experience with awk?
I just want to know is there a difference between the position of 1 .
Are
Code:
1;{}

and
Code:
{}1;

the same? If you can help,I will be very thankfulSmilie
Maybe it's just the print order, I am just not very sure,I need some expert say yes or no.

Last edited by hhdzhu; 03-29-2016 at 10:44 AM..
# 5  
Old 03-29-2016
Hello hhdzhu,

If I understood correctly your requirement then you wanted to know difference between awk '1;{print "\n"}' Input_file and awk '{print "\n"};1' Input_file.
Off course there is a difference in case of awk '1;{print "\n"}' Input_file first complete line of Input_file will be printed and then new line. In case of awk '{print "\n"};1' Input_file, first new line will be printed and then the Input_file's line.

Also in awk works on the method of condition and action, so in case when we are mentioning here only 1 which means we are making condition part as TRUE, so when condition is TRUE we should give some action because we haven't given any action here so awk is performing default action which is printing the line, hope this helps you.


Thanks,
R. Singh
This User Gave Thanks to RavinderSingh13 For This Post:
# 6  
Old 03-29-2016
That depends highly on the contents between the braces. As given, the two samples are identical. Should there be any statements inside the braces that either print something or modify any of the fields in the line, the output wil be different.
This User Gave Thanks to RudiC For This Post:
# 7  
Old 03-29-2016
The 1 (outside braces) is a true condition, and its default action is {print} (and that is {print $0}).

Last edited by MadeInGermany; 03-29-2016 at 01:43 PM..
This User Gave Thanks to MadeInGermany For This Post:
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Basic awk...newbie quetion

Hi, I was trying to change the value of the 4th column (put '1' in the 4th column of each row). My awk command is: awk -F, '{$3=1;}1' OFS= input.txt > ./test_out.txt My input file is: a 1 2 31 b 4 5 61 c 7 8 91 My output file (test_out.txt)is: a 1 2 31 b 4 5 61 c 7 8 91 What... (4 Replies)
Discussion started by: pc2001
4 Replies

2. Shell Programming and Scripting

Ps command output confusing

Hi, I ran a script named cat item when I searched for this script using command PS I get two process . I don't understand this. Also this script has run for 15 minutes but the time is showing as 0:00:confused::confused: ps -ef | grep cat_item catmgr 4508 4486 0 05:10:29 ? 0:00... (5 Replies)
Discussion started by: TomG
5 Replies

3. UNIX for Dummies Questions & Answers

Basic awk help

Im sure this is an easy question, but Ive tried and tried to get this to print all on one line and cant figure out why its not, so maybe someone can help awk '/AP/{sub(/:80/, "", $4);printf $4"\t"} /User-Agent/{sub(/^*:/,"");print};sub(/\.80/,"", $4);/Host/{sub(/^*:/,""); print}' What this... (10 Replies)
Discussion started by: sectech
10 Replies

4. Shell Programming and Scripting

basic awk questions

I find an script with awk sitting around. I went through some online manuals, but I can't figure out exactly how it works. I can't post the whole program. Not allowed. This is the line that is confusing me. I get when else is in the script grep -v "^REM " $1| grep -v "JUNK;" | awk -F" "... (2 Replies)
Discussion started by: guessingo
2 Replies

5. UNIX and Linux Applications

pikdev requirements confusing

I am looking at installing PiKdev which needs libqt3-mt and kdelibs4-dev. The installed package is qt-r1008952-i486-1 which claims to be a gui toolkit. find / -name "*libqt*" yields nothing with mt just a lot of support, compatible, and access widgets. Normally I would consider this a no go but... (0 Replies)
Discussion started by: slak0
0 Replies

6. UNIX for Dummies Questions & Answers

Basic awk question...getting awk to act on $1 of the command itself

I have a script problem that I am not able to solve due my very limited understanding of unix/awk. This is the contents of test.sh awk '{print $1}' From the prompt if I enter: ./test.sh Hello World I would expect to see "Hello" but all I get is a blank line. Only then if I enter "Hello... (2 Replies)
Discussion started by: JasonHamm
2 Replies

7. Shell Programming and Scripting

awk basic issue

Hi all, I have an awk basic question. file.text Our Location: Our home has light yellow siding, and is a duplex on Main Street, across from the High School, and across the lane from the Health Center If I run: cat file.txt | awk '{print $2}' | grep... (7 Replies)
Discussion started by: research3
7 Replies

8. Shell Programming and Scripting

Confusing me......!!!!!!

Hiii... There... I am making a Script in which I am taking the value of a variable "var" through key board. But I want, if no values are supplied for "var" for more than 5 seconds then script shuld automatically exit.Script is as follow : #cat abc #!/bin/bash echo "Enter Your Choice : "... (4 Replies)
Discussion started by: prashantshukla
4 Replies

9. Shell Programming and Scripting

Confusing Error

Hi all, Just subscribed to this forum. Not a regular user of Unix.:) I did the following: We have a directory structure /a/b/c5/ Where c5 is the only directory inside b. export ANOOP=/a/b/c*/ echo $ANOOP=/a/b/c5/ I have to create a symbolic link to anoop.txt in the directory... (2 Replies)
Discussion started by: Pankajakshan
2 Replies

10. Solaris

(Need Help) confusing format on solaris 10

Hi All, Very need help about format syntax on solaris 10. I have done install Solaris 10 OS on sun fire v245 but currently i have a problem to use "format" command to display partition info for my hard drive. i cannot enter the format menu, below is captured display : # format Searching... (7 Replies)
Discussion started by: bucci
7 Replies
Login or Register to Ask a Question