- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- How to fasten nested "for" loops
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2011 03:40 PM
08-19-2011 03:40 PM
Hey All,
I have a script/command which has a for loop nested inside of another for loop -
for i in `cat file1`; do echo "$i******"; for j in `cat file2`; do command $i arg1 |grep $j; done; done
file1
APP1
APP2
..
file2
library1
library2
..
command = libinfo APP1
output of the command list all libraries for APP1, but I want to grep for only the ones given in file2.
How can fasten this up?
Thanks,
Allan.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2011 05:17 PM
08-19-2011 05:17 PM
Re: How to fasten nested "for" loops
@allanm77 wrote:I have a script/command which has a for loop nested inside of another for loop -
for i in `cat file1`; do echo "$i******"; for j in `cat file2`; do command $i arg1 |grep $j; done; done
...output of the command list all libraries for APP1, but I want to grep for only the ones given in file2.
How can fasten this up?
I suppose by "fasten this up" you mean "make faster".
That said, you could use :
#!/usr/bin/sh while read i do echo "$i******" command $i arg1 | grep -f file2 done < file1
Look at the manpages for 'grep'. With '-f pattern_file' the regular expression (grep and grep -E) or strings list (grep -F) is taken from the pattern_file.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-19-2011 05:39 PM - edited 08-20-2011 06:05 PM
08-19-2011 05:39 PM - edited 08-20-2011 06:05 PM
Solution>I suppose by "fasten this up" you mean "make faster".
Or "speed up". :-)
>With '-f pattern_file'
Right, using vector methods.
Also, you don't have to use while, for loops also take infinite args but they all have to be read into memory:
for i in $(< file1); do
echo "$i******"
command $i arg1 | grep -f file2
done
If command takes a list of arguments, you may be able to invoke command once as in nm(1):
nm -pxAN $(< file1) | fgrep -f file2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2011 10:21 AM
08-20-2011 10:21 AM
Re: How to fasten nested "for" loops
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-20-2011 10:30 AM
08-20-2011 10:30 AM
Re: How to fasten nested "for" loops
Hi (again) :
And what was wrong with the substance of the answer I provided --- the use of 'grep -f' ?
Both of us, too, eliminated your useless 'cat'. Did you notice that either?
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-24-2011 10:20 AM
08-24-2011 10:20 AM