linux grep or awk command - how to search for string + execute
Hi all,
First time poster, and really hoping you'd be able to help me, as I'm stuck & hope Im posting this in right forum. let's assume that I already have a file created. This file creation comes from the execution of a command. 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 My question is: How am I able to pipe, awk or whatever method, thus being able to use the "2nd string of the file", ie " servernam:/vol/serverxxxx", and run a command like, for example rm servernam:/vol/server_xx_t1_01_228_04/mssql01/server_StorageSystemLevel100.lun so basically filtering for the right string and execute a command against it. Note that I would need to do the same execution command against all 4 different strings. a script example / experiences or even better all from the shell command line, would be muchly appreciated. cheers |
Hi and welcome to LinuxQuestions! If you want to execute a command over all the lines in the text file, you basically need a loop fed with the content of the file:
Code:
while read line Code:
while read line |
Also you could do away with and do it all in bash:
Code:
while read _ _ line |
Or do it all in awk (calling bash to execute the rm command):
Code:
awk '{ print ("echo rm " $3) | "/bin/bash" }' filename |
@colucix - you probably already know but just thought I would mention that if you move the pipe to just after print it will make your current shell execute the commands :)
Code:
awk '{print | "echo rm "$3"}' filename |
@grail: actually I always forget about all the print variants and their subtle differences. Thanks for that. Just a little correction to your code (you wrote an extra " after $3):
Code:
awk '{ print | "echo rm " $3 }' filename |
thanks.. but a couple more questions
Hi again,
many thanks for the answers which have helped immensely , but I'd like to be clear and hopefully make what I gotta do easier..hope you can still assist.. my "raw file" ( not the one I mentioned before, has lots of info, so what I'd like to do is actually grep the LUN Path: the information that I need is indeed on the 3rd slot, so would like to find out how to use the grep and awk command together ( preferably in one or 2 shell commands ).. Also and this may be a little more challenging, is if the 3rd slot is empty /blank, then the actual post command that I'd need to do, should be ignored completely.. is it possible to do this? thanks again |
Some modification to the awk command:
Code:
awk '/LUN Path:/{ if ( $3 ~ /.+/ ) print | "echo rm " $3 }' filename |
And while awk is blazing fast, and little, the shell is
already running ... building up on grails example: Code:
while read _ _ line; do if [[ "$line" =~ server ]]; then echo rm "$line"; fi; done < file Cheers, Tink |
If just concerned about line having something you could also go with:
Code:
while read _ _ line; do [[ "$line" ]] && echo rm "$line"; done < file |
thanks for the answers
thanks for everyones time and input. You've been great help !
pb solved. |
All times are GMT -5. The time now is 07:09 PM. |