Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 563029
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T12:37:00+00:00 2026-05-13T12:37:00+00:00

I am trying to output ‘awk’ result to file in my script, with no

  • 0

I am trying to output ‘awk’ result to file in my script, with no success.
Using ‘>’ does not 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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T12:37:01+00:00Added an answer on May 13, 2026 at 12:37 pm

    Output redirection is generally a feature of the shell you’re working with and, given how much use it gets, I’d be pretty amazed if you’d found a bug in it 🙂

    Are you sure you’re not trying to do redirection with awk itself rather than the shell?

    What happens when you do:

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

    Update:

    If this is your code as stated, it’s because the $a is not being expanded by your shell script since the awk command is within 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 tend to do is pass in specific values to awk using the -v option, something like (in your case):

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

    Then the variables become first-class awk citizens without having to worry about who’s doing the expansion.


    Further update:

    I’m standing behind my original advice. There’s something definitely screwy with the method chosen.

    I have a directory in my home directory called XpVm (among others) and I’ve created the file CDRNOutput_X.csv containing the single 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’ve stripped out 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 option to 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, -v a=$a '{
            if ($10 == a) {
                print $0
            } else {
                print "NO"
            }
        }' ./CDRNOutput_X.csv
    done
    

    (the only difference being the changes 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
    

    which is correct.


    Final update (hopefully):

    I think I have the problem solved. I’m on a different machine now (so the directory names are simply 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 a modified CDRNOutput_X.csv containing tmp instead of XpVm, I get:

    tmp is a directory
    NO
    tmp2 is a directory
    NO
    

    That’s because the if statement is being seen by awk as:

            if ($10 == tmp) {
    

    (without quotes, since the quotes are actually outside the awk string being used to surround the directory name). This will test $10 for equality against the awk variable called tmp rather than the actual string "tmp". What you need is to make sure that the quotes are inside the awk script, like:

            if ($10 == "tmp") {
    

    and you can do this 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 the double quotes are duplicated. I’ve still kept the double quotes immediately around $a in case someone’s committed the heinous crime of creating a file with a space in it 🙂

    Running that script produces:

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

    which is what I think you were aiming for.

    So, the upshot is, if you don’t want to use awk variables, you can just change your awk string from:

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

    to:

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

    and it should function okay.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to output a XML file using Python and lxml However, I
I'm trying to output some data to file in Java and do not understand
I'm trying to output a CDATA section in the result of XSLT using Xalan
I'm trying to output some data from a python script to a binary file.
I am trying to output a file's contents to terminal using the File.read() function
I was trying to output a not null terminated char array to a file.
I'm trying to output the result of my SQL query to a div using
I am trying to output dates in the Italian format using date() as follows:
I'm trying to output unicode string into RTF format. (using c# and winforms) From
I am trying to output a file in perl. I open the file and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.