0

Below code is my awk statement...I need to specify the headings for the each fields like name,age and phone_number in my output...How to give headings in the awk statement...How can i do that..

awk -v OFS='\t' '{ print $1, $2, $9 }' > "abc.txt"

Desired output:

 name              age          phone_number
  rat               26             5662200
  cat               25             562212
  • Ya ; } its a typo error...Forget about wc -l ...I need to add new headings to the above specified fields in the awk script...How can i do that – disha Feb 21 '19 at 14:21
2

In your inital question you asked about how you can add column headers. The answer to this is using the BEGIN condition:

awk -v OFS='\t' 'BEGIN { print "name", "age", "phone_number"} { print $1, $2, $9 }' > "abc.txt"

In your edit you asked about how you can visually align your columns. This possible by using column -t:

$ awk -v OFS='\t' 'BEGIN { print "name", "age", "phone_number"} { print $1, $2, $9 }'|column -t > "abc.txt"
  • Am getting the output like how i mentioned in my question ...Header is not aligned with the column..What should i do – disha Feb 21 '19 at 14:28
  • See my edit please. – finswimmer Feb 21 '19 at 14:34
  • How can i make heading as center align...see my edit pls – disha Feb 21 '19 at 14:39
  • 1
    You might like printf "%10s\t%10s...", $1, $2, ... if you can predict the maximum widths. – Jeff Schaller Feb 21 '19 at 15:30
  • @ Jeff..Can u be pls be more clear – disha Feb 21 '19 at 16:11
0
I have done this by below awk and sed command

awk 'OFS="\t"{print $1,$2,$9}' filename | sed '1i name\t age\t phonenumber\t'
  • Sir..Pls see my edits..I gave the above commands...am getting error like "Sed" : No such file or directory – disha Feb 21 '19 at 16:10
  • Before sed command you have used "<" kindly replace it with pipeline "|" – Praveen Kumar BS Feb 21 '19 at 16:13
  • Its not working sir – disha Feb 21 '19 at 16:48
  • @disha Your error is Sed no such file or dir. it is sed not Sed. – Prvt_Yadav Feb 21 '19 at 16:53
  • It is not working – disha Feb 21 '19 at 16:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.