Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/linux/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Linux 根据特定条件粘贴数据_Linux_Bash_Shell_For Loop_Awk - Fatal编程技术网

Linux 根据特定条件粘贴数据

Linux 根据特定条件粘贴数据,linux,bash,shell,for-loop,awk,Linux,Bash,Shell,For Loop,Awk,专家我在一个目录中有许多文件,这些文件包含一些数字。可能与POSIX不兼容;在Bash5.0上测试。假设文件名不包含空格且具有固定长度 对于在*a.??中带有_a的文件_。?? 做 target_left=${file_with_a:0:15}例如,2019_01_NDV.NT target_right=${file_with_a:16:3}例如.AS cat$target_left[A-C]$target_right>${target_left}\ABC$target_right 完成 使用m

专家我在一个目录中有许多文件,这些文件包含一些数字。

可能与POSIX不兼容;在Bash5.0上测试。假设文件名不包含空格且具有固定长度

对于在*a.??中带有_a的文件_。?? 做 target_left=${file_with_a:0:15}例如,2019_01_NDV.NT target_right=${file_with_a:16:3}例如.AS cat$target_left[A-C]$target_right>${target_left}\ABC$target_right 完成 使用mapfile aka readarray,这是一个bash4+功能

#!/usr/bin/env bash

files=([0-9][0-9][0-9][0-9]__*[ABC]*.??)

while mapfile -d '' -n3 array && ((${#array[*]} == 3)); do
  if [[ ${array[0]%%_*} == ${array[1]%%_*} && ${array[0]%%_*} == ${array[2]%%_*}  ]]; then
    paste "${array[0]}" "${array[1]}" "${array[2]}" > "${array[0]%${array[0]:(-4)}}_ABC.${array[0]:(-2)}"
  fi
done < <(printf '%s\0' "${files[@]}")

考虑到所有因信息不足而被抨击的问题,我真不敢相信这会有多个答案。请阅读。您一定要将其复制/粘贴到中,并修复它告诉您的问题。