2

Let's assume that I already have a file created. This file creation comes from the execution of a command. The contents of the file will be up to 4 different lines looking something like this:

LUN Path:  servernam:/vol/server_xx_t1_01_223_04/mssql01/server_StorageSystemLevel100.lun
LUN Path:  servernam:/vol/server_xx_t1_01_225_04/mssql01/server_StorageSystemLevel100.lun
LUN Path:  servernam:/vol/server_xx_t1_01_226_04/mssql01/server_StorageSystemLevel100.lun
LUN Path:  servernam:/vol/server_xx_t1_01_228_04/mssql01/server_StorageSystemLevel100.lun

How can I pipe/awk/whatever this file so I can get the second string, i.e. "servernam:/vol/serverxxxx", and then run a command like:

rm  servernam:/vol/server_xx_t1_01_228_04/mssql01/server_StorageSystemLevel100.lun

So I want to pull the right string out of the file and include it as an argument to a command. Note that I would need to do the same execution command against all 4 different strings. A script example, or even better all from the command line, would be much appreciated.

6

ls $(awk '{print $3}' /path/to/your/file)

check if this works as expected and then replace ls with rm

  • As long as there will never be spaces in the file name...+1 – Peter.O Sep 1 '11 at 14:44
  • This will resolve any whitespace issue. awk '{sub(/^LUN Path: /, ""); print; }' – Peter.O Sep 1 '11 at 14:59
  • @fred No, that's not enough: any whitespace and globbing characters will end up parsed by the shell. This script works only if there are no whitespace or globbing characters in the file names anyway. It could easily be fixed by adding backslashes before problematic characters before printing. – Gilles Sep 1 '11 at 23:11
  • thanks a lot for your responses. that got what I wanted. – user10414 Sep 3 '11 at 6:59
0
echo rm $(sed -n -e 's/[^A-Za-z0-9]/\\&/g' -e 's/LUN Path:  *//p')

The first s command adds a backslash before every character that isn't a newline, letter or digit. The second command strips the LUN Path: prefix from the lines that have it, and print out only the lines that have the prefix. The resulting string undergoes filename expansion and word splitting in the shell; since all problematic characters except newlines have been escaped, the proper file names will be passed to the command.

  • I didn't get this regex. Where is new-line handled in [^A-Za-z0-9] part? – Abhishek A Sep 2 '11 at 13:00
  • @greenmang0 Sed processes its input line by line. The regexp is matched against the line body. Since there is one file per line in the input, there is still one file per line in the output. – Gilles Sep 2 '11 at 19:32
0
cut -d: -f3 file | xargs rm

The cut command will pick out the third :-delimited field from the file (the filename after servernam:) while the xargs command would read these newline-delimited paths and feed them to rm.

If you want servernam: to be part of the actual path, then replace the cut command by awk '{ print $NF }' which will extract the last whitespace-delimited field on each line.

This using cut assumes that there is no : in the pathnames, and using awk assumes that the paths does not contain whitespace.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.