Amy_777
Amy_777

Reputation: 115

Terminal: extract same columns data and output into multiple files

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.

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

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

Could anyone help me? Thanks in advance.

Upvotes: 0

Views: 194

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133760

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.

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.

Upvotes: 2

Related Questions