我有一个看起来像这样的文本文件-
Random text
Some more random text ...
TEXT_CATEGORY_A(
SOME_INFO, A,
"Some random text.",
"Other info.",
)
TEXT_CATEGORY_B(
SOME_INFO, B,
"Some random text.",
"Other info.",
)
TEXT_CATEGORY_C(
SOME_INFO, C,
"Some random text.",
"Other info.",
)
等... 我想从每个TEXT_CATEGORY容器的最后一句中删除逗号,即从-
"Other info.",
因此文件的最终格式应如下所示:
Random text
Some more random text ...
TEXT_CATEGORY_A(
SOME_INFO, A,
"Some random text.",
"Other info."
)
TEXT_CATEGORY_B(
SOME_INFO, B,
"Some random text.",
"Other info."
)
TEXT_CATEGORY_C(
SOME_INFO, C,
"Some random text.",
"Other info."
)
如果我能以某种方式发现下一行仅包含)
字符,那么我可以解决此问题。
我无法使用sed解决此问题,因为它逐行读取文件。有什么办法可以让我了解下一行的内容,还是有其他解决办法?
答案 0 :(得分:2)
使用sed
命令灵活:
sed -E '/,$/N; s/([^,]+),\s+\)$/\1\n)/' file
/,$/
-以,
结尾的匹配行N
-将下一行捕获到缓冲区空间中\1
-捕获的第一个组(指向([^,]+)
)输出:
Random text
Some more random text ...
TEXT_CATEGORY_A(
SOME_INFO, A,
"Some random text.",
"Other info."
)
TEXT_CATEGORY_B(
SOME_INFO, B,
"Some random text.",
"Other info."
)
TEXT_CATEGORY_C(
SOME_INFO, C,
"Some random text.",
"Other info."
)
答案 1 :(得分:1)
第一个解决方案: 。能否请您尝试以下操作。在此处使用<MaterialTable
columns = {columnsSetup}
options={{
showTitle: false,
search: false,
filtering: true
}}
onRowClick={(console.log(rowData))} <----- issue right here
data={query =>
new Promise((resolve, reject) => {
let url = GET_ORDERS_URL
url += 'qtt=' + query.pageSize
url += '&page=' + query.page
console.log(url);
fetch(url)
.then(response => response.json())
.then(result => {
resolve({
data: result.data,
page: result.page,
totalCount: result.totalElems
});
})
})
}
/>
+ tac
。这应该更快。
awk
说明: 添加上述代码的说明。
tac Input_file | awk '{sub(/,/,"")} 1' | tac
第二个解决方案: 使用GNU tac Input_file ##Using tac to print Input_file inn reverse order.
awk '{ ##Using tac command output to awk program from here.
sub(/,/,"") ##Using sub to substitute very first occurrence of comma with NULL here.
} ##Closing BLOCK here.
1 ##Mentioning 1 will print edited/non-edited line here.
' | tac ##Passing previous awk command output to tac command now and making it in its normal form.
。
awk
说明: 添加上述代码的说明。
awk -v RS="" '
match($0,/.*,/){
print substr($0,RSTART,RLENGTH-1) substr($0,RSTART+RLENGTH)
}
' Input_file
答案 2 :(得分:0)
这可以使用sed如下解决-
sed -E '/,$/N; s/",$/"/' file
这是对@RomanPerekhrest提供的解决方案的略微修改。