awk command to direct the output to multiple files
Hi all,
I have an input file containing the names of the files on which the processing needs to be done on certain columns. My input file looks like this Code:
FID IID PHENO CNT CNT2 SCORE Code:
IFS=$'\n' Quote:
Code:
IFS=$'\n' |
what you mixed/missed is: Filename is evaluated by the shell, not by the awk. The awk script itself is: '{ if ($3 == 1) { print $6 } }' nothing more.
You cannot mix the two languages, the awk script cannot use $Filename as variable (which was defined in bash). If you want to do that you need to export it in shell and read that environment variable from awk (or pass this variable to the awk). |
Indeed, replace your code with the following:
Code:
for FileName in $(cat Artery_Aorta-ListOfScoreFilesForScript.txt); do cat "$FileName" | awk -v fn="$FileName" '{if ($3 == 1) {print $6 >>fn".control"} else {print $6 >>fn".case"}}'; done; |
This can also be done without invoking 'awk'.
Code:
#!/bin/bash |
Quote:
cat file | awk 'script' can be replaced by awk 'script' file also for and cat together not suggested, instead while should be used. awk knows the file processed, do not need to set a variable Code:
while read -r line; do |
Quote:
|
Quote:
If so, OP should prepend a sed or grep to his/her awk or add a condition in his/her else part... |
Quote:
|
awk can handle it, no need any external tool (for example post #4 has a solution). Also the variable NR can be used.
Code:
NR == 1 { next } |
Thankyou all for the reply. The code work. I have added the else if condition is the code which works for the header line as well. But I am encountering another problem. I want to run some statistical test using R on the cases and control files. Now the variable $FileName is not being read by the R command. I think I am gain mixing the two languages i.e., bash and R. For the R t-test, shall I loop again to grab the controls and cases files?
|
The code which I am trying to run (on HPC) is
Code:
cd $PBS_O_WORKDIR |
Quote:
This is very I/O intensive; if NFS it would stress the NFS server. The following opens/closes the file once, and even gives you the choice between > and >> (overwrite or append an existing file) Code:
#!/bin/bash the >> or > decides how the file is opened at the first write. Subsequent writes go to the stream i.e. append. |
Quote:
Code:
cd $PBS_O_WORKDIR || exit The difference is how to open an existing file at the first write. The ( ) are interpreted by the shell. This might still cause an error. But I don't know yet what Rscript is. |
All times are GMT -5. The time now is 02:40 PM. |