arjun D.
arjun D.

Reputation: 81

how to get the header of a csv file and print it as a column using linux?

I am trying to get the header or the first line of a csv file and print it as a column.

file example: test.txt name^lastname^address^zipcode^phonenumber

expected result:

name
lastname
address
zipcode
phonenumber

Upvotes: 4

Views: 12289

Answers (3)

Siva Praveen
Siva Praveen

Reputation: 2333

Just do this

head -n1 test.txt | tr , '\n'

Upvotes: 7

Duc Vo
Duc Vo

Reputation: 61

Try this:

for i in `cat test.txt`; do
IFS='^'
arr=($i)
for col in "${arr[@]}"; do
    echo "$col"
done
break;
done

Upvotes: 1

pacholik
pacholik

Reputation: 8972

head prints n lines of a file, tr replaces all occurrences of a first char with a second char

head -n1 test.txt | tr '^' '\n'

Upvotes: 1

Related Questions