A
A
Andrey Barbolin2016-06-01 22:42:16
Perl
Andrey Barbolin, 2016-06-01 22:42:16

How to find a word in a string and move or copy it to the beginning of the string?

We have Linux, a file with lines. It is necessary to copy or move the digital part separated by dots to the beginning of the line.
bla bla 23.33.72 bla
bla bla la na 45.34.33 bla en
bla la na 45.34.33 bla urjt To
be received
23.33.72 bla bla 23.33.72 bla
45.34.33 bla bla la na 45.34.33 bla en
41.65.32 bla la na 45.34.33 bla urjt

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
vaut, 2016-06-01
@dronmaxman

You can find numbers like this: [\d.]*
then you grab it with parentheses and add it as you like: by concatenation, replacement, or something else.
for example like this:
s/.*?([\d.]+).*/$1 $&/

A
abcd0x00, 2016-06-05
@abcd0x00

[[email protected] ~]$ text="\
> bla bla 23.33.72 bla
> bla bla la na 45.34.33 bla en
> bla la na 45.34.33 bla urjt
> "
[[email protected] ~]$ 
[[email protected] ~]$ echo -n "$text"
bla bla 23.33.72 bla
bla bla la na 45.34.33 bla en
bla la na 45.34.33 bla urjt
[[email protected] ~]$ 
[[email protected] ~]$ echo -n "$text" | sed 's/.* \([0-9]*\.[0-9]*\.[0-9]*\) .*/\1 &/'
23.33.72 bla bla 23.33.72 bla
45.34.33 bla bla la na 45.34.33 bla en
45.34.33 bla la na 45.34.33 bla urjt
[[email protected] ~]$

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question