如果你對其他工具開放,jq可能可以做你所需要的。
假設文件data
包含
When in the course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.
We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed.
命令
$ jq -MR '.' data
產生串序列,每輸入行一個:
"When in the course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation."
"We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed."
命令
$ jq -MR -n '[inputs]' data
將收集行到一個數組:
[
"When in the course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.",
"We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed."
]
一旦你有一個JSON對象,可以很容易地添加更多的處理。例如此過濾器
$ jq -MR -n '[inputs] | map("\(.[:30])... \(length) characters")' data
總結了每個行:
[
"When in the course of human ev... 404 characters",
"We hold these truths to be sel... 337 characters"
]
和該命令
$ jq -MR -n 'reduce inputs as $i ({}; .["\(.|length)"]=$i)' data
收集線成一個對象
{
"0": "When in the course of human events it becomes necessary for one people to dissolve the political bands which have connected them with another and to assume among the powers of the earth, the separate and equal station to which the Laws of Nature and of Nature's God entitle them, a decent respect to the opinions of mankind requires that they should declare the causes which impel them to the separation.",
"1": "We hold these truths to be self-evident, that all men are created equal, that they are endowed by their Creator with certain unalienable Rights, that among these are Life, Liberty and the pursuit of Happiness. That to secure these rights, Governments are instituted among Men, deriving their just powers from the consent of the governed."
}
有一個在線版本在https://jqplay.org/以及。
哪種語言? – jeremyjjbrown
你如何識別一個段落? –
@jeremyjjbrown @jeremyjjbrown這是在去,我不想指定,因爲我是任何將完成這項工作:) – collinglass