0
$ cat ansible/file | grep "Other users"
Other users

How can i add a new string on a new line after my grep as above?

$ cat ansible/file | grep "Other users"
Other users
NewString  # New line with new string appended to file 
John Kugelman
  • 349,597
  • 67
  • 533
  • 578
felli fel
  • 53
  • 5
  • use this command : cat ansible/file | grep "Other users" | | awk '{print $0 "\nNewString"}' – Ahmed Jul 08 '18 at 23:00

2 Answers2

1

sed is for doing s/old/new/ on individual lines, that is all. For anything else you should use awk:

awk '{print} /Other users/{print "New Line"}' file

The above will work robustly and efficiently with any awk in any shell on any UNIX box and will be trivial to enhance/maintain if/when your requirements change later.

Ed Morton
  • 188,023
  • 17
  • 78
  • 185
0

You can use sed to find the string you're looking for, and replace it with itself + the new line:

sed "s/Other users/Other users\nNew Line/g"

We're replacing "Other users" with "Other users\nNew String" (which is the string itself, and a new line appended to it with the content you want to insert).

Maroun
  • 94,125
  • 30
  • 188
  • 241
  • 3
    suggestions and observations: 1) use single quotes unless double are needed 2) in case there are other characters in the matching line, you'll need to cover that 3) `\n` is not portable 4) sed has `a` command for this use case – Sundeep Jul 08 '18 at 11:26