- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Re: Help with awk.
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-07-2011 11:56 PM
тАО02-07-2011 11:56 PM
Help with awk.
When I do a "ls" in a given directory, I want to parse it so that it prints all the files except the last four:
[root@test sample]# ls -ltr
total 0
-rw-r--r-- 1 root root 0 feb 8 08:54 one
-rw-r--r-- 1 root root 0 feb 8 08:54 two
-rw-r--r-- 1 root root 0 feb 8 08:55 three
-rw-r--r-- 1 root root 0 feb 8 08:55 four
-rw-r--r-- 1 root root 0 feb 8 08:55 five
-rw-r--r-- 1 root root 0 feb 8 08:55 six
I want it to show all the files but the four most recents, I mean: one and two only.
How can I do this???
- Tags:
- awk
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2011 01:47 AM
тАО02-08-2011 01:47 AM
Re: Help with awk.
Please try this:
[root@test sample]# ls -ltr|tail -4
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2011 01:51 AM
тАО02-08-2011 01:51 AM
Re: Help with awk.
Ooops, to show one and two files try:
[root@test sample]# ls -ltr|head -2
Rgds.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2011 04:09 AM
тАО02-08-2011 04:09 AM
Re: Help with awk.
# ls -lrt |tail -6|head -2
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2011 06:19 AM
тАО02-08-2011 06:19 AM
Re: Help with awk.
> I want it to show all the files but the four most recents, I mean: one and two only.
So you mean only the first two files in the list. You could do this:
# ls -ltr | awk '!/^total/ && NR<=3'
...which skips the "total" or first line and then delivers lines two and three which are what you want.
Regards!
...JRF...
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2011 06:29 AM
тАО02-08-2011 06:29 AM
Re: Help with awk.
Your query doesn't lend itself to any single command. That's assuming a directory with random number of files.
You could do:
ls -lt | tail +4
(Or possibly +5 to remove that total?)
The files will be in reverse order, two then one.
Otherwise you would have to put the output in a file and then count the lines before printing them. (Or you could do multiple ls(1) commands.)
Or you could have a complex awk script that buffers up the last 4 lines and if more, then print the first. And upon EOF, don't print those 4 lines.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО02-08-2011 04:07 PM
тАО02-08-2011 04:07 PM
Re: Help with awk.
You need +6 to skip total line and 4 more:
ls -lt | tail +$(( 4 + 2 ))