Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how many instances did a command affect?

Tags:

sed

Is there a way when using sed from the cli to return how many lines were affected, or better yet how many instances were affected by a command that might have multiple affects per line if the global param is used? Pretty much, for me, that would mean how many substitutions were made.

I guess one could output to a new file and then run a diff on the two files afterward, but my need to know how many instances a command affects is not that great to do that. I just wondered if there might be a feature native to sed that can be employed.

like image 909
digiscripter Avatar asked Apr 08 '26 03:04

digiscripter


1 Answers

As far as I know, sed has no native feature to manipulate variables (e.g. to increase an internal counter). Definitely, that is one of the features brought by awk that were lacking in sed. So my advice would be that you switch to awk, and then you can easily use an awk script such as:

BEGIN { counter = 0 }
/mypattern/ { do-whatever-you-want; counter++ }
END { print counter }
like image 90
jaybee Avatar answered Apr 20 '26 04:04

jaybee