File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
A friendly place for programming greenhorns!
Big Moose Saloon
Register / Login
Linux / UNIX
use of variables in awk to search for pattern from a file
Joined: Dec 21, 2010
Nov 23, 2011 22:26:11
I need to extract all the content between two strings stored in two variables "startstring" and "endstring"
startstring=Hello enstring=world #not working awk '/$startstring/, $NF ~ /$endstring/ ' errors.log > /tmp/errorlogs/errorfile2
The above code is not working with variables. It works when actual
values are used as
#working awk '/hello/, $NF ~ /world/ ' errors.log > /tmp/errorlogs/errorfile2
Can someone help me to use these two variables in awk.
Joined: Jun 25, 2001
Nov 28, 2011 05:24:09
Unix shells have several different "quote" characters, and each one has a special meaning. For example, the backtick "quotes" cause the quoted expression to be run as a shell command.
Unfortunately, the single-quote "quotes" mean "don't expand shell variables". Use the double-quote "quotes" instead. Or, in other words:
awk "/$startstring/, $NF ~ /$endstring/ " errors.log > /tmp/errorlogs/errorfile2
It's a convention that environment variables should be named all in uppercase, BTW. However, that's just a convention, so using lower-case names won't actually hurt anything.
An IDE is no substitute for an Intelligent Developer.
I agree. Here's the link:
subject: use of variables in awk to search for pattern from a file
can variables be used as pattern for awk command?
Search on a string
Column number of a search pattern
Expression to get Tomcat pid?
EJB findall method does not find persisted objects
All times are in JavaRanch time: GMT-6 in summer, GMT-7 in winter
| Powered by
Copyright © 1998-2015