josh
josh

Reputation: 14463

awk replace string with newline

I have never used awk or sed. I am trying to replace

aaa
{

with

aaa
{
    bbb

I tried different solutions using sed/awk, but couldn't figure it out.

awk '{gsub("aaa\n{", "aaa\n{\tbbb")}1' file.txt

Could you please help me on how to do it.

Upvotes: 0

Views: 1118

Answers (3)

Cron
Cron

Reputation: 71

awk with getline

awk '/aaa/ {print;getline} /{/ { print ;print "\tbbb";next}1 ' file

Upvotes: 0

potong
potong

Reputation: 58578

This might work for you (GNU sed):

sed '/^aaa$/!b;n;/^{$/a\\tbbb' file

Upvotes: 0

Arjun Mathew Dan
Arjun Mathew Dan

Reputation: 5318

With awk:

awk '{print} /^aaa$/{i=NR} /^{$/ && NR==i+1 {print "\tbbb"}' File

Output:

aaa
{
        bbb
sdjdhsjdhdsd
ds
ddsdsdsd
aaa
{
        bbb

Upvotes: 1

Related Questions