How to extract a substring using Regex

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
424
Points
18
I have a data like this

Code:
611556:WNYV:example1:173.44.62.120:7687954bf04f5acve4e047d6dcfa660a:v7B#4cfpKyui-vvR~"8%3pPz%-ulwO                
611543:ABurdick:example2:110.85.101.65:97da66b8e2aaa0801514d588bcb10253:(V7=HrBQWxV3tuddz+UMNqUMt6CGTS                
611582:CSleath:example3:107.155.116.101:1920163604e9642d482a011ed9351a7d:_ytt8b5yZD9sz~MMBIsi?Bj\il1HHU
I want to get example1, example2, example3 on per line. How can I do this using Regex?
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
[root@ ~]# cat preg.txt
611556:WNYV:example1:173.44.62.120:7687954bf04f5acve4e047d6dcfa660a:v7B#4cfpKyui-vvR~"8%3pPz%-ulwO
611543:ABurdick:example2:110.85.101.65:97da66b8e2aaa0801514d588bcb10253V7=HrBQWxV3tuddz+UMNqUMt6CGTS
611582:CSleath:example3:107.155.116.101:1920163604e9642d482a011ed9351a7d:_ytt8b5yZD9sz~MMBIsi?Bj\il1HHU

[root@ ~]# cat preg.txt | awk -F":" {'print $3'}
example1
example2
example3
[root@ ~]#
You can use above command as well in case you want to extract the field from shell
 

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
424
Points
18
Code:
[root@ ~]# cat preg.txt | awk -F":" {'print $3'}
This will overwrite into preg.txt file? if I want to save new data to a new file name?

Not sure which language you use, but example PHP you can foreach lines and get result to array:
I run this code

PHP:
<?php

$input_line='611556:WNYV:example1:173.44.62.120:7687954bf04f5acve4e047d6dcfa660a:v7B#4cfpKyui-vvR~"8%3pPz%-ulwO              
611543:ABurdick:example2:110.85.101.65:97da66b8e2aaa0801514d588bcb10253:(V7=HrBQWxV3tuddz+UMNqUMt6CGTS              
611582:CSleath:example3:107.155.116.101:1920163604e9642d482a011ed9351a7d:_ytt8b5yZD9sz~MMBIsi?Bj\il1HHU';

$array = preg_split('/:/', $input_line);

//print_r($array);

echo $array[2];

echo $array[7];

echo $array[12];

?>
$array[2] only gave example1

then $array[2], you have picked example1,2,3
How to run it with foreach to echo all example1, example2, example3 ?
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
This will overwrite into preg.txt file? if I want to save new data to a new file name?
No it won't..if you want the output in a new file use following

[root@ ~]# cat preg.txt | awk -F":" {'print $3'} > newfile.txt
 

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
424
Points
18
@mobin
[root@ ~]# cat preg.txt | awk -F":" {'print $3'}
What does it mean for this command? can you explain it in details? so i can apply with other patterns
[root@ ~]# cat preg.txt | awk -F":" {'print $3'} > newfile.txt
Sounds great, I must try it to see how it works. :)
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
mobin
cat preg.txt | - this open and send the file content to awk command
awk -F":" {'print $3'} - this separate each line using the separator : and print the 3rd column

based on the above example, you can easily extract other fields.
 

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
424
Points
18
Mujkanovic
3rd column
Which character decides is the 3rd column? this : ?
for example, with different database structure, how to know which column should I get?
 

mobin

Well-known member
Registered
Joined
Jun 22, 2017
Messages
234
Points
28
mobin
{'print $3'} defines the column..you can change $3 to $4 to get 4th column value. Its same like in your PHP script above,,you should know the column number against the separator.
 

Mujkanovic

Well-known member
Collaborate
Registered
Joined
Apr 24, 2016
Messages
424
Points
18
Mujkanovic
For exampe, this data
Code:
(143,    2,    '',    0,    'juicy',    'dbdf6d92aba8be1838c0ac48c4959622',    '2007-04-16',    '[email protected]',    0,    '',    '',    '',    '',    '',    1,    0,    'Newbies',    0,    1164906590,    0,    1175865657,    1176753268,    0,    0,    10,    5,    '0',    2,    0,    0,    0,    45112407,    '',    '0000-00-00',    -1,    -1,    '203.144.143.10',    0,    1,    '',    0,    0,    3,    17,    17,    '$QU',    '',    0,    0,    0,    0,    0,    0,    '',    0,    250,    255,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    0,    100,    1,    '',    1326351663,    0,    0,    0,    1,    1,    '',    '',    0,    '',    'vb',    '',    0,    1,    0,    0,    0,    0,    '0',    0,    0,    0,    '',    '',    0,    NULL),
How to know which column is containing [email protected]?
 

Nixtree

Well-known member
Registered
Joined
Jul 16, 2016
Messages
133
Points
28
Hope you are already got it figured by this time. Any ways I will just add a bit more for your reference

In your example, the only common which separates each is "," so we can use that.

$ cat test.txt

(143, 2, '', 0, 'juicy', 'dbdf6d92aba8be1838c0ac48c4959622', '2007-04-16', '[email protected]', 0, '', '', '', '', '', 1, 0, 'Newbies', 0, 1164906590, 0, 1175865657, 1176753268, 0, 0, 10, 5, '0', 2, 0, 0, 0, 45112407, '', '0000-00-00', -1, -1, '203.144.143.10', 0, 1, '', 0, 0, 3, 17, 17, '$QU', '', 0, 0, 0, 0, 0, 0, '', 0, 250, 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 100, 1, '', 1326351663, 0, 0, 0, 1, 1, '', '', 0, '', 'vb', '', 0, 1, 0, 0, 0, 0, '0', 0, 0, 0, '', '', 0, NULL),

$ cat test.txt |awk -F"," {'print $8'}

'[email protected]'

Hope you get the idea now. Please try with more and update if you need any more help
 
Older Threads
Replies
18
Views
4,883
Replies
4
Views
1,549
fwh
Replies
0
Views
1,060
Replies
2
Views
1,467
Latest Threads
Replies
0
Views
41
Replies
0
Views
63
Replies
1
Views
86
Replies
1
Views
98

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top