6

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)

2 Answers 2

10

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
1
  • 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, 2019 at 15:25
5

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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