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

Hello , I wrote a awk script , l

Status
Not open for further replies.

menyl

Programmer
Oct 26, 2011
12
NL
Hello ,

I wrote a awk script , line by line. each line is an awk one liner.
now I want to intergrate all lines into one AWK script.
I tried to do that for more than couple of hours but no success. each line runs perfect by itself... I must be doing sothing wrong here.

I would appricate your help:

ls -ltr --full-time -p | awk '{print $9, $6, $7}' > ~menyl/gg_list # takes all libs and files after sorting with full time
awk '{if ($1 ~/\//) {print} else {} }' ~menyl/gg_list > ~menyl/atf_list # remove all files that are not directories
awk 'BEGIN {FS= "." } 1==1 {print $1}' ~menyl/atf_list > ~menyl/array_list # remove the long time suffix
awk '{print $2, $3, $1}' ~menyl/array_list > ~menyl/finalarray_list # change the order of the fields
awk 'BEGIN {FS= "/" } 1==1 {print $1}' ~menyl/finalarray_list > ~menyl/finalarray_list2 ##






Thank you very much


meny
 
Do you need each of the output files or are they just holding the values while you proceed to the next step?

If not, this is each of your awk's concatenated:

Code:
ls -ltr --full-time -p | awk '{print $9, $6, $7}'|awk '{if ($1 ~/\//) {print} else {} }' |awk 'BEGIN {FS= "." } 1==1 {print $1}'|awk '{print $2, $3, $1}' |awk 'BEGIN {FS= "/" } 1==1 {print $1}'

This writes the files as well, in case you need them.
Code:
ls -ltr --full-time -p | tee gg_list | awk '{print $9, $6, $7}'| tee atf_list | awk '{if ($1 ~/\//) {print} else {} }' | tee array_list | awk 'BEGIN {FS= "." } 1==1 {print $1}'| tee finalarray_list | awk '{print $2, $3, $1}' | tee finalarray_list2 | awk 'BEGIN {FS= "/" } 1==1 {print $1}'

I am missing the point of this and not sure what your goal is but am happy to try and help.

If all you want is the output you could try:
This has the trailing forward slash and the second strips it in case you don't want it.

root@linux:/# ls -dlox --full-time */ | sed -e 's/.[0-9]\{9\}\>//g'|awk '{print $5" "$6" "$8}'
2012-07-06 18:36:18 bin/
2012-07-06 18:44:18 boot/
2012-08-11 08:03:20 dev/
2012-08-11 13:41:31 etc/
2012-07-06 18:53:08 home/
2012-07-06 18:39:04 lib/
2012-07-06 18:22:16 lost+found/
2012-08-11 08:03:05 media/
2012-05-07 10:55:35 mnt/
2012-07-07 10:54:10 opt/
2012-07-07 14:58:17 proc/
2012-08-11 13:41:33 root/
2012-08-11 07:54:22 sbin/
2010-07-21 02:55:22 selinux/
2012-07-06 18:24:36 srv/
2012-07-07 14:58:17 sys/
2012-08-12 16:17:01 tmp/
2012-07-06 18:24:36 usr/
2012-07-06 18:40:22 var/
root@linux:/# ls -dlox --full-time */ | sed -e 's/\(.*\)./\1/' -e 's/\.[0-9]\{9\}\>//g'|awk '{print $5" "$6" "$8}'
2012-07-06 18:36:18 bin
2012-07-06 18:44:18 boot
2012-08-11 08:03:20 dev
2012-08-11 13:41:31 etc
2012-07-06 18:53:08 home
2012-07-06 18:39:04 lib
2012-07-06 18:22:16 lost+found
2012-08-11 08:03:05 media
2012-05-07 10:55:35 mnt
2012-07-07 10:54:10 opt
2012-07-07 14:58:17 proc
2012-08-11 13:41:33 root
2012-08-11 07:54:22 sbin
2010-07-21 02:55:22 selinux
2012-07-06 18:24:36 srv
2012-07-07 14:58:17 sys
2012-08-12 16:17:01 tmp
2012-07-06 18:24:36 usr
2012-07-06 18:40:22 var



Cybex
 
Hi again,

great answer! - thanks.
I dont need the intermidate files, so I would take the first option.
but what I was really asking is how to take my 5 singles lines, that each works fine , and
integrate them into an awk script that you can run from a terminal by typing >>awk -f some_script.awk..

thx

meny

 
Well either use the one-liner I showed you or create a new file called "awkscript.sh", chmod it to make it executable, paste this as the first line "#!/bin/bash", put your five individual scripts below it and save the file. Then you can run the script like this "./awkscript.sh" and it will run each of your five lines from one command. Is that what you were asking?
 
Yes. that is exactly what I was asking!! works great.
but now Ive got few more question, if that is OK:

1. why the suffix is *.sh and not *.awk ?
2. I tried to do the same with awk suffix instaed of sh- and
it and wont work. why?

3. since the 5 different lines are all started with : awk..' isnt it possible to write is as one script, with one awk..'
can you show me?

thx

meny
 
Hi

Code:
ls -ltr --full-time -p | awk '[navy]$9[/navy] [teal]~[/teal] [fuchsia]/\//[/fuchsia][teal]{[/teal][COLOR=chocolate]sub[/color][teal]([/teal][fuchsia]/\..*/[/fuchsia][teal],[/teal] [green][i]""[/i][/green][teal],[/teal] [navy]$7[/navy][teal]);[/teal] [COLOR=chocolate]sub[/color][teal]([/teal][fuchsia]/\//[/fuchsia][teal],[/teal] [green][i]""[/i][/green][teal],[/teal] [navy]$9[/navy][teal]);[/teal] [COLOR=chocolate]print[/color] [navy]$6[/navy][teal],[/teal] [navy]$7[/navy][teal],[/teal] [navy]$9[/navy][teal]}[/teal]'


Feherke.
[link feherke.github.com/][/url]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top