瀏覽代碼

Add nljoin() for joining lines (reverse of nlfor())

Alois Mahdal 7 年之前
父節點
當前提交
4c1e2e0d9f
共有 1 個檔案被更改,包括 18 行新增0 行删除
  1. 18
    0
      dotfiles/bash/main.bashrc

+ 18
- 0
dotfiles/bash/main.bashrc 查看文件

@@ -220,6 +220,24 @@ nlfor() {
220 220
       | fixnl
221 221
 }
222 222
 
223
+nljoin() {
224
+    #
225
+    # Join lines with $1
226
+    #
227
+    # Shorthand for commonly used tr syntax.  Example:
228
+    #
229
+    #     $ { echo foo; echo bar; } | nljoin :
230
+    #
231
+    # is almost like `tr '\n' :` except that it is a bit easier
232
+    # to type and fixes the annoying problem with extraneous
233
+    # newline/colon at the end of final string.
234
+    #
235
+    local char=$1
236
+    tr '\n' "$char" \
237
+      | sed "s/$char*$//" \
238
+      | fixnl -c
239
+}
240
+
223 241
 xop() {
224 242
     #
225 243
     # Common clipboard operations with fixnl() on top