Skip to content

Commit

Permalink
docs(csvgrep): Use variable ($1) instead of replstr to avoid quoting …
Browse files Browse the repository at this point in the history
…issues
  • Loading branch information
jpmckinney committed Oct 4, 2023
1 parent 4e0b890 commit c4e5612
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion docs/scripts/csvgrep.rst
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,4 @@ Get the indices of the columns that contain matching text (``\x1e`` is the `Reco

.. code-block::
csvgrep -m 222 -a -c 1- examples/realdata/FY09_EDU_Recipients_by_State.csv | csvformat -M $'\x1e' | xargs -d $'\x1e' -I_ sh -c "echo '_' | csvcut -n" | grep 222
csvgrep -m 222 -a -c 1- examples/realdata/FY09_EDU_Recipients_by_State.csv | csvformat -M $'\x1e' | xargs -d $'\x1e' -n1 sh -c 'echo $1 | csvcut -n' sh | grep 222

2 comments on commit c4e5612

@athalhammer
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jpmckinney: After the discussion in #1209 it took me a while to grasp and understand what the tail part of the command actually does.

  1. I believe the sh after csvcut -n' is actually not needed as you are basically just supplying a first argument to the command that you want to execute so you can access the second argument (the line you parsed) as $1. It would be better to only use one argument (without the second sh) and access it via $0. This becomes more clear if you add the -t parameter that prints the executed commands to stderr before executing them.
$ csvgrep -m 222 -a -c 1- examples/realdata/FY09_EDU_Recipients_by_State.csv | csvformat -M $'\x1e' | xargs -t -d $'\x1e' -n1 sh -c 'echo $1 | csvcut -n' sh
sh -c 'echo $1 | csvcut -n' sh "State Name,State Abbreviate,Code,Montgomery GI Bill-Active Duty,Montgomery GI Bill- Selective Reserve,Dependents' Educational Assistance,Reserve Educational Assistance Program,Post-Vietnam Era Veteran's Educational Assistance Program,TOTAL,"
  1. I think 222 is not the best example as, for me, it does not have any output. I found 679 for this file more meaningful as it's found in two columns.

So in summary, I'd alter the example to:

csvgrep -m 679 -a -c 1- examples/realdata/FY09_EDU_Recipients_by_State.csv | csvformat -M $'\x1e' | xargs -d $'\x1e' -n1 sh -c 'echo $0 | csvcut -n' | grep 679

Sorry, I don't want to come across as pedantic and neither want to dwell on it. I appreciate the good software (I use it every day) and the effort you put into maintaining it.

@jpmckinney
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you! $0 is indeed simpler, without the extra sh. I meant to use 22, not 222 (22 gives some variety in what is matched).

Please sign in to comment.