CAT/SED Regular Expresseion Group

in order to use some of the regular expressions in these examples remember to use the switch/option -E or -r to use extended regular expressions

Regular Expressions groups, the whole match will be in group 0 that can be referred by $0 or \0 depends on the programming/scripting language used for the regular expression, the first ( ) in the regular expression will be $1, the second ( ) $2 and so on.

here is an example of a text file with a list of names, and we want to change it from lastname, firstname to firstname, lastname

the regex match information picture if from https://regex101.com/ , where you can test your regex

Example 1

Caulkins, Thomas
Frazier, Crissy
Jourdan, Lavera
Birnbaum, Diedre
Briscoe, Yessenia  
cat example.txt | sed -E 's/(.*),[[:space:]](.*)/\2, \1/g'

Thomas, Caulkins
Crissy, Frazier
Lavera, Jourdan
Diedre, Birnbaum
Yessenia, Briscoe


Example 2

another example on a regex with groups

String: 00298 311234

regex: (\d+)[[:space:]](\d+)









Leave a Reply

Your email address will not be published. Required fields are marked *