0
$ cat hello.txt     
shreenivasa hi hello this is test mail                      
shreenivasa how are you man

if i run in terminal

$ cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-

its giving following output

hi hello this is test mail      
how are you man

but if i write it as script in hello.sh like below

#!/bin/bash
hi=`cat hello.txt | grep shreenivasa | cut -d ' ' -f 2-`
echo $hi

output for ./hello.sh is

hi hello this is test mail how are you man.

I want to print "how are you man" in next line like

hi hello this is test mail     
how are you man

tried $hi\n didn't work

  • 3
    please review [how do I format my posts](https://stackoverflow.com/help/formatting) and then (re)format the question – markp-fuso Dec 08 '22 at 17:37
  • As a start, since you are using `grep` you do not need to use `cat`. Grep reads files. (See https://en.wikipedia.org/wiki/Cat_(Unix)#Useless_use_of_cat) – j_b Dec 08 '22 at 17:41
  • 1
    try wrapping the variable reference in double quotes: `echo "$hi"`; see [difference between single and double quotes in bash](https://stackoverflow.com/q/6697753) for more details – markp-fuso Dec 08 '22 at 17:41
  • Please c.f. [this page](https://porkmail.org/era/unix/award) on things like avoiding `cat` as an extra process when `grep` would have opened the file and been more efficient without it. Generally an antipattern. It's a small bit if data, so not a system-killing error, but try not to get in the habit. Try just `grep -oP '(?<=shreenivasa ).*' hello.txt` or `sed -nE '/^shreenivasa /{s/^shreenivasa //; p;}' hello.txt`, and as a habit, always double-quote your variables when you use them unless there's a reason *not* to do so.... – Paul Hodges Dec 08 '22 at 18:13

0 Answers0