4

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

3 Answers 3

7

Just do this

head -n1 test.txt | tr , '\n'
Sign up to request clarification or add additional context in comments.

Comments

1

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'

Comments

1

Try this:

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

3 Comments

awesome!!! it worked. sorry, but is there a way to not include echo "${arr[0,1,2,3,4]} coz the header is really long abt 58 fields.
I edited script to print more fields, hope this helps :)
it worked :) thank you so much. cant vote up coz i dont have enough reputation but u saved my day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.