subakhan k subakhan k - 2 months ago 6
Linux Question

Need to add newline after performing some action in same file in linux

I want to give new line after task performed at same file .

Input :

sample.txt


Hi
Hello
I am
back

i am After new line
you unable to get me.

..Sample_end.....


I grep the word starts from Hi and end with space and I also want print the grep words in the same file precedent with newline.

Output:
sample.txt


Hi
Hello
I am
back

i am After new line
you unable to get me


Hi
Hello
I am
back

Answer

Like this?

$ awk 'BEGIN {print} /Hi/,/^$/' file >> file
$ cat file
Hi
Hello
I am
back

i am After new line
you unable to get me.

Hi
Hello
I am
back
[THIS IS AN EMPTY LINE]

It first appends a newline to file (BEGIN {print}) then stuff between Hi and an empty record (^$). Apparently grep wouldn't append to source file just by using grep ... file >> file.

Comments