1753894 Members
7509 Online
108809 Solutions
New Discussion юеВ

awk question

 
SOLVED
Go to solution
Hunki
Super Advisor

awk question


find . -type f

returns this :
./SECQUERYuninstall
./2007.02_uninstall
./2007.03_install
./2007.03_uninstall

and I want to remove the initial ./ from the output and tried this :

find . -type f |awk -F"./" '{print $NF}'

but it gave me the wrong output :

/SECQUERYuninstall
02_uninstall
03_install
03_uninstall

How shall i get the following output :
SECQUERYuninstall
2007.02_uninstall
2007.03_install
2007.03_uninstall
11 REPLIES 11
Patrick Wallek
Honored Contributor
Solution

Re: awk question

Just omit the '.' in your '-F "./"

# find . -type f | awk -F "/" '{print $NF}'

A. Clay Stephenson
Acclaimed Contributor

Re: awk question

find . -type f | awk '{sub("^./","",$0); print $0}'

If it ain't broke, I can fix that.
Hunki
Super Advisor

Re: awk question

Thanks Patric , but a little change in requirement now :

find . -type f output is :

./shellscripts/A/SECQUERYuninstall
./shellscripts/A/B/2007.02_uninstall
./shellscripts/C/2007.03_install
./shellscripts/D/2007.03_uninstall

and I want :

shellscripts/A/SECQUERYuninstall
shellscripts/A/B/2007.02_uninstall
shellscripts/C/2007.03_install
shellscripts/D/2007.03_uninstall
A. Clay Stephenson
Acclaimed Contributor

Re: awk question

find . -type f | awk -F "/" '{print $NF}'

This approach only works in a special case; you said that you want to remove the INITIAL ./ from the string; consider the case of directories below the CWD.

That is why sub("^./","",$0) is the better approach; it removes ONLY the initial ./ becase the search is anchored.
If it ain't broke, I can fix that.
James R. Ferguson
Acclaimed Contributor

Re: awk question

Hi Hunki:

# find . -type f | sed -e 's%^./%%'

Regards!

...JRF...
Hunki
Super Advisor

Re: awk question

Thanks Clay but it gives me :

awk: syntax error near line 1
awk: illegal statement near line 1
Patrick Wallek
Honored Contributor

Re: awk question

I just cut-and-pasted Clay's solution into a terminal window and it worked fine for me.

Perhahps you forgot a " somewhere?

It would help to see the exact command line you used, not just the errors.
A. Clay Stephenson
Acclaimed Contributor

Re: awk question

Then you have a personal problem because I just typed the command in and it produced exactly the desired output. Make sure that you do not confuse single and double quotes.
If it ain't broke, I can fix that.
Sandman!
Honored Contributor

Re: awk question

Your original awk will work with a slight modification i.e.

# find . -type f |awk -F"^./" '{print $NF}'

~hope it helps