Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I have a text file for which each line looks like:

number, description1, description2,...,

How can I retrieve in a file the numbers only?

share|improve this question

2 Answers 2

up vote 0 down vote accepted

Use csvtool:

csvtool col 1 my-file.csv

See csvtool --help for details.

share|improve this answer

If you want to do it with standard command-line utilities, either cut or awk would work

cut -d, -f1 yourfile

awk -F, '{print $1}' yourfile

You could also use sed to substitute everything after the first comma-delimited field with nothing

sed 's/,.*//' yourfile

The same substitution syntax would work in vim (since you tagged the question with that) i.e. after entering command mode with Esc

:1,$s/,.*//

share|improve this answer
    
Just for the hell of it, another option is to grep the first non-, characters with grep -o '^[^,]*' yourfile, but I agree that my first instinct is cut (then awk). –  Sparhawk Oct 29 '13 at 5:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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