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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Analyzing the CLI output with AWK

Status
Not open for further replies.

brunocam

IS-IT--Management
Jul 25, 2011
4
BR

Hi, I'm trying to creat a script to organize a bunch of files in directories of years and month, and for this I will use awk and shell scripts, but I would like to know if I can use the awk's output to feed the functions of my script, becouse I had used awk before but when I typed for exemple (ls -l|awk '{print $6"-"$8}') to select only the important columns I send all to a text file ( >> log.txt) and after this the script had started to analyse the lines, but I would like to do that without create this file text. Is possible to do this with AWK.
 
You can use awk to print the commands, and pipe them to another shell:

Code:
ls -l | awk '{print "mv "$9" "$6"-"$8"/"$9}' | sh

Or you can run the move commands directly from your awk script using the system() function, which is a bit slower:

Code:
ls -l | awk '{system("mv "$9" "$6"-"$8"/"$9)}'

Of course this assumes that the destination directories already exist. It also assumes that no filenames contain spaces.

Remember also that field 8 in ls -l output may contain either a month or a time, depending on how old the file is.

Annihilannic.
 

cool suggestion, but I still have one doubt...

look, in every thing that I had read always the teach how to take parameters from a text file like this:

awk '$5 == 57 { print $8}' /bruno/log.txt

but I have to organize a directory with thousands and thousandos of files, and if I have to first create a text file with the (ls -l) in this directory for just latter start to analyse this will take a long long time...

So, I must to know is how to take this output directly from the shell prompt. In the command above, how could I replace the 'log.txt' to samply the output of the shell prompt:
# ls -l /bruno

This is the bigger question for me.
 

Simple and smart... :)

the output from this command is:

2011-07-24-bruno.sh
2011-07-29-flex.awk
2011-07-29-flex.sh
2011-07-24-staff
2011-07-24-teste2
2011-07-24-teste.awk

May I put this result into a variable?
I had try to do like this:
____________________________________________________
#! /bin/sh

VAR = $(ls -l /bruno | awk '$5 == 57 { print $8}')
----------------------------------------------------
But it doesn't work
 
Include your code between [ignore]
Code:
 ...
[/ignore]
tags to format it nicely in your posts.

What operating system is it exactly? On many OS /bin/sh is fairly primitive and doesn't understand $( ... ) syntax, in which case you may need to use backquotes instead: ` ... `

Also remove the spaces around the '=' sign.

Annihilannic.
 
I'm using CentOS

I had try to use backquotes too but it doesn't work...

If I could to put the output line in a variable, it would be much better... and fast...

 
In that case $( ... ) is fine because /bin/sh actually runs bash.

Did you take out the spaces like I suggested, i.e.:

Code:
#!/bin/sh

VAR=$(ls -l /bruno | awk '$5 == 57 { print $8}') 

echo "VAR is $VAR"

Annihilannic.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top