Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

print specific field from first line in file using awk

Tags:

field

awk

line

I have a file (logins.txt) that I need to make into a file that only displays the first and last login date/time and the last login date/time. The logins.txt looks like this:

Feb 13 2:43 (login IP)
Feb 13 2:48 (login IP)
Feb 13 5:23 (login IP)
Feb 13 9:43 (login IP)
Feb 14 1:34 (login IP)
Feb 14 8:42 (login IP)
Feb 14 8:47 (login IP)

I want to print:

First Login: Feb 13 2:43
Last Login: Feb 14 8:47

Now keep in mind I'm using an .awk file where I have all my awk statements in that executes logins.txt and makes a different file with what I want to print out.

The last login was easy. I simply just put:

END{
print "$1, $2, $3";
}

Which prints out the last occurrence of those fields. But how do I do it with the first occurrence?

EDIT: Awesome, putting NR==1 works but I NEED this in the END field because I need it below other stuff I want to print. How do I put it below everything? Also I want it to print like this:

Blah blah blah blah blah blah blah blah
Blah blah blah blah blah blah blah blah
Blah blah blah blah blah blah blah blah
First Login: Feb 13 2:43      Last Login: Feb 14 8:47

Here is my END with print statements

NF==1 {a=$1; b=$2; c=$3;}

END{
   print "Feed\t\t\tAccepted\tRejected\tCanceled";
   print "-----------------------------------------------------------------";
   print "swrinde:\t\t", accepted_sw"\t\t", rejected_sw"\t\t", canceled_sw"\t\t";
   print "news.cais.net:\t\t", accepted_ne"\t\t", rejected_ne"\t\t", canceled_ne"\t\t";
   print "\?:\t\t\t", accepted_un"\t\t", rejected_un"\t\t", canceled_un"\t\t";
   print "\nStart Time = ", a, b, c RS "\tEnd Time =", $1, $2, $3;
}
like image 228
Fred Lohrman Avatar asked Dec 15 '22 13:12

Fred Lohrman


1 Answers

Qualify another block with NR==1:

awk 'NR==1 { print "First Login:", $1, $2, $3 } END { print "Last Login:", $1, $2, $3 }' logins.txt

Results:

First Login: Feb 13 2:43
Last Login: Feb 14 8:47

NR is short for number of rows. See here. HTH.


EDIT1:

Here's two ways, depending on your style. The first would be considered more correct:

awk 'NR==1 { a=$1; b=$2; c=$3 } { print "Blah" } END { print "First Login:", a, b, c  RS "Last Login:", $1, $2, $3 }' logins.txt

awk 'NR==1 { x=("First Login:" FS $1 FS $2 FS $3) } { print "Blah" } END { print x RS "Last Login:", $1, $2, $3 }' logins.txt

Results:

Blah
Blah
Blah
Blah
Blah
Blah
Blah
First Login: Feb 13 2:43
Last Login: Feb 14 8:47

EDIT2:

Run your script like:

awk -f script.awk logins.txt

Contents of script.awk:

NR==1 {
    a=$1
    b=$2
    c=$3
}

{
    print "Blah"
}

END {
    print "First Login:", a, b, c  RS "Last Login:", $1, $2, $3
}

EDIT3:

BEGIN { OFS="\t\t" }

NR==1 { a=$1; b=$2; c=$3 }

END{
   print "Feed\t\t\tAccepted\tRejected\tCanceled"
   print "-----------------------------------------------------------------"
   print "swrinde:", accepted_sw, rejected_sw, canceled_sw
   print "news.cais.net:", accepted_ne, rejected_ne, canceled_ne
   print "?:\t", accepted_un, rejected_un, canceled_un

   print "Start Time = " a FS b FS c
   print "End Time = " $1 FS $2 FS $3
}

Results:

Feed            Accepted    Rejected    Canceled
-----------------------------------------------------------------
swrinde:                        
news.cais.net:                      
?:                          
Start Time = Feb 13 2:43
End Time = Feb 14 8:47
like image 185
Steve Avatar answered Dec 21 '22 22:12

Steve