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 6985335
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:40:00+00:00 2026-05-27T18:40:00+00:00

I have the below code in a bash file called ‘findError.sh’: #!/bin/bash filename=$1 formatindicator=\|\

  • 0

I have the below code in a bash file called ‘findError.sh’:

#!/bin/bash
filename="$1"
formatindicator="\"|\""
echo "$formatindicator"
formatarg="\$1"
echo "$formatarg"
count=`awk -F$formatindicator '{print $formatarg}' $filename | perl -ane '{ if(m/ERROR/) { print } }' | wc -l `
command="awk -F$formatindicator '{print $formatarg}' $filename | perl -ane '{ if(m/ERROR/) { print } }' | wc -l"
echo $command
echo $count

I then run it at the command line like so:
sh findError.sh test.dat

But It gives me a different count than running the command that was echoed?? How is this possible?

ie
The $command that is echoed back is:

awk -F"|" '{print $1}' test.dat | perl -ane '{ if(m/ERROR/) { print } }' | wc -l

But the $count that is echoed back is:

3

However if I just run this one line below at the command line (not through the script) – the result is 0:

awk -F"|" '{print $1}' test.dat | perl -ane '{ if(m/ERROR/) { print } }' | wc -l

Sample input file (test.dat):

sid|storeNo|latitude|longitude
2|1|-28.03720000
9|2
10
jgn352|1|-28.03ERROR720000
9|2|fdERRORkjhn422-405
0000543210|gfERRORdjk39

Notes: Using SunOS with bash version 4.0.17

  • 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-27T18:40:01+00:00Added an answer on May 27, 2026 at 6:40 pm

    You’re being too careful with your quotes around the format delimiter.

    When you type:

    awk -F"|" ...
    

    The program (awk) sees -F| as its first argument; the shell strips the double quotes.

    When you have:

    formatindicator="\"|\""
    echo "$formatindicator"
    formatarg="\$1"
    echo "$formatarg"
    count=`awk -F$formatindicator ...`
    

    You have preserved the double quotes in $formatindicator and therefore awk sees -F"|" as the delimiter, and uses the double quote as the delimiter.

    Use:

    formatindicator="|"
    echo "$formatindicator"
    formatarg="\$1"
    echo "$formatarg"
    count=`awk -F"$formatindicator" ...`
    

    The difference is that the shell strips the quotes off -F"$formatindicator" but doesn’t do that when $formatindicator itself contains the double quotes.

    (NB: edited to retain back-quotes instead of the $(...) notation that is (a) preferred and (b) was used in the first version of this answer. The $(...) notation was not recognized by the SunOS /bin/sh which was, I believe, being used to execute the script. Both bash and ksh recognize the $(...) notation, but the basic Bourne shell, /bin/sh, on Solaris 10 (SunOS 5.10) and earlier (I’ve not laid hands on Solaris 11) does not recognize $(...).)

    I note that any of perl, awk or grep could be used to find the count of the error lines on its own, so the triplet of awk piped to perl piped to wc is not very efficient.

    awk -F"|" '$1 ~ /ERROR/ { count++ } END { print count }' $filename
    
    grep -c ERROR $filename                # simple
    grep -c '^[^|]*ERROR[^|]*|' $filename  # accurate
    
    perl -anF"|" -e '$count++ if $F[0] =~ m/ERROR/; END { print "$count\n"; }' $filename
    

    It’s Perl, so TMTOWTDI; take your pick…


    Side discussion

    In the comments, we have concerns over how various parts of the script are being interpreted.

    formatindicator="|"
    formatarg="\$1"
    
    count=`awk -F$formatindicator '{print $formatarg}' $filename | perl -ane '{ if(m/ERROR/) { print } }' | wc -l `
    

    Let’s simplify this to (using part of my main answer):

    count=`awk -F"$formatindicator" '{print $formatarg}' $filename`
    

    The intention is to have the delimiter specified on the command line (which happens successfully) via the -F option. The issue, I expect, is “why does $formatarg get expanded inside single quotes?”. And the answer is “Does it?”. I think not. So, what is happening, is that awk is seeing the script {print $formatarg}. Since formatarg is not assigned any value, it is equivalent to 0, so the script prints $0, which is the entire input line. Perl is quite happy to echo the line if it matches ERROR anywhere on the line, and wc couldn’t care less about what’s in the lines, so the result is approximately what was expected. The only time there’d be a discrepancy is when the line from $filename contains ERROR in other than the first pipe-delimited field. That would be counted by the script where it should not.

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

Sidebar

Related Questions

I have a bash code below under FreeBSD: tar -c file1 > file1.tar It
I have below code in html. <li class=selected runat=server id=lihome><a href=/ISS/home.aspx title=Home><span>Home</span></a></li> Now I
I have below code behind in c# if (Session[cmpDictionaryTitle]!= null) { downloadLinks.Text += @<li><a
I have below code to insert a style into DOM (there is a use
I have below html code in my aspx. <input type=hidden id=medicalLink value='<a href=/forms/contactus.aspx >Contact
I have below XAML code : <Window x:Class=WpfApplication1.Window1 xmlns=http://schemas.microsoft.com/winfx/2006/xaml/presentation xmlns:x=http://schemas.microsoft.com/winfx/2006/xaml DataContext={Binding RelativeSource={RelativeSource Self}} WindowStartupLocation=CenterScreen
I have the below code in stdafx.h. using namespace std; typedef struct { DWORD
I have the below code, which iterates over a list based on a custom
I have the below code for my a Dialog box for a which contains
Hi I have below pseudo code with throws an exception like this throw new

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.