Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change AWK field separator on the fly

Tags:

linux

bash

awk

I'd like to use AWK to take the following spread sheet where the first name and last name are in one column:

Peter Griffin, 31 Spooner St, Quahog
Homer Simpson, 732 Evergreen Terr, Springfield
Fred Flintstone, 301 Cobblestone Way, Bedrock

and output to a new spreadsheet where the first name and last name have their own columns:

Peter, Griffin, 31 Spooner St, Quahog
Homer, Simpson, 732 Evergreen Terr, Springfield
Fred, Flintstone, 301 Cobblestone Way, Bedrock

I've tried changing field separators on the fly doing something like:

awk '{print $1 "," $2} {FS=","} {print $3} {FS=" "}' spreadsheet.csv

but it doesn't seem to work that way, and I get a jumbled mess. Is this possible using AWK?

like image 354
Ryan R Avatar asked Aug 30 '16 15:08

Ryan R


People also ask

How do you change the awk separator?

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.

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.

What is awk default field separator?

The default field delimiter or field separator (FS) is [ \t]+ , i.e. one or more space and tab characters.

What is awk '{ print $2 }'?

awk '{ print $2; }' prints the second field of each line. This field happens to be the process ID from the ps aux output. xargs kill -${2:-'TERM'} takes the process IDs from the selected sidekiq processes and feeds them as arguments to a kill command.


2 Answers

Just add a comma whenever a space is found in the first ,-based field:

awk 'BEGIN {FS=OFS=","} {sub(/ /, ", ", $1)}1' file
#                             ^    ^^
#               find a space...    ... replace it with , plus space

With your file:

$ awk 'BEGIN {FS=OFS=","} {sub(/ /, ", ", $1)}1' file
Peter, Griffin, 31 Spooner St, Quahog
Homer, Simpson, 732 Evergreen Terr, Springfield
Fred, Flintstone, 301 Cobblestone Way, Bedrock

This uses the function sub() to perform the replacement in the first field.

like image 144
fedorqui 'SO stop harming' Avatar answered Sep 18 '22 08:09

fedorqui 'SO stop harming'


Replace the first space with a comma-space:

$ sed 's/ /, /' file.csv
Peter, Griffin, 31 Spooner St, Quahog
Homer, Simpson, 732 Evergreen Terr, Springfield
Fred, Flintstone, 301 Cobblestone Way, Bedrock

Here, s/ /, / is a substitute command. It replaces the first found with ,.

To change the file in place, use the -i option:

sed -i.bak 's/ /, /' file.csv
like image 30
John1024 Avatar answered Sep 19 '22 08:09

John1024