2

i have a CSV file i want to get the columns name for each column

sample :

image

how can i do that with awk or sed or grep ??

  • 1
    Please add your desired output for that sample input to your question. – Cyrus Jun 24 '18 at 7:51
10
head -n 1 file.csv

or

sed 1q file.csv

or

grep -m 1 '' file.csv

or

awk 'NR==1 {print; exit}' file.csv
  • As print is the default action you can shorten the awk command to awk NR==1 file.csv. – dessert Jun 24 '18 at 9:17
  • @dessert: Thank you. I'm aware of that. This way awk reads only the first line of the file. – Cyrus Jun 24 '18 at 9:20
  • I don’t understand – awk 'NR==1 {print; exit}' does the exact same as awk NR==1. – dessert Jun 24 '18 at 10:30
  • @dessert when awk NR==1, still awk will read your whole file's lines where those are not necessary to read. Also that can be only awk '{print; exit}' – αғsнιη Jun 24 '18 at 10:48
  • 1
    or awk '1;{exit}' – glenn jackman Jun 24 '18 at 11:09

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.