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?

5
  • The problem is how you print the variable. Assignment is fine.
    – KamilCuk
    Commented Jun 5, 2021 at 18:27
  • How should I print it @KamilCuk?
    – Themelis
    Commented Jun 5, 2021 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
    Commented Jun 5, 2021 at 18:30
  • 3
    @KamilCuk: did you reopen the Q? - I was trying to find the exact dupe - stackoverflow.com/q/29378566/5291015. Could you close it ?
    – Inian
    Commented Jun 5, 2021 at 18:32
  • 1
    The variable needs to be enclosed in double quotes, echo "$nouns" (stackoverflow.com/a/51665500/9164071)
    – Themelis
    Commented Jun 5, 2021 at 18:37

0

Browse other questions tagged or ask your own question.