LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   cat|grep|awk (https://www.linuxquestions.org/questions/programming-9/cat%7Cgrep%7Cawk-655094/)

pudhiyavan 07-11-2008 07:39 AM

cat|grep|awk
 
i have files like jul07, jul08 which contains a data as follow

Wed Jul 9 16:45:01 IST 2008
Mem Used 744
hddMounted Used Avail
/apps 2.2G 31G


now i need to have the time (16:45) and Mem used (744) tobe extracted and display in timely order

can anyone help me?

cat Jul090000 |grep 'Mem Used' |awk '{print $3}' gives me 744 how to add 16;45 along with this?

pixellany 07-11-2008 08:00 AM

Use the same technique to extract "16:45" and put it in a variable. Then use that variable in combination with the other command.

really good AWK tutorial here:
http://www.grymoire.com/Unix/Awk.html

pudhiyavan 07-11-2008 08:03 AM

but i have multiple files like this,

pixellany 07-11-2008 08:10 AM

To do the same thing on multiple files, you would simply put your script in a structure such as this:

Suppose your script is "wordsmasher", and you have all your files in one directory:

for file in ls; do
wordsmasher $file
<<other commands>>
done

pudhiyavan 07-11-2008 08:14 AM

Thanks for your help

chrism01 07-14-2008 01:36 AM

You could grab the time like this:

head -1 <yourfilename>|awk '{print $4}'

Mr. C. 07-14-2008 01:56 AM

Code:

$ cat data1 data2

Wed Jul 9 16:45:01 IST 2008
Mem Used 744
hddMounted Used Avail
/apps 2.2G 31G


Wed Jul 8 12:45:01 IST 2008
Mem Used 894
hddMounted Used Avail
/apps 2.2G 31G

$ perl -e '$/ = undef; while ($_ = <>) { print "$1 $2\n" if /\w{3} \w{3} +\d+ (\d{2}:\d{2}):\d{2}.*\nMem Used (\d+)/}' data*
16:45 744
12:45 894



All times are GMT -5. The time now is 06:19 AM.