Issue
Below is the file content and I'm trying to add a new line after the match - luxury_staty_villas:
but only once or at the first match.
{
'volume': {
'square': {
'name': 'Lotus_tank',
'Places': {
'id': 3,
'country': 'Australia',
},
'check-ins': {
three_star_reviews: "/home/properties_locality/marx",
three_star_reviews: "/home/properties_locality/marx",
paid_accomodation: "/home/properties_locality/marx",
luxury_staty_villas: "/home/properties_locality/marx,
token_payment: "cash_Deposit",
luxury_staty_villas: "/home/properties_locality/marx,
},
},
},
}
Came up with - sed '/luxury_staty_villas: .*/a\t\ \line_row_apartments: "/home/properties_locality/marx/platinum",'
but it doesn't work as expected.
Extra forward slash gets appended at the start of the line, then, the 1st two characters of the added lines gets chomped off(ne_row_apartments: instead of line_row_apartments:
) and it is matching all the occurences.
I'm on Windows 11 and making use of Git Bash
.
Current Output.
{
'volume': {
'square': {
'name': 'Lotus_tank',
'Places': {
'id': 3,
'country': 'Australia',
},
'check-ins': {
three_star_reviews: "/home/properties_locality/marx",
three_star_reviews: "/home/properties_locality/marx",
paid_accomodation: "/home/properties_locality/marx",
luxury_staty_villas: "/home/properties_locality/marx,
/ ne_row_apartments: "/home/properties_locality/marx/platinum",
token_payment: "cash_Deposit",
luxury_staty_villas: "/home/properties_locality/marx,
/ ne_row_apartments: "/home/properties_locality/marx/platinum",'
},
},
},
}
Expected output
{
'volume': {
'square': {
'name': 'Lotus_tank',
'Places': {
'id': 3,
'country': 'Australia',
},
'check-ins': {
three_star_reviews: "/home/properties_locality/marx",
three_star_reviews: "/home/properties_locality/marx",
paid_accomodation: "/home/properties_locality/marx",
luxury_staty_villas: "/home/properties_locality/marx,
line_row_apartments: "/home/properties_locality/marx/platinum",
token_payment: "cash_Deposit",
luxury_staty_villas: "/home/properties_locality/marx,
},
},
},
}
Solution
You could try this SED solution :
(file cmd.sed
)
/^[[:space:]]*luxury_staty_villas:/!b
a \
line_row_apartments: "/home/properties_locality/marx/platinum",
:loop
n
b loop
Then (based on the input sample provided in your post) : sed -f cmd.sed INPUTFILE
should produce :
{
'volume': {
'square': {
'name': 'Lotus_tank',
'Places': {
'id': 3,
'country': 'Australia',
},
'check-ins': {
three_star_reviews: "/home/properties_locality/marx",
three_star_reviews: "/home/properties_locality/marx",
paid_accomodation: "/home/properties_locality/marx",
luxury_staty_villas: "/home/properties_locality/marx,
line_row_apartments: "/home/properties_locality/marx/platinum",
token_payment: "cash_Deposit",
luxury_staty_villas: "/home/properties_locality/marx,
},
},
},
}
Explanation
/^[[:space:]]*luxury_staty_villas:/!b
: as long as this expression doesn't match, print current line and restart cycle. This is the first loop.a \ line_row_apartments: (...)
: we have found a match. Append desired line.:loop
: define looping labeln
: print current line, then replace pattern space with next lineb loop
: repeat
The second loop is needed (IMO) to consume remaining input lines once the first match has been found.
Tested under Linux Debian 11 with GNU SED in posix mode.
Hope that helps.
Answered By - Grobu Answer Checked By - Willingham (WPSolving Volunteer)