LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
LinkBack Search this Thread
Old 08-29-2007, 05:38 AM   #1
patilvidya
LQ Newbie
 
Registered: Apr 2007
Posts: 8

Rep: Reputation: 0
script help needed:


Hi All,

Is there way to grep for line from file.Suppose this is my file:

tomcat.shared
abc="/tmp/java/cc.jar","/usr/lib/jj.jar"
def="/usr/lib/jj.jar","/usr/etc/c.jar"

tomcat.server
abc="/tmp/java/cc.jar","/usr/lib/jj.jar"
def="/usr/lib/jj.jar",/usr/etc/c.jar"

tomcat.common
abc="/tmp/java/cc.jar","/usr/lib/jj.jar"
def="/usr/lib/jj.jar",/usr/etc/c.jar"

Here say abc and def are some products.And tomcat.shared,tomcat.serevr, tomcat.common are namespace.What i want is, grep for "tomcat.shared" if matches read next line and write full line into another file(say conf.sh) and then come to second line within same namespace and want to read single string which are separted by coma and check every string if it is already written in conf.sh.
Here when pattern matches control should read just below line and write that to conf.sh.I am not able to read single string and check for duplicates.If we read file with "while loop " how it should read single string and matching will happen.

At the end conf.sh file should looks like:
tomcat.shared="/tmp/java/cc.jar","/usr/lib/jj.jar","/usr/etc/c.jar"
tomcat.serevr="/tmp/java/cc.jar","/usr/lib/jj.jar","/usr/etc/c.jar"
tomcat.common="/tmp/java/cc.jar","/usr/lib/jj.jar","/usr/etc/c.jar"

How it can be achieved..Awaiting for reply
Any help is appreciated.
Thanks a lot.
 
Old 08-29-2007, 07:49 AM   #2
miedward
Member
 
Registered: Feb 2007
Distribution: RHEL 4, SOLARIS 10
Posts: 91

Rep: Reputation: 15
perl jumps to mind. a quick googling found this article on a fairly flexible parsing framework. as he suggests you might check CPAN and see if someone else has done this already

http://www.stonehenge.com/merlyn/UnixReview/col40.html
 
Old 08-29-2007, 08:53 AM   #3
theYinYeti
Senior Member
 
Registered: Jul 2004
Location: Nantes (France)
Distribution: Arch Linux
Posts: 1,897

Rep: Reputation: 61
Now that was interesting
Here's what I've come up with:
Code:
#!/bin/bash

tr ',=' '||' | awk -F\| '
function new() { if (line != "") print line; line=$1; last=""; }
function add(p) {
  if (last == "")
    line=line "=" p;
  else if (p != last)
    line=line "," p;
  last=p;
}
NF==1 && $1~/^tomcat/ { new(); next; }
NF>1 { for (i=2; i<=NF; i++) add($i); }
END { new() }
'
Suppose the script is named summary.sh, you would run it like this:
Code:
./summary.sh </path/to/input/file >/path/to/result/file
This script assumes the following:
- The namespace begins with "tomcat" and has neither equals nor comma in it.
- The products have neither equals nor comma in them.
- The JAR paths have neither equals nor comma in them.
- There's at least one JAR path for a namespace else it is not printed.
- Out of two identical paths, only one is kept if they are side by side only, because the order of JARs is considered important as it may have impact on which version of a Java class your program will end-up using.

If any of the above is not true, changes are possible.

Yves.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Script help needed! Atif_Khan Linux - Software 25 09-03-2007 12:51 AM
Script needed ganeshinforum Programming 2 01-05-2007 07:29 AM
bash script help needed Henster Linux - General 4 08-21-2005 09:54 AM
little script help needed abisko00 Programming 7 07-11-2005 08:37 AM
Help Needed with Firewall Script extremebfn Linux - Networking 0 09-04-2004 03:56 PM


All times are GMT -5. The time now is 05:50 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration