Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

awk

Given the following command,

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

why does AWK output:

1: 

?

like image 932
user173446 Avatar asked Apr 09 '10 17:04

user173446


People also ask

What is awk default field separator?

The default value of the field separator FS is a string containing a single space, " " . If awk interpreted this value in the usual way, each space character would separate fields, so two spaces in a row would make an empty field between them.

What is awk '- F option?

The -f option only controls where the awk program is read from. If enabled, it means that the first filename is in fact the name of a file that contains the awk program. Otherwise, the first filename is the first file to start looking for patterns.

How do I change a field separator in Unix?

Shell script to change the delimiter of a file:Using the shell substitution command, all the commas are replaced with the colons. '${line/,/:}' will replace only the 1st match. The extra slash in '${line//,/:}' will replace all the matches. Note: This method will work in bash and ksh93 or higher, not in all flavors.


1 Answers

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

 echo "1: " | awk -F  ":" '/1/ {print $1}' 
like image 86
Jürgen Hötzel Avatar answered Sep 24 '22 08:09

Jürgen Hötzel