what does the command awk -F/ mean?

Issue

In more detail, I’ve been doing an assignment that asks for me to find all files under a directory in all caps, saved in a txt file with no overlapping entries, and said directory looks something like this.

website.com - - [date/date/date:date:date:date -0400] "GET /elv/HELLO/wave.gif HTTP/1.0" 200 1318
website.com - - [date/date/date:date:date:date -0400] "GET /elv/ball.gif HTTP/1.0" 200 306

I tried:

grep "/elv/" (filename.txt) | awk '{print $7}' |
awk -F/ '{print $3}' | grep ^[A-Z] | sort -u > elv.txt

I’ve come to the understanding what

grep "/elv/" (filename.txt)

awk '{print $7}'

grep ^[A-Z]

sort -u > elv.txt

mean, but i do not understand why the code

awk -F/ '{print $3}' 

Is then necessary. Is it used to not register the rest of the $7 past the / of /elv/CAPITALISED_WORD after the CAPITALISED_WORD?

I have tried

grep "/elv/" (filename.txt) | awk '{print $7}' |
awk -F/ '{print $3}' | grep ^[A-Z] | sort -u > elv.txt

for my initial test, and the result comes in a txt file with all names that are capitalised after /elv/.

HELLO
(list of other capitalised words)

But when I tried

grep "/elv/" (filename.txt) | awk '{print $7}' | grep ^[A-Z] |
sort -u > elv.txt

The txt file would appear blank.

I would have thought that by putting in the first attempt I made, it would have given a text file that would look like this.

HELLO/wave.gif

Edited for more clarity

Solution

You may use this awk that combines 2 grep and 2 awk commands into one:

awk '/\/elv\// && split($7, a, /\//) && a[3] ~ /^[A-Z]/ {print a[3]}' file |
sort -u

HELLO

To answer your question:

awk -F/ '{print $3}'

Prints 3rd column in a record split by /.

Details about awk command:

  • /\/elv\//: If an input line matches text /elv/
  • && split($7, a, /\//): and if 7th field can be split by /
  • && a[3] ~ /^[A-Z]/: and if 3rd field in split array starts with an uppercase letter
  • {print a[3]}: Print 3rd element of split array

Answered By – anubhava

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published