UNIX Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsOperating SystemsUNIX Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 10th, 2005, 03:12 AM
stevefox stevefox is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2005
Posts: 12 stevefox User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 42 m 29 sec
Reputation Power: 0
Question Unix commad help

I want to copy a particular word which is inside the 2nd and 4th set of square brackets "[ ]" from a text file and insert the results into a new text file having each words separated by pipes "|".

e.g.
I have a text file input.txt which contains 4 line of text below:

user[1] = [Mike] group = [b] role = [Designer]
user[2] = [Peter] group = [a] role = [Supervisor]
user[3] = [Ben] group = [c] role = [Checker]
user[1] = [Mike] group = [b] role = [Supervisor]

I want to create a new text file output.txt which contains 3 line of text below:

Mike|Designer|Supervisor
Peter|Supervisor
Ben||Checker



Could someone tell me the unix or shell command to do this?

Any help will be appreciated.

Reply With Quote
  #2  
Old November 10th, 2005, 03:36 PM
LinuxPenguin's Avatar
LinuxPenguin LinuxPenguin is offline
fork while true;
Dev Shed God 1st Plane (5500 - 5999 posts)
 
Join Date: May 2005
Location: England, UK
Posts: 5,535 LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)LinuxPenguin User rank is General (90000 - 100000 Reputation Level)  Folding Points: 11590 Folding Title: Novice Folder
Time spent in forums: 1 Month 3 Weeks 1 Day 19 h 30 m 28 sec
Reputation Power: 1008
Ooo, pipe soup, my favourite...

Actually i'll call out my baby, perl

perl Code:
Original - perl Code
  1. #!/usr/bin/perl
  2. open(INFILE,"</path/to/file");
  3. open(OUTFILE,">/path/to/file");
  4. while (<INFILE>){
  5. $_ =~ /(\ w+)\ [(\ d+)\ ]\ s= \ s\ [(\ w+)\ ]\ s (\ w+) \ s=\ s\ [(\ w+)\]/;
  6. @jobs($2) = (@jobs($2)) ? (@jobs($2) + "|$5") : ($3 + "|" + $5);
  7. }
  8.  
  9. foreach (@jobs){
  10. print INFILE $_;
  11. }


should do you

Reply With Quote
Reply

Viewing: Dev Shed ForumsOperating SystemsUNIX Help > Unix commad help


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway