-
Notifications
You must be signed in to change notification settings - Fork 5
/
merge_line
executable file
·44 lines (39 loc) · 963 Bytes
/
merge_line
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/bin/bash
# read from pipeline, and merge per n lines to one line, and output to stdout
# usage: merge_line # merge per 2 lines to one line, and separate with " "
# merge_line "=" # merge per 2 lines to one line, and separate with "="
# merge_line 3 # merge per 3 lines to one line, and separate with " "
# merge_line 4 "##" # merge per 4 lines to one line, and separate with "##"
# created by zhouwei on 2019-08-28
# default value
n=2
separator=" "
count=$#
if [[ $count -eq 0 ]]; then
n=2
separator=" "
elif [[ $count -eq 1 ]]; then
if [[ "$1" =~ ^[0-9]+$ ]]; then
n="$1"
else
separator="$1"
fi
else
n="$1"
separator="$2"
fi
i=0
awk '{print $0}' | while read line
do
if [[ $i -gt 0 ]]; then
if (( i%n == 0 )); then
echo ""
else
echo -n "$separator"
fi
fi
echo -n "$line"
((i++))
done
echo ""
exit 0