![bash +如何根据现有路径将路径追加到行尾](https://linux22.com/image/124684/bash%20%2B%E5%A6%82%E4%BD%95%E6%A0%B9%E6%8D%AE%E7%8E%B0%E6%9C%89%E8%B7%AF%E5%BE%84%E5%B0%86%E8%B7%AF%E5%BE%84%E8%BF%BD%E5%8A%A0%E5%88%B0%E8%A1%8C%E5%B0%BE.png)
以下配置文件(示例1) 未按应有的方式配置。
文件中的每一行应包含/grid/sdX
( a
to z
),如中所述示例2。
我需要找到一种方法来为此任务编写 bash 脚本。如何/grid/sdX
在行尾追加缺失的内容?
示例1
more dfs_data_dir_mount.hist
/grid/sdk/hadoop/hdfs/data,/
/grid/sdi/hadoop/hdfs/data,/
/grid/sdh/hadoop/hdfs/data,/
/grid/sdc/hadoop/hdfs/data,/grid/sdc
/grid/sdj/hadoop/hdfs/data,/
/grid/sde/hadoop/hdfs/data,/grid/sde
/grid/sdd/hadoop/hdfs/data,/grid/sdd
/grid/sdb/hadoop/hdfs/data,/grid/sdb
/grid/sdf/hadoop/hdfs/data,/grid/sdf
/grid/sdg/hadoop/hdfs/data,/
预期结果(示例 2)
/grid/sdk/hadoop/hdfs/data,/grid/sdk
/grid/sdi/hadoop/hdfs/data,/grid/sdi
/grid/sdh/hadoop/hdfs/data,/grid/sdh
/grid/sdc/hadoop/hdfs/data,/grid/sdc
/grid/sdj/hadoop/hdfs/data,/grid/sdj
/grid/sde/hadoop/hdfs/data,/grid/sde
/grid/sdd/hadoop/hdfs/data,/grid/sdd
/grid/sdb/hadoop/hdfs/data,/grid/sdb
/grid/sdf/hadoop/hdfs/data,/grid/sdf
/grid/sdg/hadoop/hdfs/data,/grid/sdg
答案1
sed
解决方案:
sed -Ei 's~^(/[^/]+/[^/]+)(.*,)/$~\1\2\1~' dfs_data_dir_mount.hist
~
- 视为sed
子命令分隔符[^/]+
- 匹配除斜杠之外的一个或多个字符/
^
$
- 分别是行的起点和终点
答案2
我可以使用以下 awk 命令来完成此操作:
awk -F'/' '{OFS="/";}{print $1,$2,$3,$4,$5,$6,"grid",$3}' input
awk
-F'/'
- 分隔输入/
{OFS="/";}
- 分隔输出/
{print $1,$2,$3,$4,$5,$6,"grid",$3}'
- 打印/grid/sd*/hadoop/hdfs/data,/
(从输入中获取的字段)和grid/sd*
(手动输入grid
并再次添加字段 3)
答案3
awk
:
awk -F/ -v OFS='/' '!$NF {$0=$0 $2 OFS $3}; 1'
-F/ -v OFS='/'
将输入和输出字段分隔符设置为/
!$NF {$0=$0 $2 OFS $3}; 1
如果最后一个字段为空,我们将以所需的格式重新构建记录。1
是真实的awk
并且是打印记录。
例子:
% cat file.txt
/grid/sdk/hadoop/hdfs/data,/
/grid/sdi/hadoop/hdfs/data,/
/grid/sdh/hadoop/hdfs/data,/
/grid/sdc/hadoop/hdfs/data,/grid/sdc
/grid/sdj/hadoop/hdfs/data,/
/grid/sde/hadoop/hdfs/data,/grid/sde
/grid/sdd/hadoop/hdfs/data,/grid/sdd
/grid/sdb/hadoop/hdfs/data,/grid/sdb
/grid/sdf/hadoop/hdfs/data,/grid/sdf
/grid/sdg/hadoop/hdfs/data,/
% awk -F/ -v OFS='/' '!$NF {$0=$0 $2 OFS $3}; 1' file.txt
/grid/sdk/hadoop/hdfs/data,/grid/sdk
/grid/sdi/hadoop/hdfs/data,/grid/sdi
/grid/sdh/hadoop/hdfs/data,/grid/sdh
/grid/sdc/hadoop/hdfs/data,/grid/sdc
/grid/sdj/hadoop/hdfs/data,/grid/sdj
/grid/sde/hadoop/hdfs/data,/grid/sde
/grid/sdd/hadoop/hdfs/data,/grid/sdd
/grid/sdb/hadoop/hdfs/data,/grid/sdb
/grid/sdf/hadoop/hdfs/data,/grid/sdf
/grid/sdg/hadoop/hdfs/data,/grid/sdg
答案4
使用字符串连接运算符的替代(基于字段)perl 方法.=
:
perl -F/ -lpe '$_ .= "$F[1]/$F[2]" unless defined $F[7]' dfs_data_dir_mount.hist