Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's imagine we have a file:

foo bar
foo1 bar1

I want to cut every second word in every line, so I want file to look like this:


I am trying to use cut -d ' ' -f -1 but what I get is:

foo foo1

How can i preserve line endings in the file?

share|improve this question
do you mean cut -d' ' -f1? – forcefsck Mar 27 '11 at 11:53
could you post how you're using that? cut leaves the line endings intact, i'm guessing you're using the output of cut in a way that's replacing them. – Mat Mar 27 '11 at 12:16
up vote 2 down vote accepted

file contents

john@caffe:~$ cat listing.txt
foo bar baz
foo1 bar1 baz1

getting the first column only

john@caffe:~$ cut -d' ' -f1 listing.txt

getting the first and third column, this time from stdin through a pipe

john@caffe:~$ cat listing.txt | cut -d' ' -f1,3
foo baz
foo1 baz1

EDIT: I think I know what you're doing wrong

john@caffe:~$ foo=$(cut -d " " -f -1 listing.txt)
john@caffe:~$ echo $foo
foo foo1
john@caffe:~$ echo "$foo"
share|improve this answer
You are right, I forgot to add quotes when tested it with echo. – Anton Barkovsky Mar 27 '11 at 12:52

Check the --output-delimiter option of cut, i.e. cut -f 1 -d ' ' --output-delimiter='\n' in your case

share|improve this answer
This has nothing to do with the output delimiter, if you bother reading the accepted answer and the comments... – don_crissti Jun 24 at 12:18

Your Answer


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.