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?
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.
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.
The default field delimiter or field separator (FS) is [ \t]+ , i.e. one or more space and tab characters.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With