ewok
ewok

Reputation: 21443

Bash: Split a single line of output with spaces into multiple lines of one word each

I have a command which outputs a randomly ordered set on words. My goal is, ultimately, to get a sorted list. I'm trying to find a way to do this that can be done with a single command. I know that if the output is on multiple lines, it can be sorted by piping it to sort, such as

$ echo "foo
> bar
> baz" | sort
bar
baz
foo

So is there a simple command that I can use to split a string like "foo bar baz" into separate lines, so I can accomplish my goal with echo "foo bar baz" | magic_string_splitter | sort

edit: bonus, the list actually contains some extra output at the end that would be great to ignore. so for instance, the output of my command might actually be like this

$ print_words
foo bar baz are your words

But I'd still like the final result to just be

bar
baz
foo

We can totally strip off and ignore the "are your words" at the end.

Upvotes: 4

Views: 5674

Answers (3)

user1934428
user1934428

Reputation: 22225

echo "foo bar   baz more" |tr -s ' ' | cut -d ' ' -f 1-3 | fmt -w 1

The cut selectes the first three fields and the fmt arranges them into separate lines.

Upvotes: 1

Cyrus
Cyrus

Reputation: 88553

With GNU awk:

echo 'foo bar baz are your words' | awk '{NF-=3; OFS="\n"; $1=$1}1' | sort

NF-=3: Remove last 3 columns from current row.

OFS="\n": set output field separator to newline (or use OFS=ORS).

$1=$1: Forces awk to rebuild current row with new OFS (now newline).

1: awk evaluates 1 as true expression and outputs the current line.

Output:

bar
baz
foo

See: 8 Powerful Awk Built-in Variables – FS, OFS, RS, ORS, NR, NF, FILENAME, FNR

Upvotes: 5

Benjamin W.
Benjamin W.

Reputation: 52102

You can use tr and (GNU) head:

$ echo "foo bar baz are your words" | tr -s '[:blank:]' '\n' | head -n -3 | sort
bar
baz
foo
  • tr -s '[:blank:]' '\n' squeezes any runs of blanks and replaces them with one linebreak
  • head -n -3 drops the last three lines

Upvotes: 5

Related Questions