04-24-2014
Some additional explanation might be helpful. The awk program is going to process the first file all the way through and then it will process the second file. NR is the total number of records seen so far. FNR is the total number of record seen from the current input file. If FNR == NR we are reading the first file. During the sceond file NR will be larger than FNR.
Look at FNR==NR {f1[$0];next}. While we are reading the first file the code in the braces will be run. f1[$0]; cause an array element to pop into existence. And the next just says we are done with the current record. So during the processing of the first file we are simply building up an array with one element for each unique input line.
During the processing of the second file, we skip the above code and proceed to the second snippet of code: !($1 in f1). This just asks if $1 can be be found in the array. Actually the explanation point flip the questions so it really asks if the record cannot be found in the array. But here we have nothing in braces to tell us what to do. So we do the default action which is to print the current record.
$0 is the whole input record. $1 is the the first field. So we compare the whole input record of the first file to first field of the second file.
Last edited by Scrutinizer; 04-24-2014 at 03:53 PM..
This User Gave Thanks to Perderabo For This Post:
10 More Discussions You Might Find Interesting
1. Shell Programming and Scripting
Hi,
I found this command in this forum, but, couldnt understand much from it.
could any one help me understand that???
the commands are :
awk '{sub(/ ~/,""); printf $0 ($0~/\|$/?ORS:"")}' file1 > file2
awk '{sub(/~ */,x);printf $0(/\|$/?ORS:x)}'
awk '{sub(/~ */,x);sub(/\|$/, "|\n")}8'... (4 Replies)
Discussion started by: hitmansilentass
4 Replies
2. Shell Programming and Scripting
please explain this awk '{print \$NF}'
i have a command
grep -i adding /logs/eap | grep -iv equation | awk '{print \$NF}' | sort -u | sed 's/\.\$//' >> /temp/t
please explain the above awk and sed
as well how it works
and also what is \$NF (1 Reply)
Discussion started by: mail2sant
1 Replies
3. Shell Programming and Scripting
echo "23.54" | awk '
function round(A) {
return int( A + 0.5 )
}
{
printf("%d\n",round($1));
}'> > > > > >
awk: syntax error near line 2
awk: bailing out near line 2 (2 Replies)
Discussion started by: santosh1234
2 Replies
4. Shell Programming and Scripting
Hi frnds,
one my frnds has given resolution for my problem as below. it working great , but i couldnt understand somethings in the script.
Why ++ operator after the function calling. how these each block working. will each run for each input line sequencially or one block for all the lines... (9 Replies)
Discussion started by: Gopal_Engg
9 Replies
5. UNIX for Dummies Questions & Answers
awk '!_++'
Most importantly, I want to know what the underscore does "!_"
But ideally, please breakdown the whole thing. It is supposed to remove duplicate lines when found in a file. (1 Reply)
Discussion started by: glev2005
1 Replies
6. Shell Programming and Scripting
Hi Guys,
Please help me, I am new to programming and I don’t understand what some parts of this code are doing. I have comments on the parts I know, please help if my understanding of the code is not correct and also help with parts with questions.
awk '
{
gsub( ">",... (1 Reply)
Discussion started by: James_Owen
1 Replies
7. Shell Programming and Scripting
Dear all ,
Can any people explain this awk command?
What is the purpose of if (v++){b=$i;$i=""}?
awk -F, '{for (i=1;i<=NF;i++){if (v++){b=$i;$i=""}};print $0} END { print "dups are" ;for ( i in b) print i}' OFS="," input_file
This script is used to replace column duplicate value
... (1 Reply)
Discussion started by: eldonlck
1 Replies
8. Shell Programming and Scripting
found this handy one liner in another thread which is closed, it does what i need but im trying to understand it. it basically matches the field that contains the value v and prints its position
awk -F, '{for(i=1;i<=NF;i++)if($i==v)print i}' v=yourfield inputfile
my understanding is assign... (3 Replies)
Discussion started by: jack.bauer
3 Replies
9. Shell Programming and Scripting
Referring to this:
#!/bin/awk -f
BEGIN{
for(i=0;i<=10;i++)
{
if (i <=1 )
{
x=0;
y=1;
print i;
}
else
{
z=x+y;
print z;
x=y;
y=z;
}
} (3 Replies)
Discussion started by: p1ne
3 Replies
10. UNIX for Beginners Questions & Answers
Example:
`abc.ksh | grep '^GLIS'| awk -F' ' '{print \$1}'`; (3 Replies)
Discussion started by: Tanu
3 Replies
fwtmp(1M) fwtmp(1M)
NAME
fwtmp, wtmpfix - manipulate connect accounting records
SYNOPSIS
[files]
DESCRIPTION
fwtmp
reads from the standard input and writes to the standard output, converting binary records of the type found in to formatted ASCII records.
The ASCII version is useful to enable editing, via ed(1), bad records or for general purpose maintenance of the file.
The argument is used to denote that input is in ASCII form, and output is to be written in binary form. The arguments and are independent,
respectively specifying ASCII input and binary output. Therefor, is an ASCII to ASCII copy and is a binary to binary copy. should be used
for reading If is not used, structure is read.
wtmpfix
examines the standard input or named files in format, corrects the time/date stamps to make the entries consistent, and writes to the stan-
dard output. A can be used in place of files to indicate the standard input. If time/date corrections are not performed, will fault when
it encounters certain date-change records.
Each time the date is set, a pair of date change records is written to The first record is the old date denoted by the string old time
placed in the line field and the flag placed in the type field of the structure. The second record specifies the new date, and is denoted
by the string placed in the line field and the flag placed in the type field. uses these records to synchronize all time stamps in the
file. nullifies date change records when writing to the standard output by setting the time field of the structure in the old date change
record equal to the time field in the new date change record. This prevents and from factoring in a date change record pair more than
once.
In addition to correcting time/date stamps, wtmpfix checks the validity of the name field to ensure that it consists solely of alphanumeric
characters or spaces. If it encounters a name that is considered invalid, it changes the login name to and writes a diagnostic to the
standard error. This minimizes the risk that will fail when processing connect accounting records.
DIAGNOSTICS
wtmpfix generates the following diagnostics messages:
WARNINGS
generates no errors, even on garbage input.
FILES
SEE ALSO
ed(1), acct(1M), acctcms(1M), acctcom(1M), acctcon(1M), acctmerg(1M), acctprc(1M), acctsh(1M), runacct(1M), acct(2), acct(4), utmp(4),
wtmps(4).
STANDARDS CONFORMANCE
fwtmp(1M)