Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use space as field separator using awk?

Tags:

awk

Hereby I tried to output the column along with field separator.

But it fails in my case.

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 command which I tried as follows:-

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

Obtained Output:

NoName
AmitPhysics
RahulMaths
ShyamBiology
KedarEnglish
HariHistory

Expected output:

    Name Sub 
    Amit Physics 
    Rahul Maths 
    Shyam Biology 
    Kedar English 
    Hari History 
like image 973
PathFinder Avatar asked Aug 09 '17 11:08

PathFinder


1 Answers

awk '{print $3,$4;}' awk.txt > output.txt

don't need indicate field separator. field numbers starts from 1, so you need third and fourth fields. and for print OFS (output field separator) use comma.

or:

awk 'BEGIN{FS=OFS=" ";}{print $3,$4;}' awk.txt > output.txt
like image 155
tso Avatar answered Jan 01 '23 09:01

tso