Shell 在unix脚本中将日期时间戳问题附加到文件名
我有一堆由遗留系统生成的文件,需要将这些文件重命名为业务用户能够理解的长描述 示例文件名:buusrapp.O16200.L1686.1201304590298 RptList:应用程序用户的bususrapp计费文件 当系统生成一个文件“bususrapp”文件时,该文件将被转换为“应用程序用户的账单文件”,最终输出需要 类似于app user.1201201304590298.txt的账单文件 示例文件中的最后一个限定符是我的日期和时间戳。我需要剪切/复制并将其附加到新的长描述文件名中。app user.1201201304590298.txt的账单文件Shell 在unix脚本中将日期时间戳问题附加到文件名,shell,unix,awk,Shell,Unix,Awk,我有一堆由遗留系统生成的文件,需要将这些文件重命名为业务用户能够理解的长描述 示例文件名:buusrapp.O16200.L1686.1201304590298 RptList:应用程序用户的bususrapp计费文件 当系统生成一个文件“bususrapp”文件时,该文件将被转换为“应用程序用户的账单文件”,最终输出需要 类似于app user.1201201304590298.txt的账单文件 示例文件中的最后一个限定符是我的日期和时间戳。我需要剪切/复制并将其附加到新的长描述文件名中。ap
请建议如何实现此目的。提供您的输入文件,此awk将生成您所需的输出文件名:
echo "bususrapp.O16200.L1686.1201201304590298 RptList: bususrapp Billing file for app user" | awk -F"[.]| RptList| bususrapp " '{print $NF"." $4 ".txt"}'
Billing file for app user.1201201304590298.txt
使用FS完成所有繁重的工作,然后按指定顺序打印字段
这里有一个更完整的答案:
# use find instead of ls, pointing to files only
find /tmp/Rptlist -name "bususrapp*" -type f -print |
awk -F/ '
{
# rebuild the path which was split up by FS
for(i=1;i<NF;i++) path = path $i"/"
fname=$NF
# split up the file name for recombination
c=split(fname, a, "[.]| RptList:| bususrapp ")
# output 2 arguments the "/original/file/path" "/new/file/path"
printf( "\"%s%s\" \"%s%s.%s.txt\"\n", path, fname, path, a[c], a[4] )
path=""
}' |
# use xargs to feed those two arguments to mv for the copy
xargs -n2 mv
只是一种不同的风格@EtanReisner的答案更清晰。由于我没有在bash中编写脚本,所以我会尝试这样做。给出您的输入文件,此awk将生成所需的输出文件名:
echo "bususrapp.O16200.L1686.1201201304590298 RptList: bususrapp Billing file for app user" | awk -F"[.]| RptList| bususrapp " '{print $NF"." $4 ".txt"}'
Billing file for app user.1201201304590298.txt
使用FS完成所有繁重的工作,然后按指定顺序打印字段
这里有一个更完整的答案:
# use find instead of ls, pointing to files only
find /tmp/Rptlist -name "bususrapp*" -type f -print |
awk -F/ '
{
# rebuild the path which was split up by FS
for(i=1;i<NF;i++) path = path $i"/"
fname=$NF
# split up the file name for recombination
c=split(fname, a, "[.]| RptList:| bususrapp ")
# output 2 arguments the "/original/file/path" "/new/file/path"
printf( "\"%s%s\" \"%s%s.%s.txt\"\n", path, fname, path, a[c], a[4] )
path=""
}' |
# use xargs to feed those two arguments to mv for the copy
xargs -n2 mv
只是一种不同的风格@EtanReisner的答案更清晰。因为我没有在bash中编写脚本,所以我会尝试这样做。我相信下面的内容可以满足您的需要
for file in *; do
# Drop everything up to the last .
stamp=${fname##*.};
# Drop everything after the first period.
name=${fname%%.*}
# Find the matching line in RptList.
line=$(grep "$name" RptList)
# Drop the first (space separated) field.
line=${line#* }
outfile="$line.$stamp.txt"
mv $file $outfile;
done
我相信以下内容将满足您的要求
for file in *; do
# Drop everything up to the last .
stamp=${fname##*.};
# Drop everything after the first period.
name=${fname%%.*}
# Find the matching line in RptList.
line=$(grep "$name" RptList)
# Drop the first (space separated) field.
line=${line#* }
outfile="$line.$stamp.txt"
mv $file $outfile;
done
一美元的价值是多少?如果它包含完整的文件名,那么如何从grep部分返回任何结果呢?千万不要像这样循环ls的结果。直接使用地球仪。因为我在*。。。;i美元的价值是多少?如果它包含完整的文件名,那么如何从grep部分返回任何结果呢?千万不要像这样循环ls的结果。直接使用地球仪。因为我在*。。。;doneYou可以使用awk'..'您可以使用awk'..'谢谢大家的回答。伊坦的解决方案对我很有效。谢谢大家的回答。伊坦的解决方案对我很有效。