-2

Please guide me to get out of it. Thanks in advance.

I am having a situation that I have to write a awk script which takes the input of two .out files and generate a single .txt file.

$ cat file1.sh
awk -f awk_file.awk < outfile1.out outfile2.out  > text_file.txt

I want to display the .txt file like,

----------------Output from File1----------

column1 column2 column3
--------------------------------------------

columns i will pick from outfile1.out

columns i will pick from outfile1.out

--------------------------------------------
Total no. of columns from outfile1.out

//////////////////////////////////////////////// ----------------Output from File2----------

column1 column2 column3 column4 column5
-------------------------------------------
columns i will pick from outfile2.out

columns i will pick from outfile2.out

columns i will pick from outfile2.out

columns i will pick from outfile2.out

----------------------------------------
Total no. of columns from outfile2.out

How resulting the text_file.txt???

  • And what is wrong with awk -f awk_file.awk outfile1.out outfile2.out > text_file.txt? – jimmij Jan 29 '15 at 8:46
  • it will work but what should be inside the awk_file.awk file, or how to format the inputs of both the files or you can say how i will control the inputs of both the files...?? – pankaj kumar Feb 2 '15 at 5:24
  • The content of awk script depends on what you are trying to do with those two files (output1.out and output2.out). So edit your question and add how resulting text_file.txt should look like. – jimmij Feb 2 '15 at 11:22
  • can i mail the situation as i am not able to explain here??? @jimmij – pankaj kumar Feb 2 '15 at 12:37
1

It all depends on what you want. If you want to process files one after the other, you can simply call your awk script on both files sequentially with a loop, and redirect the output:

(for file in outfile1.out outfile2.out; do awk -f awk_file.awk < "$file"; done)  > text_file.txt

or in this case of two files, simply

(awk -f awk_file.awk < outfile1.out; awk -f awk_file.awk < outfile2.out) > text_file.txt

If you wanted to merge the files together before processing with awk (this will treat them as a single file), you just do

cat outfile1.out outfile2.out | awk -f awk_file.awk > text_file.txt

It's a nice practice to let a tool do one job well, and let the shell glue together the pieces into a functioning script.

  • can i mail the situation as i am not able to explain here??? @orion – pankaj kumar Feb 2 '15 at 12:38
  • Just modify your question to post a short (few lines) example of outfile1, outfile2 and what you expect as output. Right now the problem is we don't know what you are trying to do. If you just want to apply the same script to both files, then my answer above is ok. If you are merging or combining data, then you need something different. – orion Feb 2 '15 at 12:49
  • yes i wanna get merging or combining data into single file – pankaj kumar Feb 2 '15 at 18:48
  • With what rule? Just tell us. A simple concatenation of two files is performed by cat. If you need something more sophisticated, you will probably need getline command in awk. But it really all depends on what you need, there are a lot of core utilities that are meant for different things, unless you tell us what you need, we can't help you. – orion Feb 2 '15 at 18:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.