0

I am dealing with a long data in the vim file. I want to separate the positive values and negative values in the 3rd column in separate files.

Example:

1.000 3.889  4.666

4.889 2.809 -8.687

3.896 4.797  2.808

2.797 4.098 -0.458

Now I want to have two separate files based on the positive and negative values in 3rd column.

I use an awk command as

awk '$3 ~ /\-/ { print $0 }' chh.dat1

but it is not working.

2
  • It's not clear if you want to do this at the command line in awk or if you want to do it within vim. Aug 25, 2021 at 15:47
  • 2
    Welcome to Vi and Vim! I’m not clear on what your desired output looks like, nor what « it’s not working » means. Please edit. (I’ve cleaned up some formatting, but I left in some linebreaks whose origin I wasn’t sure of. If they were markdown artifacts, please remove them; if they were part of the example text, leave them in.)
    – D. Ben Knoble
    Aug 25, 2021 at 15:59

1 Answer 1

1

Using vim I would do it with 2 steps

put all lines with negatives on third column down

:g/^\(.*\s\+\)\{2}-\d/m$

Find all lines that has third column (space delimited) starting with - and some digit. Move that line at the end of buffer.

write them to a separate file

vip:w ~/newfile.dat

Select a paragraph and write selection to the ~/newfile.dat.

remove all negatives from current file

gvd

Reselect and delete selection.

PS, although sed or awk might be a better choice for the task.

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.