终端:提取相同的列数据并输出到多个文件中

时间:2022-12-04 15:47:19

I have 30 files which every file has the same data structure, but for different observations. I want to extract the each file's first 4 columns data and then output each file's result into a new file. Therefore, I will have another 30 files which every file only contains original file's first 4 columns data.

我有30个文件,每个文件具有相同的数据结构,但用于不同的观察。我想提取每个文件的前4列数据,然后将每个文件的结果输出到一个新文件中。因此,我将有另外30个文件,每个文件只包含原始文件的前4列数据。

I used the command line in Ubuntu. I used the following code, but I only got all outputs into a single file.

我在Ubuntu中使用了命令行。我使用了以下代码,但我只将所有输出都放在一个文件中。

awk '{print $1, $2, $3, $4}' File_*.txt > Part_File.txt ##I use the * to represent 01 to 30.

awk'{print $ 1,$ 2,$ 3,$ 4}'File _ * .txt> Part_File.txt ##我使用*来表示01到30。

Could anyone help me? Thanks in advance.

谁能帮助我?提前致谢。

1 个解决方案

#1


2  

EDIT: As per OP needed output file as Output_file00 etc format so following may help you. Also if you have only 30 files to read then you could remove this close(prev);prev=FILENAME part, this will save you from too many files opened error in case you are reading n number of files by this code too.

编辑:根据OP需要输出文件作为Output_file00等格式,以下可能会帮助您。此外,如果您只有30个文件可供阅读,那么您可以删除此关闭(上一个); prev = FILENAME部分,这将为您节省太多文件打开错误,以防您通过此代码读取n个文件。

awk 'FNR==1{close(out); out=sprintf("Output_%02d",++i)} {print $1, $2, $3, $4 > out}' File_*.txt 

Since you haven't provided samples so couldn't test it, could you please try following and let me know if this helps you.

由于您没有提供样品,因此无法测试,请您试试,请告诉我这是否对您有所帮助。

awk '{print $1,$2,$3,$4 > ("Output_file"i)}' File_*.txt

Above should create Output_file(s) eg--> Output_file1, Output_file2 and so on.

上面应该创建Output_file,例如 - > Output_file1,Output_file2等等。

#1


2  

EDIT: As per OP needed output file as Output_file00 etc format so following may help you. Also if you have only 30 files to read then you could remove this close(prev);prev=FILENAME part, this will save you from too many files opened error in case you are reading n number of files by this code too.

编辑:根据OP需要输出文件作为Output_file00等格式,以下可能会帮助您。此外,如果您只有30个文件可供阅读,那么您可以删除此关闭(上一个); prev = FILENAME部分,这将为您节省太多文件打开错误,以防您通过此代码读取n个文件。

awk 'FNR==1{close(out); out=sprintf("Output_%02d",++i)} {print $1, $2, $3, $4 > out}' File_*.txt 

Since you haven't provided samples so couldn't test it, could you please try following and let me know if this helps you.

由于您没有提供样品,因此无法测试,请您试试,请告诉我这是否对您有所帮助。

awk '{print $1,$2,$3,$4 > ("Output_file"i)}' File_*.txt

Above should create Output_file(s) eg--> Output_file1, Output_file2 and so on.

上面应该创建Output_file,例如 - > Output_file1,Output_file2等等。