How can I count instances of a regex match in a log file using PowerShell?

641

Solution 1

If you want to break out just the title portion (which I'm guessing you do) and not group based on the whole URL (which could contain information specific to that visit) you need to get the value of the title parameter like so:

get-content "test.txt" | % {if($_ -match 'title=([^\&]+)') {$($Matches[1])}} | group | sort -desc Count

Solution 2

This is off the top of my head but you should be able to do this with a one-liner.

You can either shove it in a variable and get the length of that variable like so:

$count = get-content .\test.txt | select-string -pattern "AA000"
$count.length

Or our can just do it all inline by using parens:

(get-content .\test.txt | select-string -pattern "AA000").length

You can do you top count with the group-object cmdlet.

get-content .\test.txt | group-object | export-csv out.csv

That is pretty ugly right now, but you should be able to go from there

Share:
641

Related videos on Youtube

Pierson Leo
Author by

Pierson Leo

Updated on September 18, 2022

Comments

  • Pierson Leo
    Pierson Leo over 1 year

    I have a receiver that plays audio when phone screen is turned on or off.

    I send the audio file to the receiver like this

                    try {
                        Intent i = new Intent("my.action");
                        i.putExtra("posLock", newPosition2).putExtra("songlistLock", mySongs).putExtra("lockSound", "lock");
                        sendBroadcast(i);
                    }catch (Exception e) {
                        Log.e(TAG, "Intent error");
                    }
    

    and

                    try {
                        Intent i = new Intent("my.action.unlock");
                        i.putExtra("posUnlock", newPosition3).putExtra("songlistUnlock", mySongs).putExtra("unlockSound", "unlock");
                        sendBroadcast(i);
                    }catch (Exception e) {
                        Log.e(TAG, "Intent error2");
                    }
    

    Then I play the audio on my receiver class

    LockScreenReceiver.java

    public class LockScreenReceiver extends BroadcastReceiver {
    
    MediaPlayer mp;
    ArrayList<File> mySongs;
    ArrayList<File> mySongs2;
    Uri u;
    Uri u2;
    AudioManager am;
    private static final String TAG = SecondScreen.class.getSimpleName();
    
    
    
    @Override
    public void onReceive(Context context, Intent intent) {
    
        String action = intent.getAction();
        am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
    
        if(action.equals("my.action")) {
                Bundle b = intent.getExtras();
                mySongs = (ArrayList) b.getParcelableArrayList("songlistLock");
                int position = b.getInt("posLock", 0);
    
                u = Uri.parse(mySongs.get(position).toString());
            }
    
            if(action.equals("my.action.unlock")) {
                Bundle b = intent.getExtras();
                mySongs2 = (ArrayList) b.getParcelableArrayList("songlistUnlock");
                int position = b.getInt("posUnlock", 0);
    
                u2 = Uri.parse(mySongs2.get(position).toString());
            }
    
    
        if (action.equals(Intent.ACTION_SCREEN_ON) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
        {
            if(u2!=null) {
                stopPlaying();
                mp = MediaPlayer.create(context, u2);
                mp.start();
                Toast.makeText(context, "Audio on playing", Toast.LENGTH_SHORT).show();
            }
        }
        else if (action.equals(Intent.ACTION_SCREEN_OFF) && am.getRingerMode() == AudioManager.RINGER_MODE_NORMAL)
        {
            if(u!=null) {
                stopPlaying();
                mp = MediaPlayer.create(context, u);
                mp.start();
                Toast.makeText(context, "Audio off playing", Toast.LENGTH_SHORT).show();
    
            }
        }
    
    
    }
    
    private void stopPlaying() {
        if (mp != null) {
            mp.stop();
            mp.release();
            mp = null;
        }
    }
    }
    

    And I register my receiver using service

    LockScreenService.java

    public class LockScreenService extends Service {
    
    BroadcastReceiver receiver;
    
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }
    
    @Override
    @SuppressWarnings("deprecation")
    public void onCreate() {
    
        //Start listening for the Screen On, Screen Off, and Boot completed actions
        IntentFilter filter = new IntentFilter(Intent.ACTION_SCREEN_ON);
        filter.addAction(Intent.ACTION_SCREEN_OFF);
        filter.addAction(Intent.ACTION_BOOT_COMPLETED);
    
        //Set up a receiver to listen for the Intents in this Service
        receiver = new LockScreenReceiver();
        registerReceiver(receiver, filter);
        registerReceiver( receiver, new IntentFilter( "my.action" ) );
        registerReceiver( receiver, new IntentFilter( "my.action.unlock" ) );
    
       // Toast.makeText(getApplicationContext(), "Starting service now", Toast.LENGTH_SHORT).show();
    
        super.onCreate();
    }
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        return START_STICKY;
    }
    
    @Override
    public void onDestroy() {
        unregisterReceiver(receiver);
        super.onDestroy();
    }
    }
    

    And I also register my receiver in my Manifest.xml

    Manifest.xml

        <receiver
            android:name=".LockScreenReceiver"
            android:enabled="true">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />
                <action android:name="my.action" />
                <action android:name="my.action.unlock" />
            </intent-filter>
        </receiver>
    

    From what I read, when receiver is registered through the manifest, it should keep on running even after the app is terminated. But when I test it, my phone stops playing the audio after I closed my app from the recent apps manager.

    • Scott Keck-Warren
      Scott Keck-Warren over 12 years
      Is the log file delimited in some way (tab, csv, etc.)?
    • TZttt
      TZttt over 12 years
      Nope. The part I'm trying to parse out is in the middle of very long URLs as a parameter (&Title=AA000####).
    • Deividi Cavarzan
      Deividi Cavarzan over 8 years
      You should implement onStartCommand and return the flag START_STICKY developer.android.com/intl/pt-br/reference/android/app/…
    • Pierson Leo
      Pierson Leo over 8 years
      I tried that before, but it doesn't work.
    • Deividi Cavarzan
      Deividi Cavarzan over 8 years
      because your MediaPlayer object need to be in the service, not in the receiver
    • Pierson Leo
      Pierson Leo over 8 years
      Can you post the code? Does that mean I should play the audio from the service instead of from the receiver?
    • Deividi Cavarzan
      Deividi Cavarzan over 8 years
    • Pierson Leo
      Pierson Leo over 8 years
      Will it work If I make and play my MediaPlayer object in the service and send the player as an intent to the receiver? (so that it plays audio when phone screen is turned on or off)
  • TZttt
    TZttt over 12 years
    I need to count how many times a given AA000#### appears in the file. For example, how many times was item PX0001582 requested? What I'm really interested in is which items were requested the most times.
  • TZttt
    TZttt over 12 years
    This worked GREAT. Thanks!! I learned something today :)
  • Narendra talapara
    Narendra talapara almost 8 years
    this works fine in on 4.4+ version i have added also
  • psantiago
    psantiago over 6 years
    One thing to keep in mind is also piping to format-table autosize ( | ft -autosize ) in the event of your counts being large enough to where powershell ellipses the output.