LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   linux grep or awk command - how to search for string + execute (https://www.linuxquestions.org/questions/linux-newbie-8/linux-grep-or-awk-command-how-to-search-for-string-execute-900562/)

couta 09-01-2011 08:39 AM

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

colucix 09-01-2011 08:48 AM

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
do
  some stuff here
done < filename

To pass the extracted field to the command you need command substitution, that is:
Code:

while read line
do
  echo rm $(echo "$line" | awk '{print $3}')
done < filename

In your example the field is the 3rd, not the 2nd. In any case I put an echo statement in front of the rm command for testing purposes so that you can check the resulting commands before actually executing them. Hope this helps.

grail 09-01-2011 09:29 AM

Also you could do away with and do it all in bash:
Code:

while read _ _ line
do
  echo rm "$line"
done < filename


colucix 09-01-2011 09:38 AM

Or do it all in awk (calling bash to execute the rm command):
Code:

awk '{ print ("echo rm " $3) | "/bin/bash" }' filename

grail 09-01-2011 09:47 AM

@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

colucix 09-01-2011 09:55 AM

@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

couta 09-01-2011 10:41 AM

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

colucix 09-01-2011 11:49 AM

Some modification to the awk command:
Code:

awk '/LUN Path:/{ if ( $3 ~ /.+/ ) print | "echo rm " $3 }' filename
As you can see there is no need of grep, since awk can manage regular expressions as well. This code matches only the lines containing the "Lun Path:" string. The if statement inside the brackets takes care of non-empty fields (the command is executed if $3 matches any string of 1 or more characters). If you want to match more strictly the file name to remove, you have to refine the regular expression in the if statement.

Tinkster 09-01-2011 02:10 PM

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
The bash version executes in 0m0.001s, the awk one in 0m0.023s on my machine. ;}


Cheers,
Tink

grail 09-02-2011 12:05 AM

If just concerned about line having something you could also go with:
Code:

while read _ _ line; do [[ "$line" ]] && echo rm "$line"; done <  file

couta 09-03-2011 01:58 AM

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.