Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The following script will do all of what you asked, <strong><em>including the bonus</em></strong>. Put this script in the top-level directory that contains all the possible files with the 'uid' you want to crop. The script will recursively search this directory and crop all files that match and put the result in a new file with a <code>.crp</code> extension at the end (see example below). I took special consideration to make sure that this script will work with whatever filename you throw at it, whether it contain spaces or newlines or whatever in its name.</p> <pre><code>#!/bin/bash uid="1111-ABCD-1111-SOME-GUID" while IFS= read -r -d $'\0' file; do printf "%s\n" "?$uid?+1,\$d" "1,/$uid/-1d" "%p" | ex -s "$file" &gt; "$file".crp echo "$file being cropped" done &lt; &lt;(grep -lZR --exclude="${0#*/}" --exclude="*.crp" "$uid" .) </code></pre> <h3>Result</h3> <pre> $ ./uid.sh ./sample1.txt being cropped ./subdir/sample2.txt being cropped $ cat ./sample1.txt.crp line three containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa line four containing other session id: 2222-ABCD-1111-SOME-GUID line five blaa blaa blaa line six containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa line seven containing other session id: 2222-ABCD-1111-SOME-GUID line eight containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa line nine containing other session id: 3333-ABCD-1111-SOME-GUID line ten containing my session id: 1111-ABCD-1111-SOME-GUID line eleven line twelve containing other session id: 3333-ABCD-1111-SOME-GUID blaa blaa blaa line thirteen containing my session id: 1111-ABCD-1111-SOME-GUID $ cat ./subdir/sample2.txt.crp line three containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa foo bar line eight containing my session id: 1111-ABCD-1111-SOME-GUID blaa blaa blaa baz line ten containing my session id: 1111-ABCD-1111-SOME-GUID </pre> <p>As you can see in the example above, my script found two files which matched, one of which was in a sub-directory below the top-level directory.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload