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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:31:36+00:00 2026-05-27T01:31:36+00:00

I’m writing a shell script, and in an effort to make it shorter and

  • 0

I’m writing a shell script, and in an effort to make it shorter and easier to read, I’m trying to use nested subshells to pass parameters to diff.

Here’s what I have:

if
  diff -iy '$(sort '$(awk 'BEGIN { FS = "|" } ; {print $1}' new-participants-by-state.csv)' '$(awk 'BEGIN { FS = "|" } ; {print $1}' current-participants-by-state.csv)')' > /dev/null;
then  
  echo There is no difference between the files. > ./participants-by-state-results.txt;
else  
  diff -iy '$(sort '$(awk 'BEGIN { FS = "|" } ; {print $1}' new-participants-by-state.csv)' '$(awk 'BEGIN { FS = "|" } ; {print $1}' current-participants-by-state.csv)')' > ./participants-by-state-results.txt;
fi

When I run the script, I keep getting diff: extra operand 'AL'

I’d appreciate any insight into why this is failing. I think I’m pretty close. Thanks!

  • 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-27T01:31:37+00:00Added an answer on May 27, 2026 at 1:31 am

    Your code is unreadable because the lines are so long:

    if diff -iy '$(sort '$(awk 'BEGIN { FS = "|" } ; {print $1}' new-participants-by-state.csv)' \
           '$(awk 'BEGIN { FS = "|" } ; {print $1}' current-participants-by-state.csv)')' \
           > /dev/null;
    then  
        echo There is no difference between the files. > ./participants-by-state-results.txt;
    else  
       diff -iy '$(sort '$(awk 'BEGIN { FS = "|" } ; {print $1}' new-participants-by-state.csv)' \
          '$(awk 'BEGIN { FS = "|" } ; {print $1}' current-participants-by-state.csv)')' \
          > ./participants-by-state-results.txt;
    fi
    

    Repeating whole commands like that is also fairly nasty. You also have major problems with your use of single quotes; you only have one sort in each set of commands, apparently operating on the combined outputs of two identical awk commands (whereas you probably need two separate sorts, one for the output of each awk command); you’re not using the -F option to awk when you could; you are repeating the gargantuan file names all over the place; and finally, it appears that you are probably wanting to use process substitution, but not actually doing so.

    Let’s take a step back and formulate the question clearly.

    • Given two files (new-participants-by-state.csv and current-participants-by-state.csv) find the first pipe-separated field on each line of each file, sort the lists of those fields, and compare the results of the two sorted lists.
    • If there are no differences, write a message into the output file participants-by-state-results.txt; otherwise, list the differences in the output file.

    So, we could use:

    oldfile='current-participants-by-state.csv'
    newfile='new-participants-by-state.csv'
    outfile='participants-by-state-results.txt'
    
    tmpfile=${TMPDIR:-/tmp}/xx.$$
    
    awk -F'|' '{print $1}' $oldfile | sort > $tmpfile.1
    awk -F'|' '{print $1}' $newfile | sort > $tmpfile.2
    
    if diff -iy $tmpfile.1 $tmpfile.2 > $outfile
    then echo "There is no difference between the files" > $outfile
    fi
    
    rm -f $tmpfile.?
    

    If this was going to be the final script, we’d want to put trap handling in place so that the temporary files are not left around unless the script is killed dead with SIGKILL.

    However, we can now use process substitution to avoid the temporary files:

    oldfile='current-participants-by-state.csv'
    newfile='new-participants-by-state.csv'
    outfile='participants-by-state-results.txt'
    
    if diff -iy <(awk -F'|' '{print $1}' $oldfile | sort) \
                <(awk -F'|' '{print $1}' $newfile | sort) > $outfile
    then echo "There is no difference between the files" > $outfile
    fi
    

    Note how the code carefully preserves symmetries where there are symmetries. Note the use of shortish variable names to avoid the repetition of long file names. Note that the diff command is run just once, not twice – throwing away results which are needed later is not very sensible.

    You could compress the output I/O redirection even more using:

    {
    if diff -iy <(awk -F'|' '{print $1}' $oldfile | sort) \
                <(awk -F'|' '{print $1}' $newfile | sort)
    then echo "There is no difference between the files"
    fi
    } > $outfile
    

    That sends the standard output of the enclosed commands to the file.

    Of course, CSV might not be the appropriate nomenclature if the files are pipe-separated rather than comma-separated, but that’s another matter altogether.

    I’m also assuming that the status from diff -iy works as suggested by the original script; I’ve not validated that usage of the diff command.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Basically, what I'm trying to create is a page of div tags, each has
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am writing an app with both english and french support. The app requests
I'm trying to create an if statement in PHP that prevents a single post

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.