Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to print lines containing a certain number, but not containing other numbers that never previously appeared?
    text
    copied!<p>I have a file containing many numbers, written with 10 leading digits and with "A" temporarily placed before and "Z" placed after, to make sure scripts do not misidentify the beginning and ending of a number. E.g.:</p> <pre><code>A00000000001Z A00000000003Z,A00000000004Z;A00000000005Z A00000000004Z A00000000005Zsome wordsA00000000001Z A00000000006Z;A00000000005Z A00000000001Z </code></pre> <p>I need to search for a particular number, but output only those lines where the number is found, but no other numbers that never previously appeared are on the same line.</p> <p>For example, if I searched for "0000000001", it would print lines 1, 3, and 5:</p> <pre><code>A00000000001Z A00000000004Z A00000000005Zsome wordsA00000000001Z A00000000001Z </code></pre> <p>It can print line 3 because the other numbers "00000000004" and "00000000005" previously appeared in line 2.</p> <p>If I searched for "00000000005", it would print line 3:</p> <pre><code>A00000000004Z A00000000005Zsome wordsA00000000001Z </code></pre> <p>It would not print line 2, because the other numbers "00000000003" and "00000000004" never appeared previously.</p> <p>So far, I have worked out this:</p> <pre><code># search for the line and print the previously appearing lines to a temporary file grep -B 10000000 0000000001 file.txt &gt; output.temp # send the last line to another file cat output.temp | tail -1 &gt; output.temp1 sed -i '$ d' output.tmp &gt; output.temp2 # search for numbers appearing in output.temp2 for i in 1 .. 1000000 NOT original number a=`printf $010d $i` if [ $a FOUND in output.temp2] then # check if was found in the previous line if [ $a NOT FOUND in output.temp1] else fi fi done &lt; ./file.txt </code></pre> <p>How can I print out only those lines containing a certain number, while excluding the other numbers that never previously appeared in the file?</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