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.
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.
Comments