How to use space as a field separator using awk?

Here I tried to display the column along with the field separator.

But in my case, it fails.

awk.txt(Input file)

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

awk, which I tried as follows: -

 awk -F ' ' '{print $2 $3;}' awk.txt > output.txt

The resulting conclusion:

NoName
AmitPhysics
RahulMaths
ShyamBiology
KedarEnglish
HariHistory

Expected Result:

    Name Sub 
    Amit Physics 
    Rahul Maths 
    Shyam Biology 
    Kedar English 
    Hari History 
+4
source share
1 answer
awk '{print $3,$4;}' awk.txt > output.txt

no field separator required. field numbers start at 1, so you need the third and fourth fields. and for printing OFS (output field separator) use a comma.

or

awk 'BEGIN{FS=OFS=" ";}{print $3,$4;}' awk.txt > output.txt
+2
source

Source: https://habr.com/ru/post/1683340/


All Articles