Bash、Python或Awk来匹配和修改文件

2024-09-30 14:33:49 发布

您现在位置:Python中文网/ 问答频道 /正文

10000个数据集。。。10000.dat.元。每个文件都包含一行以@开头的行,并包含一个与此文件相关的空格的字符串,即lijec37 7.379 6.23。在

我有另外一组10000个文件_发送日期(其中XXX从1到10000)。每个文件只有一行。每条线路均为thsis类型:

_1 3456.000000-21 0-98.112830-20.326192

我想做的是,对于每个数字XXX(介于1到10000之间),从cXXX.dat公司将字符串像c37 7.379 6.23一样归档,并将其添加到确定的\u cXXX文件中_发送日期到文件的开头,我得到:

c37 7.379 6.23?1 3456.000000-21 0-98.112830-20.326192

我尝试了bash和python,但没有找到好的解决方案。在

最好的方法是什么?在

谢谢


Tags: 文件数据字符串bash类型公司数字线路
3条回答

如果两种类型的文件都只有一行:

for i in {1..10000}
do
    paste "c${i}.dat" "determined_c${i}_send.dat" > c${i}.out && 
    mv "c{$i}.out" "determined_c${i}_send.dat"
done

编辑:

^{pr2}$

在Python中,可以执行类似的操作

# loop on all the files
for num in range(1,1000):

    cfile = open ( 'c%u.dat'%num, mode='r')

    # find the specific line
    for line in cfile:
        if line[0]=='@':

            # open the determined file and add the line
            dfile = open( 'determined_c%u_send.dat'%num, mode='a')
            dfile.write( line[1:-1] )
            dfile.close()

    cfile.close()

它还没有经过测试,但应该可以用

编辑:我意识到你想在已确定的∗cXXX的开头添加一行_发送日期,不是在结尾。

因此,根据丹尼斯·威廉姆森的回答,我还可以提出以下bash代码

^{pr2}$

一种基本上用来处理文本的语言:Perl!

相关问题 更多 >