Please note that LinuxExchange will be shutting down on December 31st, 2016. Visit this thread for additional information and to provide feedback.

I'm running into a slight quandary with this particular script I need. I have a used egrep to yank from a large number of config files the location parameter of various unity databases. I have that information in a script. For example, the data looks like:

/home/test/demo/new
/home/test/demo/old
/home/yikes/mydir/ss/new
/home/yikes/mydir/ss/old

So, the problem is each location doesn't have a particular field I can cut with "/" as the delimiter, however, the second to last field in that string is what I want: demo and ss. Is there an easy way to script this. I want to basically spin through this flat file (290 lines), in a new file print out the complete path and then on the same line separated by a space the second to last field.

The end result would look like:

/home/test/demo/new demo
/home/test/demo/old demo
/home/yikes/mydir/ss/new ss
/home/yikes/mydir/ss/old ss

Thanks for the help!

asked 20 Sep '10, 01:28

Andy's gravatar image

Andy
2972920
accept rate: 14%




cat $FILE | awk -F'/' '{print $0, $(NF-1)}'

link

answered 20 Sep '10, 08:09

Bob%202's gravatar image

Bob 2
361
accept rate: 100%

Great! Thanks for your help!

(20 Sep '10, 15:04) Andy
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×8
×1
×1

Asked: 20 Sep '10, 01:28

Seen: 8,360 times

Last updated: 20 Sep '10, 08:09

powered by OSQA