Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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

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

share|improve this answer
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
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.

share|improve this answer
I didn't get this regex. Where is new-line handled in [^A-Za-z0-9] part? – greenmang0 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
thanks a lot for your answers. pb has been fixed. – user10414 Sep 3 '11 at 7:00

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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