简体   繁体   中英

Bash script - how to insert value of variable as new line, 2 line after matching a pattern in text file

I want to add value of variable as new line, 2 line after matching a pattern. Below is the example.

Below is contents of line before adding value of variable as new line,2 line after matching pattern "FILTER datalist"

FILTER datalist

{

"DEBAU00_NFS_fmabackup_BBTFLRBAU01"

}

FILTER copylist

{

}

Below is the desired result , in case value of variable is Laptop.

FILTER datalist

{

"DEBAU00_NFS_fmabackup_BBTFLRBAU01"

Laptop

}

I have tried below.

sed '/^FILTER datalist/{N;N;s/$/\Laptop/}' file_name

Use sed with branching to achieve your objective.

$ cat casefile48486676
FILTER datalist

{

"DEBAU00_NFS_fmabackup_BBTFLRBAU01"

}

FILTER copylist

{

}
$ variable="Laptop\n\n" #bash variable
$ sed -E "/FILTER datalist/{:l1;/^}$/! {n;bl1};s/(^.*$)/$variable\1/}" casefile48486676

Output

FILTER datalist

{

"DEBAU00_NFS_fmabackup_BBTFLRBAU01"

Laptop

}

FILTER copylist

{

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM