ProgrammedChem
ProgrammedChem

Reputation: 157

What is this command doing? sed

I was given a general set of commands used as an example for something I will have to perform:

1 cat /etc/dsh/machines.list | sed -e "s|\(.*\)|sshfs \1:/opt/dextor/cloud \1|"
2 cat /etc/dsh/machines.list | sed -e "s|\(.*\)|sshfs \1:/opt/dextor/cloud \1|" | sh
3 cat /etc/dsh/machines.list | sed -e "s|\(.*\)|cp run.sh \1/|" 
4 cat /etc/dsh/machines.list | sed -e "s|\(.*\)|cp run.sh \1/|" | sh

What exactly is this doing?

I understand that the contents of machines.list are being passed to sed but what is the sed command doing?

Thank you!

Upvotes: 0

Views: 154

Answers (1)

shivams
shivams

Reputation: 2717

I will explain cat /etc/dsh/machines.list | sed -e "s|\(.*\)|cp run.sh \1/|" to you and rest all are same.

1) cat will read lines from machines.list and pass that to sed.

2) sed is doing basically a match and replace operation.

| is used as a separator, it could be any character, and it's usually /. It basically work like this |match this|replace with this|.

You are matching \(.*\) which basically is telling sed to match everything in line. . matches to every character and * means to match that character any number of times. Using () will save that match in \1.

And in replace section you are replacing that with the text cp run.sh \1/ and \1 will be contain whatever is matched.

Also i do think there is no need to use cat at all. Better use it this way sed -e "s|\(.*\)|cp run.sh \1/|" /etc/dsh/machines.list

Upvotes: 1

Related Questions