Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

multiple task

Status
Not open for further replies.

ovince

Programmer
Feb 27, 2007
55
FR
Hi All,

I really enjoy command that I learnd few days ago. It finds directories with a particular names and do removing of particular files. Something like

find DH* -type f -name "*.dat" -exec rm {} \;

I was wandering, is it posible to 'employ' awk command in similar manner and how?. For intance to find all "*.dat" in all DH* dirs (like above) and calculate average of $3 by awk


Thank you
oliver
 
any command you choose can come after the -exec clause

find ... -exec awk '{sum+=$3} END{print sum/NR}' {} \;

should do it (mind you - untested...)



HTH,

p5wizard
 
thanks. why this does not work on your opinion?

find DH* -type f -name "less*data.dat" -exec head -1 | awk 'BEGIN {FS=","}{print $3}' {} \;

The idea is to take out 3 column from the header of the files.

thanks
 
Hi

The pipe ( | ) breakes the code given as parameter to [tt]find[/tt]. But you do not need it, because [tt]awk[/tt] can do the [tt]head[/tt]'s job too :
Code:
find DH* -type f -name "less*data.dat" -exec awk [red]-F,[/red] '[red]NR==1[/red]{print $3}' '{}' \;

Feherke.
 
yes ... one more question

I would like to redirect what awk does into each directory. More precisely, to have fajl 'list' in each directory with the awk product

this attemp failed

find DH* -type f -name "less*data.dat" -exec awk -F, 'NR==1{print $3 > "list"}' '{}' \;
 
Hi

Just saying "list" means the file in the current directory, from where the command is run. Extract the path from the input file path.
Code:
find DH* -type f -name "less*data.dat" -exec awk -F, 'NR==1{[red]f=FILENAME;sub(/[^/]+$/,"list",f);[/red]print $3>[red]f[/red]}' '{}' \;

Feherke.
 
upffff

a small modification of it does not work again

find DH* -type f -name "less*data.dat" -exec awk -F, '{if(NR>2 && NR<6) f=FILENAME;sub(/[^/]+$/,"list",f);prin
t $3>f}' '{}' \;

what I do wrong again?

by the way, how do you put codes to this nice boxy code-place?
 
Hi

You have more then one command after the [tt]if[/tt] statement. But I am not sure if this is your problem.
Code:
find DH* -type f -name "less*data.dat" -exec awk -F, '{if(NR>2 && NR<6)[red]{[/red] f=FILENAME;sub(/[^/]+$/,"list",f);print $3>f[red]}[/red]}' '{}' \;

[gray]# or[/gray]

find DH* -type f -name "less*data.dat" -exec awk -F, '[red]NR>2 && NR<6[/red]{f=FILENAME;sub(/[^/]+$/,"list",f);print $3>f}' '{}' \;
oliver said:
by the way, how do you put codes to this nice boxy code-place?
Using TGML tags.

Feherke.
 
like this?

Feherke said:
find DH* -type f -name "less*data.dat" -exec awk -F, '{if(NR>2 && NR<6){ f=FILENAME;sub(/[^/]+$/,"list",f);print $3>>f}}' '{}' \;

I put >> in redirection ... I do not know why but in this way it works :)
 
I am steel with this issue ...


find DH* -type f -name "less*data.dat" -exec awk -F, '{if(NR>2 && NR<6){ f=FILENAME;sub(/[^/]+$/,"list",f);print $3>>f}}' '{}' \;


May I somehow put into 'list' as a second column the name of the file that is read out ("less*data.dat")
 
Hi

oliver said:
May I somehow put into 'list' as a second column the name of the file that is read out ("less*data.dat")
Code:
find DH* -type f -name "less*data.dat" -exec awk -F, '{if(NR>2 && NR<6){ f=[red]n=[/red]FILENAME;sub(/[^/]+$/,"list",f);[red]sub(/.*\//,"",n);[/red]print $3[red],n[/red]>>f}}' '{}' \;
I do not understand what you want exactly.

Feherke.
 
well, the goal is to take out first few records from each 'less*' file from each 'DH*' directories. File name would serve meto know which files are used in which order by awk. For example if I have files like:

less1.dat
less2.dat
....
less10.dat

awk will process the files in different order

less10.dat
less1.dat
less2.dat
...
less9.dat

and it is important to know the order


Maybe some sorting would help but I do not know how.

can I use more then one command after -exec? Then I could sort files first and pipelie somehow to


find DH* -type f -name "less*data.dat" -exec awk -F, '{if(NR>2 && NR<6){ f=n=FILENAME;sub(/[^/]+$/,"list",f);sub(/.*\//,"",n);print $3,n>>f}}' '{}' \;

 
Hi

Sorry, I was not clear enough. I meant I do not understand the whole thing, not just the file name appending.

So if you have a directory structure like this :

[tt]|- DH1
| +- less1.dat
|- DH2
| |- DH2b
| | |- less1.dat
| | +- less2.dat
| |- less1.dat
| |- less2.dat
| +- less3.dat
+- DH3
|- less1.dat
+- less2.dat[/tt]

Then want to have files in all subdirectories :

[tt]|- DH1
| |- less1.dat
| +- [red]list[/red]
|- DH2
| |- DH2b
| | |- less1.dat
| | |- less2.dat
| | +- [red]list[/red]
| |- less1.dat
| |- less2.dat
| |- less3.dat
| +- [red]list[/red]
+- DH3
|- less1.dat
|- less2.dat
+- [red]list[/red][/tt]

And for example in directory DH3 the list file to be generated as :

[tt]+-less1.dat-+ +-less2.dat-+ +-list---------+
| [blue]11[/blue] | | [green]21[/green] | | [blue]13[/blue] less1.dat |
| [blue]12[/blue] | | [green]22[/green] | | [blue]14[/blue] less1.dat |
| [blue]13[/blue] | + | [green]23[/green] | = | [blue]15[/blue] less1.dat |
| [blue]14[/blue] | | [green]24[/green] | | [green]23[/green] less2.dat |
| [blue]15[/blue] | | [green]25[/green] | | [green]24[/green] less2.dat |
| [blue]16[/blue] | | [green]26[/green] | | [green]25[/green] less2.dat |
+-----------+ +-----------+ +--------------+[/tt]

But I feel like I miss something.

Feherke.
 

this is exactly what I want...you do not miss anything...

But in some directories I have less1.dat upto less10.dat. Final file should contain rows from these files in this order

less1.dat
less2.dat
...
less10.dat

But awk takes them in this one

less10.dat
less1.dat
less2.dat
...
less9.dat

which is not good. So it would be nice to sort files first for reading out by awk.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top