3

I have a file (around 1.6 million lines) which formatted like this:

-0.00697468 0 -1.95966e-49 101325 -3.06381  
-0.00697468 0 -1.95966e-49 101325 -3.06381  
-0.00697468 0 -1.95966e-49 101325 -3.06381  
. . .  

After some thousand lines, the format will be changed (no minus sign on the first character), like this:

0.453945 -2.14126e-54 3.40152e-49 101325 214.355  
0.453945 -2.14126e-54 3.40152e-49 101325 214.355  
0.453945 -2.14126e-54 3.40152e-49 101325 214.355  
. . .

So, how to delete the entire line, after the first whitespace on every line? I have tried to search but couldn't get any answer that I want. Thanks! (Note: some lines have different number of character, hence that I cannot simply delete some characters from the end of the line on every line)

5

The two commands below will reduce every line to the first sequence of non-whitespace character followed by a space:

from
0.453945 -2.14126e-54 3.40152e-49 101325 214.355

to
0.453945 <-- space

But your question is a little confusing so I'm not sure I understood what you want.

With a substitution:

:%s/\(\S\+\s\).*/\1

With a macro:

:%normal! WD
  • and if you want to delete those trailing single whitespaces left behind on every line, use :%s/ *$// <- yes, that is a single whitespace character between / and * – lobi Oct 18 at 15:25
3

If you wanted to delete entire line starting with a space, This pattern will be useful.

:g/^\s.*/d
  • g -global
  • ^ - start of the line
  • \s - space
  • .* - anything after that
  • d - delete

If you want the starting whitespace to remain, but delete the remaining content, you can use,

:%s/^\(\s\+\).*/\1/g
  • First one doesn't work, get "Pattern not found: ^\s.*" using both vim/vi – lobi Oct 18 at 15:19
  • It works for me. Why did you add quotes at the end? – SibiCoder Oct 19 at 21:26
  • that quote at the end was due to my sentence. first quote opens before Pattern – lobi Oct 20 at 21:02
  • try ':g/^ .*/d'. you could replace \s with space itself and try – SibiCoder Oct 21 at 15:35
  • also does not work, however this does: :%s/ .*// – lobi Oct 22 at 17:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.