-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy path49.sh
More file actions
49 lines (45 loc) · 1.14 KB
/
Copy path49.sh
File metadata and controls
49 lines (45 loc) · 1.14 KB
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
45
46
47
48
49
## shell脚本一天一练系列 -- Day49
<< 'COMMENT'
想办法把文本里面每三行内容合并到一行 例如:1.txt内容
1
2
3
4
5
6
7
处理后应该是
1 2 3
4 5 6
7
其实,该需求用sed处理非常简单: sed 'N;N;s/\n/ /g' filename
但这里,就是要用shell脚本实现,锻炼大家的思维能力
COMMENT
### ------- 分割线,以下为脚本正文 -------
#!/bin/bash
# author: aming (微信: lishiming2009)
# version: v1
# date: 2023-11-21
## 思路:
## 设定计数器n,从1开始,也就是从文本第1行开始
## 遍历文本所有行,当行数不被3整除时,打印该行文本,后面不带换行符
## 当行数正好被3整除时,打印该行文本,后面带上换行符
## 遍历完一行后,n的值加1
## n为计数器,初始值为1
n=1
## 遍历文本内容,其中$1就是文件名
cat $1 |while read line
do
## n1为n/3的余数
n1=$[$n%3]
## 当n/3余数为0时,打印文本后带换行符
if [ $n1 -eq 0 ]
then
echo "$line"
else
## 当n/3余数不为0时,打印文本后不带换行符而是空格
echo -n "$line "
fi
## n的值加1
n=$[$n+1]
done