0

I have a data file, named words, which is made of rows and 3 columns. First column is a german word, second is its translation and third some metadata like verb, noun, etc...

$ cat words
offen open verb
Fenster window noun
Regd shelf noun

To grab all the nouns I could use grep,

$ cat words | grep noun
Fenster window noun
Regd shelf noun

I like that output because each record in its own row. The problem comes when I am trying to store that output in a variable,

$ nouns=$(cat words | grep noun)

The output now is one line where those two records merged into one,

$ echo $nouns
Fenster window noun Regd shelf noun

How can I assign a multiline output from grep into a variable?

Themelis
  • 4,048
  • 2
  • 21
  • 45
  • The problem is how you __print__ the variable. Assignment is fine. – KamilCuk Jun 05 '21 at 18:27
  • How should I print it @KamilCuk? – Themelis Jun 05 '21 at 18:28
  • If you don't want the new lines to be printed then there's the `-n` option for `echo`. So shouldn't by default print the new lines? I mean I could use `printf` but I would still want to know the reason. – Themelis Jun 05 '21 at 18:30
  • 3
    @KamilCuk: did you reopen the Q? - I was trying to find the _exact_ dupe - https://stackoverflow.com/q/29378566/5291015. Could you close it ? – Inian Jun 05 '21 at 18:32
  • 1
    The variable needs to be enclosed in double quotes, `echo "$nouns"` (https://stackoverflow.com/a/51665500/9164071) – Themelis Jun 05 '21 at 18:37

0 Answers0