Text-Processing
在某行之後將長行拆分為最大長度的單獨行
我有帶有長 base64 字元串的大文件(第 112 行之後)。前 112 行中有一些超過 80 個字元,我想保持這樣。我只想將第 112 行之後的所有行拆分為最多 80 個字元。
在此範例中,我只想將第 5 行之後的所有行拆分為最多 80 個字元:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. rcitation ullamco laboris nisi ut aliquip e rcitation ullamco laboris nisi ut aliquip e TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2VkIGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlxWEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0ZSBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1bSBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjdXBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vbGxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg0KcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZQ0KcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZQ==
預期輸出:
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum. rcitation ullamco laboris nisi ut aliquip e rcitation ullamco laboris nisi ut aliquip e TG9yZW0gaXBzdW0gZG9sb3Igc2l0IGFtZXQsIGNvbnNlY3RldHVyIGFkaXBpc2NpbmcgZWxpdCwgc2Vk IGRvIGVpdXNtb2QgdGVtcG9yIGluY2lkaWR1bnQgdXQgbGFib3JlIGV0IGRvbG9yZSBtYWduYSBhbGlx WEuIFV0IGVuaW0gYWQgbWluaW0gdmVuaWFtLCBxdWlzIG5vc3RydWQgZXhlcmNpdGF0aW9uIHVsbGFtY 28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZXggZWEgY29tbW9kbyBjb25zZXF1YXQuIER1aXMgYXV0Z SBpcnVyZSBkb2xvciBpbiByZXByZWhlbmRlcml0IGluIHZvbHVwdGF0ZSB2ZWxpdCBlc3NlIGNpbGx1b SBkb2xvcmUgZXUgZnVnaWF0IG51bGxhIHBhcmlhdHVyLiBFeGNlcHRldXIgc2ludCBvY2NhZWNhdCBjd XBpZGF0YXQgbm9uIHByb2lkZW50LCBzdW50IGluIGN1bHBhIHF1aSBvZmZpY2lhIGRlc2VydW50IG1vb GxpdCBhbmltIGlkIGVzdCBsYWJvcnVtLg0KcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0I GFsaXF1aXAgZQ0KcmNpdGF0aW9uIHVsbGFtY28gbGFib3JpcyBuaXNpIHV0IGFsaXF1aXAgZQ==
我知道如何使用 sed 忽略行,但不知道如何設置最大字元限制並使用拆分行。當我在網際網路上搜尋時,人們推薦折疊
fold -w80 input.txt
但是折疊我不能忽略線條?知道如何用 fold 或 sed 做到這一點嗎?也許是 awk?
使用 sed 你可以做,
-i
如果你需要應用更改,添加一個input.txt
(如果你的文件包含 80 個字元長度的行,這將在它之後添加一個空白行):sed -E 's/(.{80})/\1\n/g' input.txt
在你的情況下,如果你只需要在第 112 行之後:
sed -E '113,$s/(.{80})/\1\n/g' input.txt
113,$
從第 113 行到文件末尾s/PATTERN/REPLACEMENT/g``PATTERN
用_REPLACEMENT
(.{80})
一組80個字元\1
第一個擷取的組使用 perl :
perl -pe 's/(.{80})(?!$)/\1\n/g if $. > 112' input.txt