ChatGPT解决这个技术问题 Extra ChatGPT

How can I use ":" as an AWK field separator?

Given the following command,

echo "1: " | awk '/1/ -F ":" {print $1}'

why does AWK output:

1:

?


P
Peter Mortensen

"-F" is a command line argument, not AWK syntax. Try:

 echo "1: " | awk -F  ":" '/1/ {print $1}'

Ignorant's question here: the /1/ part is to tell awk to only process rows (or records to be more precise) that contain the number 1 right?
@rantsh Awk syntax looks like (pattern){action}. If pattern (mostly a conditional statement) is true, action is executed. If pattern is not available, true is implied. Here the pattern is /1/ which states is regex 1 matched in the current record $0
As a sidenote, if you separator is comma, you'd want to add -v OFS="," parameter to awk, to keep it in the output
And how you can indicate this in a .awk script?
@JoanSerrano Look at the answer by Dennis below that does exactly that
D
Dennis Williamson

If you want to do it programatically, you can use the FS variable:

echo "1: " | awk 'BEGIN { FS=":" } /1/ { print $1 }'

Note that if you change it in the main loop rather than the BEGIN loop, it takes affect for the next line read in, since the current line has already been split.


f
fedorqui

You have multiple ways to set : as the separator:

awk -F: '{print $1}'

awk -v FS=: '{print $1}'

awk '{print $1}' FS=:

awk 'BEGIN{FS=":"} {print $1}'

All of them are equivalent and will return 1 given a sample input "1:2:3":

$ awk -F: '{print $1}' <<< "1:2:3"
1
$ awk -v FS=: '{print $1}' <<< "1:2:3"
1
$ awk '{print $1}' FS=: <<< "1:2:3"
1
$ awk 'BEGIN{FS=":"} {print $1}' <<< "1:2:3"
1

which is the preferred way? i assume the final example with the BEGIN statement would be the most correct (being consistent with the overall awk syntax).
@randomware all of them are fine. I tend to use BEGIN if I use a file to store the whole thing, while -F comes in handy with one-liners.
It must be said that there are subtle differences between the third case and all others. Example : awk 'BEGIN{print split("foo:bar",a)}' FS=":" file and awk 'BEGIN{FS=":"; print split("foo:bar",a)}' file
Thank you! I learn best from clear examples.
d
danben

-F is an argument to awk itself:

$echo "1: " | awk -F":" '/1/ {print $1}'
1

No need to quote colon.
P
Peter Mortensen

You can also use a regular expression as a field separator. The following will print "bar" by using a regular expression to set the number "10" as a separator.

echo "foo 10 bar" | awk -F'[0-9][0-9]' '{print $2}'

P
Peter Mortensen

Or you can use:

echo "1: " | awk  '/1/{print $1-":"}' 

This is a really funny equation.


what does /1/ mean?
Find a pattern. In this case "1"
Why is it a really funny equation?
i think this trick only works cuz the value before ":" is numeric. echo "ab1: " | awk '/1/{print $1-":"}' fails, printing a "0"
P
Peter Mortensen

There isn't any need to write this much. Just put your desired field separator with the -F option in the AWK command and the column number you want to print segregated as per your mentioned field separator.

echo "1: " | awk -F: '{print $1}'
1

echo "1#2" | awk -F# '{print $1}'
1

P
Peter Mortensen

AWK works as a text interpreter that goes linewise for the whole document and that goes fieldwise for each line. Thus $1, $2...$n are references to the fields of each line ($1 is the first field, $2 is the second field, and so on...).

You can define a field separator by using the "-F" switch under the command line or within two brackets with "FS=...".

Now consider the answer of Jürgen:

echo "1: " | awk -F  ":" '/1/ {print $1}'

Above the field, boundaries are set by ":" so we have two fields $1 which is "1" and $2 which is the empty space. After comes the regular expression "/1/" that instructs the filter to output the first field only when the interpreter stumbles upon a line containing such an expression (I mean 1).

The output of the "echo" command is one line that contains "1", so the filter will work...

When dealing with the following example:

echo "1: " | awk '/1/ -F ":" {print $1}'

The syntax is messy and the interpreter chose to ignore the part F ":" and switches to the default field splitter which is the empty space, thus outputting "1:" as the first field and there will be not a second field!

The answer of Jürgen contains the good syntax...


it's not awk ignored it - awk reads that as one regex's boolean outcome ( 1 / 0 ) , then numerically minus a variable named F, then string concat with a single colon (:), which means the total pattern yielded true because it's a non-empty string, thus $1 split by default space gets printed
R
RARE Kpop Manifesto
echo "1: " | "456:abc:515:xyz "

awk -F: NF=/1/

      1    |  456

UPDATE : realizing how verbose my previous answer was