1

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?

2
  • do you mean cut -d' ' -f1? Commented Mar 27, 2011 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. Commented Mar 27, 2011 at 12:16

2 Answers 2

4

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
1
  • 1
    You are right, I forgot to add quotes when tested it with echo. Commented Mar 27, 2011 at 12:52
0

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

1
  • 2
    This has nothing to do with the output delimiter, if you bother reading the accepted answer and the comments... Commented Jun 24, 2016 at 12:18

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.