0

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.

1 Answer 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.

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Many thanks! Your code works well and the outputs do have the form like: Output_file1, Output_file2 and so on. However, if I hope the output has the prev like Output_file01, Output_02 and so on, what should I do?
@EdMorton, thanks a TON Ed sir I have changed it now. So it means whenever we edit a file then only we need to close a file it means? reading input_file(s) or putting data into output_file(s) will not cause the too many opened file issue? could you please guide.
@EdMorton, sure, close(FILENAME) was a mistake, thanks for letting me know.
@EdMorton, sure done sir edited it. I really wonder how much knowledge do you have sir, hats off to you really :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.