Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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:

foo
foo1

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
1  
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

1 Answer 1

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
foo
foo1

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"
foo
foo1
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

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.