- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- Awk Variable Substition
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
12-11-2006 01:30 AM
12-11-2006 01:30 AM
Just want to strip everything prior to the last backslash for a paramter in a file using awk.
i.e.
cat file | awk ' BEGIN { FS="\n" ; RS="" } { print $5 } '
Test\SQL Servers\Server1
Edinburgh\SQL Servers\Server2
London\SQL Servers\Server3
Test Servers\Server4
to be
Server1
Server2
Server3
Outside of awk I know i can do ${variable##*\\}
but not sure how to get it to work alongside awk and everything I see appears to work on the first occurence i.e.
awk ' BEGIN { FS="\n" ; RS="" } { print substr($5,(index($5, "\\")+1),20) } '
works on the first occurence of "\" but doesn't handle any further occurences
I'm sure I'm missing something simple here so please let me know ;-)
Cheers
Solved! Go to Solution.
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 01:47 AM
12-11-2006 01:47 AM
Re: Awk Variable Substition
you just want the last part?
awk -F\\ '{print $NF }' infile
regards,
John K.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 01:48 AM
12-11-2006 01:48 AM
Re: Awk Variable Substition
In Perl, dimply:
# perl -nle 'print $1 if m/.*\\(.*)/' filename
Regards!
...JRF...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 01:52 AM
12-11-2006 01:52 AM
Re: Awk Variable Substition
Thanks for the perl solution but would like to use awk if possible
Cheers
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 02:06 AM
12-11-2006 02:06 AM
Re: Awk Variable Substition
OK, then in 'awk':
# awk '{sub(/.+\\/, "",$0);print}' filename
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 02:45 AM
12-11-2006 02:45 AM
Re: Awk Variable Substition
# basename London\SQL Servers\Server3
LondonSQL
Easiest awk would be:
awk -F'\' '{print $NF}' filename
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 02:52 AM
12-11-2006 02:52 AM
Re: Awk Variable Substition
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 02:56 AM
12-11-2006 02:56 AM
Re: Awk Variable Substition
I am trying to do something with other parameters and formatting the input so ideally would like something to be able to do something all in one line
i.e.
| awk ' BEGIN { FS="\n" ; RS="" } { print $1 ", " $2 ", " $3 ", "
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 03:47 AM
12-11-2006 03:47 AM
Re: Awk Variable Substition
just split the filed in request using '\' as seperator:
awk 'BEGIN { FS="\n" ; RS="" }
{n=split($5,a,"\\"); if(n>1) print a[n]}' file
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 04:01 AM
12-11-2006 04:01 AM
Solutionsorry for the follow-up, but I missed your last post.
To include my solution into in your code, use
the splitted result directly. If you set ORS, then there is no need to diddle with output formatting:
| awk ' BEGIN { FS="\n" ; RS=""; ORS=" " }
{ n=split($5,a,"\\"); print $1,$2,$3,a[5],$6}'
mfG Peter
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 08:27 PM
12-11-2006 08:27 PM
Re: Awk Variable Substition
cat file.out | awk -F\\ '{print $NF } ' | awk ' BEGIN { FS="\n" ; RS="" } { print $1", "$2", "$3", "$4", "$5" "$7" "$8} '
Hope someone else finds a use for this !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-11-2006 08:29 PM
12-11-2006 08:29 PM