我对bash没有经验,所以我在语法上挣扎 - 我放弃了在python中尝试,因为我认为它可能更容易。我想提取部分文件名(在.xyz扩展名之前,在前缀之后),将其插入每一行(从第三行开始)并将输出传递给新文件。我还想为多个文件执行此操作,其中字符串更改。
我的输入文件如下:
blahblah-0.xyz
blahblah-1.xyz
blahblah-2xyz
到目前为止,我知道我可以做到:
sed '3,$ s/^/500 /' file-500.xyz > output
这将在每一行插入字符串。但我不想为每个目录做100次这样的事情!我也从这里尝试了以下内容:awk parse filename and add result to the end of each line:
for filename in ratio*; do
num=$(echo $filename | grep -Eo '[^ratio_distances]+\.xyz' | cut -d. -f1)
sed -i "s/\^/\t$num" $filename
done
只是补充一下,这只是在标准的mac终端中执行,因为我已经出现了关于&sed -i'命令。
修改
我让它在python中工作,但我仍然有兴趣知道bash命令。 Python代码应该是任何其他人在同一件事之后:
import sys
import os
import glob
list_of_files = glob.glob("./blah-blah*.xyz")
for file in list of files:
for i in range (0, 80):
P = 10*i
if str(P) in file:
with open(file, 'r') as infile:
lines = infile.readlines()
lines[:]=lines[2:]
lines = [str(P)+' '+line for line in lines]
with open(file.replace('blahblah','output'),'w') as outfile:
outfile.writelines(lines)
infile.close()
outfile.close()
非常感谢任何见解, 安娜
答案 0 :(得分:2)
假设您可以在旧文件名前加上" new _"创建新文件名:
awk '
FNR==1 { pfx = FILENAME; sub(/.*\./,"",pfx) }
FNR>=3 { $0 = pfx $0 }
{ print > ("new_"FILENAME) }
' ratio*
答案 1 :(得分:0)
您可以使用bash的参数扩展从文件名中提取数字。 Mac的sed不支持-i
,因此您必须使用临时文件:
#! /bin/bash
for filename in ratio* ; do
num=${filename#ratio_distances-} # Remove from the left.
num=${num%.xyz} # Remove from the right.
sed "3,\$ s/^/$num /" "$filename" > new"$num"
mv new"$num" "$filename" # Replace the original with the tempfile.
done
答案 2 :(得分:0)
#!/bin/bash
PrefixFile="blahblah"
awk -v "Prefix=${PrefixFile}" '
# At each new file starting
FNR == 1 {
# take number from current file name
gsub( "^" Prefix "-|[.]xyz$", "", Index = FILENAME)
}
# at each line (so for every files)
{
# print the index file (current) followed by original line
# to the (corresponding) filename.New
print Index $0 > ( FILENAME ".New" )
}
' ${PrefixFile}*.xyz
-
(如果没有,则容易适应)感谢@EdMorton提供有关文件重定向的awk行为的额外信息