-1

New to Linux commands.. What syntax would I use to find and replace a string with each line starting with "Data Centre:" Address information that is different on every line" but ends with the word Site: and I need to keep the site details that follow..

Example:

Input.txt
Data Centre: 23 Mulberry lane, Washington Site: 9067 Green

Expected Output
Site: 9067 Green

I've found gsub examples, but gsub is not available on my OS.

Any help appreciated.

Mac

pa4080
  • 30,621
majtx
  • 3

2 Answers2

3

If you use semicolon as field separator, what you want will be in the last field

awk -F ':'  '/Site:/ { print "Site: ", $NF }'
3

Here is a sed solution that uses extended regular expressions -r and capture groups ()->\1:

sed -r 's/^Data Centre:.*(Site: .*)$/\1/' Input.txt

If you want to suppress the output of any other lines that could appear in the file:

sed -n -r 's/^Data Centre:.*(Site: .*)$/\1/p' Input.txt

Add the option -i.bak to do the changes and backup file simultaneously, ot redirect the output to new file.

pa4080
  • 30,621