bash +如何根据现有路径将路径追加到行尾

bash +如何根据现有路径将路径追加到行尾

以下配置文件(示例1) 未按应有的方式配置。

文件中的每一行应包含/grid/sdX( ato 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

相关内容