1. increase file space
first, double space a file:
I probably can understand itrint a blank line every time.But when I read triple space a file I am confused:
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:
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
I am confused because in Unix exchange format like this:
those make sense, but awk 1 doesn't make sense..
4.remove duplicate,consecutive lines:
I can't understand it at all.
remove duplicate,nonconsecutive lines:
this seems very useful,but can't understand it either!
hope you can help
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: 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!
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
and
the same? If you can help,I will be very thankful
Maybe it's just the print order, I am just not very sure,I need some expert say yes or no.
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:
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.
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)
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)
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)
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)
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)
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)
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)
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)
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)
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)