$ 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
$ 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
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.
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).