How to change separator in a new file using awk

Sumin Kim

I have a file marks.txt that looks like this separated by spaces.

1)  Amit    Physics  80
2)  Rahul   Maths    90
3)  Shyam   Biology  87
4)  Kedar   English  85
5)  Hari    History  89

I want to convert the separator into ",".
The desired output is this

1),Amit,Physics,80
2),Rahul,Maths,90
3),Shyam,Biology,87
4),Kedar,English,85
5),Hari,History,89

I found out this can be done by

awk '$1=$1' FS=" " OFS="," marks.txt

However I don't understand what '$1=$1' means.
Also, when I type

awk '{print}' FS=" " OFS=":" marks.txt>test

or

awk 'BEGIN{OFS=","}1' marks.txt >test

nothing changes. Why? Thank you very much.

RavinderSingh13

You could verify this by yourself by printing lines before and after for testing for it.

awk '{print "BEFORE line-->" $0;$1=$1;print "AFTER line-->" $0}' FS=" " OFS=","  Input_file

Output will be as follows.

BEFORE line-->1)  Amit    Physics  80
AFTER line-->1),Amit,Physics,80
BEFORE line-->2)  Rahul   Maths    90
AFTER line-->2),Rahul,Maths,90
BEFORE line-->3)  Shyam   Biology  87
AFTER line-->3),Shyam,Biology,87
BEFORE line-->4)  Kedar   English  85
AFTER line-->4),Kedar,English,85
BEFORE line-->5)  Hari    History  89
AFTER line-->5),Hari,History,89

By this we could see that $1=$1 re-evaluate value of it and changes whole line's value accordingly.

More explanation from comments: thing is if you re-initiate $1 or $2 or any field whole line be re-evaluated or re-read and it will apply OFS new value then. We write it $1 we could write it $2 eg--> awk '$2=$2' FS=" " OFS="," Input_file

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to skip separator inclusion in file using awk command

How to use space as field separator using awk?

awk: how to append lines in a file until finding a file separator symbol?

How to change position of data columns using regex in a CSV file using comma as separator?

Change AWK field separator on the fly

How to print filenames to a new file with a specific format using awk?

How to change a file in-place using awk? (as with "sed -i")

awk write in file with column separator

How to split string with awk using '\t|' sting as a separator?

How to delete and replace field separator from column using awk?

Row separator after using AWK

How to change values in one file using another file for reference using awk?

How to change the separator on the clock?

How to use ^#$ as record separator in awk?

How to use AWK record separator?

AWK doesn't update record with new separator

awk: change field separator keeping first column as is

AWK change field separator multiple times

change and manipulate lines in a file using awk

How to parse a file using AWK

How to set a multicharacter record separator RS in GNU awk so it encompasses the new lines?

How to override `File::SEPARATOR`

AWK not printing output file separator OFS

How to insert new line separator on regular (every 13th) intervals of delimiter in text file which has no new line separator

How to copy a certain amout of lines to a new txt file in bash using awk

Using multiple character string as separator in awk

awk field separator using ":" and ignore spaces within ":"

manipulate input file and create a new file using awk

How to change the category separator in EventStore?