Saturday, February 5, 2022

[SOLVED] Bash: sed/grep between patterns (including only first pattern)

Issue

I want to extract all substrings starting with "[A-Z]:" and ending with "[A-Z]:" or the end of the string

so that if my input is

C:\Users\Alex Sim\Libraries\Some Folder D:\Hello world\Something C:\Ran out of names\whatever

my output would be

C:\Users\Alex Sim\Libraries\Some Folder
D:\Hello world\Something
C:\Ran out of names\whatever

Solution

Simply with GNU sed:

sed 's/[[:space:]]\([A-Z]:\)/\n\1/g' input.txt

The output:

C:\Users\Alex Sim\Libraries\Some Folder
D:\Hello world\Something
C:\Ran out of names\whatever


Answered By - RomanPerekhrest
Answer Checked By - David Goodson (WPSolving Volunteer)