-2

I want to sort my database by regex, but don't know exactly how to do it.

The editor I use is Atom

It's example of input (the entire file has 10,000 lines)

x1.83
30.12.2018, 12:05:29
1386834
30
x2.66
30.12.2018, 12:05:01
1386833
30
x2.12
30.12.2018, 12:04:36
1386832
30
x2.51
30.12.2018, 12:04:09
1386831
30
x2.19
30.12.2018, 12:03:44
1386830
30
x1.04
30.12.2018, 12:03:31
1386829
30
x4.18

What I want, as a output:

x1.83
x2.66
x2.12
x2.51
x2.19
x1.04
x4.18

Can someone help me with this ?

P.S. On the other hand - is it possible to copy values from specific class by chrome inspector ?

closed as off-topic by D. Ben Knoble, akshay, B Layer, Hotschke, jamessan Jan 2 at 17:17

  • This question does not appear to be about Vi and Vim family of editors, within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • cat myfile | grep x :-) or cat my file | grep ^x – The Unix Janitor Dec 30 '18 at 13:42
  • Sorry, but I don't know what to do with this. I'm using this tool for regex: imgur.com/a/yyZ9aGy – Yashe Dec 30 '18 at 14:16
  • 4
    I'm voting to close this question as off-topic because, as written, this editing task concerns the Atom editor and not vi/vim. – D. Ben Knoble Dec 30 '18 at 15:38
  • 1
    @TheUnixJanitor grep x file, no useless cats ;) – D. Ben Knoble Dec 30 '18 at 15:39
  • 1
    Should be closed as off topic as it targets Atom, not Vi/Vim. – Ralf Dec 30 '18 at 16:10
3

The Vim way

Basically, you are trying to delete lines which starts with numbers and to select only those lines you can use a simple regex like this: ^\d.\+$

You can find the matches by prefixing the regex with a /, i.e, /^\d.\+$. This will only match the lines starting with a number, you can delete those lines with

:g/^\d.\+$/d

This will produce your preferred output. For more info see :help :global and :help pattern.txt

In command line you can directly do this without opening vim:

vim datafile -es +"g/^\d.\+$/d" +"w\! new_datafile" +"qa\!"

Other ways

sed way : sed -nre '/^x[0-9]+\.[0-9]+$/p' datafile
grep way: grep -v "^[0-9].*$" datafile
awk way : awk '/^x[0-9]+\.[0-9]+$/' datafile
perl way: perl -ne "print if /^x[0-9]+\.[0-9]+$/" datafile

To save it I usually redirect the output to a new file ( there is also in-place editing mode you can check ), like this cmd > new_datafile

  • Thank you very much, it made my work a lot easier. For others - In Atom you have to write this: ^\d.+$ – Yashe Dec 30 '18 at 15:30

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