Note that there are some explanatory texts on larger screens.

plurals
  1. POLinux: counting spaces and other characters in file
    primarykey
    data
    text
    <p><strong>Problem:</strong></p> <p>I need to match an exact format for a mailing machine software program. It expects a certain format. I can count the number of new lines, carriage returns, tabs ...etc. using tools like</p> <pre><code>cat -vte </code></pre> <p>and </p> <pre><code>od -c </code></pre> <p>and </p> <pre><code>wc -l ( or wc -c ) </code></pre> <p>However, I'd like to know the exact number of leading and trailing spaces between characters and sections of text. Tabs as well. </p> <p><strong>Question:</strong></p> <p>How would you go about analyzing then matching a template exactly using common unix tools + perl or python? One-liners preferred. Also, what's your advice for matching a DOS encoded file? Would you translate it to NIX first, then analyze, or leave, as is?</p> <p><strong>UPDATE</strong></p> <p>Using this to see individual spaces [ assumes no '%' chars in file ]:</p> <pre><code>sed 's/ /%/g' filename.000 </code></pre> <p>Plan to build a script that analyzes each line's tab and space content.</p> <p>Using @shiplu's solution with a nod to the anti-cat crowd:</p> <pre><code>while read l;do echo $l;echo $((`echo $l | wc -c` - `echo $l | tr -d ' ' | wc -c`));done&lt;filename.000 </code></pre> <p>Still needs some tweaks for Windows but it's well on it's way.</p> <p><strong>SAMPLE TEXT</strong></p> <p>Key for reading:</p> <p>newlines marked with \n</p> <p>Carriage returns marked with \r</p> <p>Unknown space/tab characters marked with [:space:] ( need counts on those )</p> <pre><code>\r\n \n [:space:]Institution Anon LLC\r\n [:space:]123 Blankety St\r\n [:space:]Greater Abyss, AK 99999\r\n \n \n [:space:] 10/27/2011\r\n [:space:]Requested materials are available for pickup:\r\n [:space:]e__\r[:space:] D_ \r[:space:] _O\r\n [:space:]Bathtime for BonZo[:space:] 45454545454545[:space:] 10/27/2011\r\n [:space:]Bathtime for BonZo[:space:] 45454545454545[:space:] 10/27/2011\r\n \n \n \n \n \n \n [:space:] Pantz McManliss\r\n [:space:] Gibberish Ave\r\n [:space:] Northern Mirkwood, ME 99999\r\n ( untold variable amounts of \n chars go here ) </code></pre> <p><strong>UPDATE 2</strong></p> <p>Using IFS with read gives similar results to the ruby posted by someone below.</p> <pre><code>while IFS='' read -r line do printf "%s\n" "$line" | sed 's/ /%/g' | grep -o '%' | wc -w done &lt; filename.000 </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

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