Operating System - Linux
1828220 Members
2146 Online
109975 Solutions
New Discussion

Re: Awk Variable Substition

 
SOLVED
Go to solution
gstonian
Trusted Contributor

Awk Variable Substition

Hey,

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
11 REPLIES 11
john korterman
Honored Contributor

Re: Awk Variable Substition

Hi,

you just want the last part?

awk -F\\ '{print $NF }' infile

regards,
John K.
it would be nice if you always got a second chance
James R. Ferguson
Acclaimed Contributor

Re: Awk Variable Substition

Hi:

In Perl, dimply:

# perl -nle 'print $1 if m/.*\\(.*)/' filename

Regards!

...JRF...
gstonian
Trusted Contributor

Re: Awk Variable Substition

Thanks but it's not the last part of the file, just simply the last part of the parameter ($5) I want to print. There are other fields after this one $6,$7 etc. I just simplified the issue down .

Thanks for the perl solution but would like to use awk if possible

Cheers
James R. Ferguson
Acclaimed Contributor

Re: Awk Variable Substition

Hi (again):

OK, then in 'awk':

# awk '{sub(/.+\\/, "",$0);print}' filename

Regards!

...JRF...
Tom Danzig
Honored Contributor

Re: Awk Variable Substition

Sounds like basename would be the simplest in your situation:

# basename London\SQL Servers\Server3
LondonSQL

Easiest awk would be:
awk -F'\' '{print $NF}' filename
Tom Danzig
Honored Contributor

Re: Awk Variable Substition

Please excuse my ridiculous 'basename' suggestion above.
gstonian
Trusted Contributor

Re: Awk Variable Substition

NO problem,

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 ", " ", " $6 } '
Peter Nikitka
Honored Contributor

Re: Awk Variable Substition

Hi,

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
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
Peter Nikitka
Honored Contributor
Solution

Re: Awk Variable Substition

Hi,

sorry 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
The Universe is a pretty big place, it's bigger than anything anyone has ever dreamed of before. So if it's just us, seems like an awful waste of space, right? Jodie Foster in "Contact"
gstonian
Trusted Contributor

Re: Awk Variable Substition

Thanks for all your answers. I ended up taking your answers and ended up with using awk twice to get what I wanted, which was.


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 !
gstonian
Trusted Contributor

Re: Awk Variable Substition

Thanks