How to extract a substring using Regex

Mujkanovic

Well-known member
Joined
Apr 24, 2016
Messages
279
Best answers
1
Ratings
14
Points
18
#1
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?
 

Malware.Expert

Corporate Member
Corporate Member
Joined
Jun 18, 2018
Messages
55
Best answers
1
Ratings
39 6
Points
18
#2

mobin

Well-known member
Joined
Jun 22, 2017
Messages
212
Best answers
0
Ratings
85 1
Points
28
#3
[[email protected] ~]# 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

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

Mujkanovic

Well-known member
Joined
Apr 24, 2016
Messages
279
Best answers
1
Ratings
14
Points
18
#4
Code:
[[email protected] ~]# 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
Joined
Jun 22, 2017
Messages
212
Best answers
0
Ratings
85 1
Points
28
#7

Mujkanovic

Well-known member
Joined
Apr 24, 2016
Messages
279
Best answers
1
Ratings
14
Points
18
#8

mobin

Well-known member
Joined
Jun 22, 2017
Messages
212
Best answers
0
Ratings
85 1
Points
28
#9
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
Joined
Apr 24, 2016
Messages
279
Best answers
1
Ratings
14
Points
18
#10
Mujkanovic
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
Joined
Jun 22, 2017
Messages
212
Best answers
0
Ratings
85 1
Points
28
#11
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
Joined
Apr 24, 2016
Messages
279
Best answers
1
Ratings
14
Points
18
#12
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
Joined
Jul 16, 2016
Messages
127
Best answers
0
Ratings
42 4
Points
28
#13
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
16
Views
351
Replies
2
Views
57
Replies
0
Views
65
Replies
1
Views
72

Latest postsNew threads

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