LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-01-2011, 08:39 AM   #1
couta
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Rep: Reputation: Disabled
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
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-01-2011, 08:48 AM   #2
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 09-01-2011, 09:29 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
Also you could do away with and do it all in bash:
Code:
while read _ _ line
do
  echo rm "$line"
done < filename
 
2 members found this post helpful.
Old 09-01-2011, 09:38 AM   #4
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
Or do it all in awk (calling bash to execute the rm command):
Code:
awk '{ print ("echo rm " $3) | "/bin/bash" }' filename
 
1 members found this post helpful.
Old 09-01-2011, 09:47 AM   #5
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
@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
 
2 members found this post helpful.
Old 09-01-2011, 09:55 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
@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
 
Old 09-01-2011, 10:41 AM   #7
couta
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
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
 
Old 09-01-2011, 11:49 AM   #8
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978Reputation: 1978
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.
 
Old 09-01-2011, 02:10 PM   #9
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910Reputation: 910
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
 
Old 09-02-2011, 12:05 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,496

Rep: Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867Reputation: 2867
If just concerned about line having something you could also go with:
Code:
while read _ _ line; do [[ "$line" ]] && echo rm "$line"; done <  file
 
Old 09-03-2011, 01:58 AM   #11
couta
LQ Newbie
 
Registered: Sep 2011
Posts: 7

Original Poster
Rep: Reputation: Disabled
thanks for the answers

thanks for everyones time and input. You've been great help !
pb solved.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] Using grep / awk to search for coloured text s7upify Linux - General 5 09-21-2010 11:00 AM
Sed/awk/grep search for number string of variable length in text file Alexr Linux - Newbie 10 01-19-2010 01:34 PM
grep, sed, awk or tr - searching words in a string hal8000b Programming 2 03-06-2009 08:04 PM
Sed, Awk, grep,Search,delete joyds219 Linux - Newbie 6 04-03-2008 06:15 AM
use grep and execute a command if string is found plisken Linux - General 6 07-28-2005 11:06 AM


All times are GMT -5. The time now is 06:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration