Solved

Unix scripting: need last  (or second last) line before each blank line

Posted on 2010-09-16
5
373 Views
Last Modified: 2012-05-10
Hello again (where would I be without you).

In a file I need last line before each blank, unless last line has regex HOLD in it, then I would need the second last line. See attached screenshot.
Bottomline, I need lines with last runtime of each job.

Thanks for your help.
conman.jpg
0
Comment
Question by:Watnog
  • 3
  • 2
5 Comments
 
LVL 84

Expert Comment

by:ozo
Comment Utility
perl -ne '/\w/||print $l;/HOLD/||($l=$_)' file
0
 

Author Comment

by:Watnog
Comment Utility
Thanks, that's great.
I discover that what I thought to be blank lines fact aren't,  :-~
They would become if I can get rid of the last 2 columns, see attached.
That would make your oneliner work as intended.
Cheers.



conman1.jpg
0
 

Author Comment

by:Watnog
Comment Utility
Last 2 columns that is 'Returncode' and 'Dependencies'.
0
 
LVL 84

Accepted Solution

by:
ozo earned 300 total points
Comment Utility
This treats lines with more than 2 columns as non-blank
perl -ane '@F>2||print $l;/HOLD/||($l=$_)' file
0
 

Author Closing Comment

by:Watnog
Comment Utility
Wonderful.
Thx.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Let's say you need to move the data of a file system from one partition to another. This generally involves dismounting the file system, backing it up to tapes, and restoring it to a new partition. You may also copy the file system from one place to…
Every server (virtual or physical) needs a console: and the console can be provided through hardware directly connected, software for remote connections, local connections, through a KVM, etc. This document explains the different types of consol…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now