- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Re: string substituion in awk
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
02-14-2006 11:17 AM
02-14-2006 11:17 AM
awk '/testbed/ {real=$NF; soft=$(NF-2)}\
{
print real,soft
if ( real ~ /c6.4.4.x/) {
newreal=s,'c6.4.4.x,new644,
fi
}
I knwo the new real part is wrong..but I want to do string substituion in awk, is it possible?
Mimosa
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2006 11:37 AM
02-14-2006 11:37 AM
SolutionSee the manpages for 'awk'. You could use the 'sub' command --- something like this:
# cat .myawk
#!/usr/bin/awk -f
/testbed/ {real=$NF; soft=$(NF-2)}\
{
print real,soft
if ( real ~ /c6.4.4.x/) {
sub(/c6.4.4.x/,"new644",$0)
}
}
Use like:
# echo "testbed has a value of c6.4.4.x " | ./myawk
...returns:
c6.4.4.x value
testbed has a value of new644
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-14-2006 04:06 PM
02-14-2006 04:06 PM
Re: string substituion in awk
Why don't you give us some real (sic) sample input lines. Just a few, as a .txt attachment and as text to a next reply. Also please show the desired output.
For example, I am wondering whether you only want to deal with c6.4.4.x, or whether you perhaps want a generic function changing aX.Y.X.c into newXYZ. Would c1.2.3. y need to become new123?
If so, then you probably want to 'split' the field and glue the pieces back together. Example for that:
# awk '{ real=$0; split(real,array,"[^0-9]+"); new = "new" array[2] array[3] array[4]; print new}'
c6.4.4.x
new644
test12.345.67.xxxx
new1234567
The "[^0-9]+" is a regular expression to split on, using any series of non-digits
The [0-9] being digits 0 thru 9
The ^ meaning NOT
and the + indicating 'one or more'
hth,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2006 10:46 AM
02-15-2006 10:46 AM
Re: string substituion in awk
thank you..
btw..
if I have something like
path='pwd'
awk '{
if ($path ~ 644)
print "get it"
}'
that doesnt work...because path was defined outside of awk...how do I use path?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2006 10:59 AM
02-15-2006 10:59 AM
Re: string substituion in awk
Here's two ways (by example) to pass variables into an awk script:
# awk -v PATH=Gemini 'BEGIN{print PATH}'
# awk 'END{print PATH}' PATH=Gemini /dev/null
On HP-UX either syntax will work.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2006 01:36 PM
02-15-2006 01:36 PM
Re: string substituion in awk
Fine answer by JRF, but please also consider the awk (and perl) "Built-in Variables that Convey Information" (man page) to do exactly what you need without the extra step:
Specific example: ENVIRON["PWD"]
You can pick up any exported variable that way:
# x=test
# export x
# awk 'END{print ENVIRON["x"]}' /dev/null
btw... i would advise against export a variable named 'path'.... too confusing.
Enjoy,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2006 04:51 PM
02-15-2006 04:51 PM
Re: string substituion in awk
awk -v var="information" '{ .... }'
or
awk '{ .... }' var="information"
Note: You can define only one variable in second method.
Else set a environment variable as,
# export VAR="Information"
Use in awk with ENVIRON["variable"] will help out.
You can use perl as well as for this.
--
Muthu
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2006 08:54 PM
02-15-2006 08:54 PM
Re: string substituion in awk
path=644
%echo $path|awk '{if ($0 ~ 644) print "get it"}'
get it
HTH,
Art
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-15-2006 11:24 PM
02-15-2006 11:24 PM
Re: string substituion in awk
What you do with your time is of course none of my concern. However, it does create clutter and is does bring a topic which I was 'following' back to the top, making me waste my time.
Sorry to 'pick' on you, it is not really personal as this happens time and again with mutliple contributors. I do appreciate your contributions in general!
Best regards,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
02-16-2006 04:58 AM
02-16-2006 04:58 AM
Re: string substituion in awk
Hein's point regarding gaining the values of (ENV)ironmental variables directly is the more appropriate choice.
For the record, it *is* indeed possible to pass multiple variable values into an awk script despite what a post above would suggest:
# awk 'END{print "I am",FIRST,"and I am",LAST}' FIRST=alpha LAST=omega /dev/null
# awk -v FIRST=alpha -v LAST=omega 'END{print "I am",FIRST,"and I am",LAST}' /dev/null
*Either* form above will produce the output:
I am alpha and I am omega
ALSO: Your choice of the two methods I suggested is also influenced when/if you use BEGIN and END blocks in 'awk'.
# awk 'BEGIN{print "I am",FIRST,"and I am",LAST};END{print "ok"}' FIRST=alpha LAST=omega /etc/hosts
...prints:
I am and I am
ok
...whereas:
# awk 'END{print "I am",FIRST,"and I am",LAST};END{print "ok"}' FIRST=alpha LAST=omega /etc/hosts
...prints:
I am alpha and I am omega
ok
Yet, either:
# awk -v FIRST=alpha -v LAST=omega 'END{print ...
(or)
# awk -v FIRST=alpha -v LAST=omega 'BEGIN{print ...
...produces the desired:
I am alpha and I am omega
ok
Regards!
...JRF...