英文:
Inserting Tab and spaces before the word while adding a new line using sed
问题
{
'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,
},
},
},
}
英文:
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,
},
},
},
}
答案1
得分: 1
你可以尝试这个SED解决方案:
(文件cmd.sed
)
/^[[:space:]]*luxury_staty_villas:/!b
a \
line_row_apartments: "/home/properties_locality/marx/platinum",
:loop
n
b loop
然后(基于你在帖子中提供的输入示例):sed -f cmd.sed INPUTFILE
应该产生:
{
'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,
},
},
},
}
解释
-
/^[[:space:]]*luxury_staty_villas:/!b
:只要此表达式不匹配,打印当前行并重新开始循环。这是第一个循环。 -
a \ line_row_apartments: (...)
:我们找到了匹配项。附加所需的行。 -
:loop
:定义循环标签 -
n
:打印当前行,然后替换模式空间为下一行 -
b loop
:重复
我认为需要第二个循环来在找到第一个匹配项后处理剩余的输入行。
在Linux Debian 11上使用GNU SED在posix模式下测试过。
希望有所帮助。
英文:
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 \
: we have found a match. Append desired line.
line_row_apartments: (...) -
:loop
: define looping label -
n
: print current line, then replace pattern space with next line -
b 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论