Solved

Help with SED Command

Posted on 2011-03-09
5
395 Views
Last Modified: 2012-08-14
Hello all,
 Ok I don't know if this is against forum rules or not. I am not asking for an answer here, I am simply looking for help getting started. I have this homework question that is as follows:

Using the emp.lst data file generate a list of all the employees formatted as firstname, lastname 19xx where xx it the year of birth from the birth date field. Sort the list in alphabetical order. For maximum credit solve the problem without using the cut command.  

What he wants us to use is the SED command. The file is as follows:

2233:charles harris          :g.m.          :sales           :12/12/52: 90000
9876:bill johnson               :director    :production   :03/12/50:130000
5678:robert dylan              :d.g.m.       :marketing     :04/19/43: 85000
2365:john woodcock         :director    :personnel  :05/11/47:120000
5423:barry wood               :chairman  :admin         :08/30/56:160000
1006:gordon lightfoot         :director    :sales          :09/03/38:140000
6213:michael lennon          :g.m.          :accounts   :06/05/62:105000
1265:p.j. woodhouse        :manager    :sales         :09/12/63: 90000
4290:neil o'bryan               :executive  :production :09/07/50: 65000
2476:jackie wodehouse    :manager     :sales        :05/01/59:110000
6521:derryk o'brien           :director       :marketing :09/26/45:125000
3212:bill wilcocks              :d.g.m.         :accounts  :12/12/55: 85000
3564:ronie trueman           :executive   :personnel :07/06/47: 75000
2345:james wilcox            :g.m.            :marketing  :03/12/45:110000
0110:julie truman               :g.m.             :marketing :12/31/40: 95000

Please help.
0
Comment
Question by:BoxunloX
  • 2
  • 2
5 Comments
 
LVL 45

Accepted Solution

by:
Kdo earned 250 total points
ID: 35087282
Hi Box,

If you split the task into smaller tasks, this is pretty "doable".  :)

2233:charles harris          :g.m.          :sales           :12/12/52: 90000


The name is between the first two colons.  Skip/blank the text before the first colon, or extract the text between the first two.

Same thing with the date.  It's between the 4th and 5th colons.  Once the date is found, use the same technique to extract the date from a string where the field delimiter is slash (/) instead of colon (:).


Good Luck,
Kent
0
 
LVL 48

Assisted Solution

by:Tintin
Tintin earned 250 total points
ID: 35087406
Thank you for being upfront about it being a homework question.  We can certainly assist you, but can't give you the direct answer.

It can certainly be done as a single sed statement (I tried it), although it is relatively complex.

What I did was to use a combination of regular expressions and pattern capturing.

I give you a simple example to help you on your way.

In sed, to capture a pattern, you enclose them in brackets, eg:

\(..\)

You need to escape the brackets with backslashes.


Let's say I wanted to capture the first number in your data, then I could do

sed "s#^\([0-9]*\):.*#Num is \1#" emp.lst

Open in new window



Hope that helps.

0
 
LVL 1

Author Comment

by:BoxunloX
ID: 35087924
Thank you kdo. My solution didn't work from the terminal but at least your help got me started and I have something to turn in. It isnt pretty and is most definitely wrong but some points are better than none. We have covered 12 chapters of UNIX in 6 weeks and in my opinion it is just toooooooo damn fast to actually learn any of the material. AWK, SED, PERL, SORT, etc., could all take weeks or months to learn fluently and we get a week for each.......

Thanks again.
0
 
LVL 45

Expert Comment

by:Kdo
ID: 35087965
Hi Box,

A lot of these things are wrappers around the same core tools.

While they are completely separate programs, sed is quite simple.  awk is sed on steriods.  Perl is a complete programming language that is awk on mega-steroids.

It all becomes a lot easier if you can learn their common key, that is "regular expressions".  Once you understand them, the tool that's putting them into play becomes easy.


You'll like unix once you get comfortable with it.

Good Luck,
Kent
0
 
LVL 1

Author Closing Comment

by:BoxunloX
ID: 35087988
Thank you guys. As I stated in my reply to kdo, this class has basically forced UNIX down our throats in a very short period of time. I have been doing sysadmin (on the Windows side) for a few years now and these homework questions just make me feel dumb. Anyway, thanks again and if I make it through this class alive I may think of broadening my UNIX skills on my own time in the future;)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

If you have a server on collocation with the super-fast CPU, that doesn't mean that you get it running at full power. Here is a preamble. When doing inventory of Linux servers, that I'm administering, I've found that some of them are running on l…
Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…

707 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

11 Experts available now in Live!

Get 1:1 Help Now