title: grep: match all characters up to (not including) first blank space
body: <p>I have a text file that has the following format:</p>
  
  <pre><code>characters(that I want to keep) (space) characters(that I want to remove)
  </code></pre>
  
  <p>So for example:</p>
  
  <pre><code>foo garbagetext
  hello moregarbage
  keepthis removethis
  (etc.)
  </code></pre>
  
  <p>So I was trying to use the grep command in Linux to keep only the characters in each line up to and not including the first blank space. I have tried numerous attempts such as:</p>
  
  <pre><code>grep '*[[:space:]]' text1.txt &gt; text2.txt
  grep '*[^\s]' text1.txt &gt; text2.txt
  grep '/^[^[[:space:]]]+/' text1.txt &gt; text2.txt
  </code></pre>
  
  <p>trying to piece together from different examples, but I have had no luck. They all produce a blank <code>text2.txt</code> file. I am new to this. What am I doing wrong?</p>
  
  <p>*EDIT:</p>
  
  <p>The parts I want to keep include capital letters. So I want to keep any/all characters up to and not including the blank space (removing everything from the blank space onward) in each line.</p>
  
  <p>**EDIT:</p>
  
  <p>The garbage text (that I want to remove) can contain anything, including spaces, special characters, etc. So for example:</p>
  
  <pre><code>AA rough, cindery lava [n -S]
  </code></pre>
  
  <p>After running <code>grep -o '[^ ]*' text1.txt &gt; text2.txt</code>, the line above becomes:</p>
  
  <pre><code>AA
  rough,
  cindery
  lava
  [n
  -S]
  </code></pre>
  
  <p>in text2.txt. (All I want to keep is <code>AA</code>)</p>
  
  <hr>
  
  <p>SOLUTION (provided by Rohit Jain with further input by beny23):</p>
  
  <pre><code> grep -o '^[^ ]*' text1.txt &gt; text2.txt
  </code></pre>