-1

This is output of the grep -E "scsi0:" *.vmx | grep -E "fileName" command in one of my directory:

scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "P120_vmname_2.vmdk"
scsi0:4.fileName = "P120_vmname_3.vmdk"

I need to rewrite above output inside that vmx file so it looks like following

scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "vmname_3.vmdk"
scsi0:4.fileName = "vmname_4.vmdk"

So in essence the script needs to the following:

  • look for the line which contains scsi0 and filename and remove everything after double quote before vmname
  • check what is the number in that line after "scsi0:" and add/replace that number after the underscore, so P120_vmname_2 becomes vmname_3

The thing is that there can by any number of characters before vmname which need to be removed and lines which need to be fixed can be anywhere in the source file.

Do I need to assign individual line output to separate variable or it is possible to manipulate them with just one?

Thanks

Invisible999
  • 547
  • 2
  • 5
  • 16
  • Please avoid *"Give me the codez"* questions. Instead show the script you are working on and state where the problem is. Also see [How much research effort is expected of Stack Overflow users?](https://meta.stackoverflow.com/q/261592/608639) – jww Aug 24 '18 at 10:13
  • I'm voting to close this question as off-topic because it belongs to https://serverfault.com – Martin Zeitler Aug 24 '18 at 11:53

1 Answers1

0

Could you pipe your grep output into sed?

If so, you could do a substitution like this:

sed -r 's/^(.*:)([[:digit:]]+)(.*) = \".*vmname_[[:digit:]]+/\1\2\3 = \"vmname_\2/'
#           ^        ^         ^       ^             ^          ^              ^
#           |        |         |       |             |          |              Replace number.
#           |        |         |       |             |          Re-create line start
#           |        |         |       |             Match incorrect number                        
#           |        |         |       Match garbage before "vmname"
#           |        |         Match Filename
#           |        Correct number
#           Line start

So your actual command would look like this:

grep -E "scsi0:" *.vmx | grep -E "fileName" | \
sed -r 's/^(.*:)([[:digit:]]+)(.*) = \".*vmname_[[:digit:]]+/\1\2\3 = \"vmname_\2/'

Converts this:

scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "P120_vmname_2.vmdk"
scsi0:4.fileName = "P120_vmname_3.vmdk"
scsi0:5.fileName = "P12asdasdsada_asdasd_sdsad0_vmname_3.vmdk"
scsi0:6.fileName = "vmname_3.vmdk"

To this:

scsi0:0.fileName = "vmname.vmdk"
scsi0:1.fileName = "vmname_1.vmdk"
scsi0:2.fileName = "vmname_2.vmdk"
scsi0:3.fileName = "vmname_3.vmdk"
scsi0:4.fileName = "vmname_4.vmdk"
scsi0:5.fileName = "vmname_5.vmdk"
scsi0:6.fileName = "vmname_6.vmdk"
Karl Nicoll
  • 16,090
  • 3
  • 51
  • 65
  • Karl, super! Now I just need to figure out how to put ls *.vmx | sed -e 's/\.vmx$//' output (which is the filename without extension) instead of "vmname" in the command above and we are done. :) – Invisible999 Aug 24 '18 at 10:14
  • @Invisible999 Could you put it into a variable in bash before you call grep/sed? If so, you could just use [parameter substitution](http://tldp.org/LDP/abs/html/parameter-substitution.html) to put it in the `sed` query instead of `"vmname"`. You'd need to change the quotes around my sed command though since single quotes won't allow the substitution. – Karl Nicoll Aug 24 '18 at 10:21
  • 1
    Yes. Above in one command looks uberpro and fancy, but would more difficult to understand by addition more content and easy to make errors. By having everything in variables and have script to call - much more manageable and understandable. Thanks again. – Invisible999 Aug 24 '18 at 10:32
  • This doesn't do what the OP requested -- i.e. leave all other lines the same. – ivan_pozdeev Aug 24 '18 at 11:59