How to output awk result to file

I am trying to output the result of 'awk' for a file in my script, without success. Using '>' doesn't work, why?

for a in $(find $OUPUT_DIR/ -maxdepth 1 -mindepth 1 -type d -printf "%P\n") do echo $a is a directory awk -F, '{ if ($10 == '"$a"') print $0 }' $OUPUT_DIR/CDRNOutput_${CDR_DATE}.csv > $OUPUT_DIR/$a/CDR-${CDR_DATE}.csv done 
+4
source share
2 answers

Redirecting output is usually a function of the shell you are working with, and given how useful it is, I would be amazed if you found an error in it :-)

Are you sure you are not redirecting with awk , not a shell?

What happens when you do:

 echo 'hello' | awk '{print}' >qq.tmp 

Update:

If this is your code, as stated, this is because the $a is not expanded by your shell script, as the team awk is in single quotes.

 for a in $(find $OUPUT_DIR/ -maxdepth 1 -mindepth 1 -type d -printf "%P\n") do echo $a is a directory awk -F, '{ if ($10 == '"$a"') print $0 }' $OUPUT_DIR/CDRNOutput_${CDR_DATE}.csv > $OUPUT_DIR/$a/CDR-${CDR_DATE}.csv done 

What I mean is to pass specific awk values ​​using the -v , something like (in your case):

 awk -F, -va=$a '{ if ($10==a) print $0 }' ... 

Variables then become first-class awk citizens, without worrying about who makes the extension.


Further update:

I stood behind my original advice. There is something definitely related to the chosen method.

I have a directory in my home directory called XpVm (among others), and I created a CDRNOutput_X.csv file containing one line:

 1,2,3,4,5,6,7,8,9,XpVm,11 

When I execute:

 for a in $(find . -maxdepth 1 -mindepth 1 -type d -printf "%P\n" | grep -v '^\.') do echo $a is a directory awk -F, '{ if ($10 == '"$a"') { print $0 } else { print "NO"; } }' ./CDRNOutput_X.csv done 

(I deleted the directories starting with . , Since they were causing another problem), I get this output:

 workspace is a directory NO Documents is a directory NO XpVm is a directory NO Downloads is a directory NO 

which is clearly not what is expected. However, when I use the -v for awk , as I originally suggested, the command:

 for a in $(find . -maxdepth 1 -mindepth 1 -type d -printf "%P\n" | grep -v '^\.') do echo $a is a directory awk -F, -va=$a '{ if ($10 == a) { print $0 } else { print "NO" } }' ./CDRNOutput_X.csv done 

(the only difference is changing to a ), I get:

 workspace is a directory NO Documents is a directory NO XpVm is a directory 1,2,3,4,5,6,7,8,9,XpVm,11 Downloads is a directory NO 

what is right.


Final update (hopefully):

I think the problem is resolved. Now I'm on a different machine (so the directory names are just tmp and tmp2 ), and when I run the original script:

 for a in $(find . -maxdepth 1 -mindepth 1 -type d -printf "%P\n" | grep -v '^\.') do echo $a is a directory awk -F, '{ if ($10 == '"$a"') { print $0 } else { print "NO"; } }' ./CDRNOutput_X.csv done 

with the modified CDRNOutput_X.csv containing tmp instead of XpVm , I get:

 tmp is a directory NO tmp2 is a directory NO 

This is because the if treated as awk like:

  if ($10 == tmp) { 

(without the quotes as the quotes are actually located outside the line awk , used to surround the directory name). This will check $10 for equality with the awk variable named tmp , not the actual string "tmp" . You need to make sure the quotes are inside the awk script, for example:

  if ($10 == "tmp") { 

and you can do it with the following script (only the if line has changed):

 #!/bin/bash for a in $(find . -maxdepth 1 -mindepth 1 -type d -printf "%P\n" | grep -v '^\.') do echo $a is a directory awk -F, '{ if ($10 == "'"$a"'") { print $0 } else { print "NO"; } }' ./CDRNOutput_X.csv done 

Note that double quotes are duplicated. I still kept double quotes around $a if someone committed the terrible crime of creating a file with a space in it :-)

Running this script calls:

 tmp is a directory 1,2,3,4,5,6,7,8,9,tmp,11 tmp2 is a directory NO 

what I think, what you were striving for.

So, the result is that if you don't want to use awk variables, you can just change the awk line:

 '{ if ($10 == '"$a"') print $0 }' 

in

 '{ if ($10 == "'"$a"'") print $0 }' 

and it should work fine.

+6
source

since you will find a command with the -mindepth parameter and maxdepth equal to 1, you can simply do this with the shell

 #!/bin/bash CDR_DATE="somedate" infile=CDRNOutput_${CDR_DATE}.csv outfile=CDR-${CDR_DATE}.csv OUPUT_DIR="/some/dir" cd $OUPUT_DIR for dir in */ do echo "${dir%/*} is a directory" dir=${dir%/*} while read -rabcdefghijk do case "$j" in $dir) echo $a $b $c $d $e $f $g $h $i $j $k >> $dir/$outfile;; esac done < $infile done 
0
source

Source: https://habr.com/ru/post/1299230/


All Articles