Need help with sed command


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Need help with sed command
# 1  
Old 03-13-2012
Need help with sed command

Hi all,

I try to use sed to find something specific in a file (test.h), find the same specific term in another file (test.xml) read an attribute of it, and replace the begining of the specific term in the test.h.

Well.... an example will be beter Smilie

test.h
Code:
typedef LON_STRUCT_BEGIN(LonWriteableValueFile)
{
    SCPTminSendTime cpMinSendTime;             /* sd_string("1,0,0\x80,52,2;") */
    SCPTmaxSendTime cpMaxSendTime;             /* sd_string("1,0,0\x80,49,2;") */
    SCPTmaxRcvTime cpMaxRcvTime;               /* sd_string("1,0,0\x80,48,2;") */
}

I need to find every word that begin with [space]cp and end with ;

eg: cpMinSendTime

I think I figured something. probably not pretty, but it's works.
Code:
sed -n "/[ ]cp.*[;] /{s/^.*\(cp.*[;] \).*$/\1/;p;}" test.h | sed 's/[;]//'

this output
Code:
cpMinSendTime
cpMaxSendTime
cpMaxRcvTime

so far so good.

I need to find in the test.xml file

Code:
<?xml version="1.0" encoding="utf-8"?>
<Template Name="MyTemplate">
  <Object Name="0">
    <CPs>
      <cpMinSendTime type="SNVT_time_sec" default="5"></cpMinSendTime>
      <cpMaxSendTime type="SNVT_time_sec" default="10"></cpMaxSendTime>
    </CPs>
    <SNVTs>
      <nviPercentOpen type="SNVT_lev_percent" default="0"></nviPercentOpen>
    </SNVTs>
  </Object>
</Template>

The attribute type of the cpMinSendTime.

I think I found something too using bash script
Code:
xmlOutput=`sed -n -e '/<'${file}'/p' Test.xml`
regex="type=\"(.*?)\" "
if [[ $xmlOutput =~ "type=\"(.*?)\" " ]]; then
			cpType=${BASH_REMATCH[1]}
fi

with this I found the field Type in my xml type.

When the type is found, I need to replace SCPTminSendTime in the test.h for the cpMinSendTime with SNVT_time_sec;

The result should look like this

test.h (modified)
Code:
typedef LON_STRUCT_BEGIN(LonWriteableValueFile)
{
    SNVT_time_sec cpMinSendTime;             /* sd_string("1,0,0\x80,52,2;") */
    SNVT_time_sec cpMaxSendTime;             /* sd_string("1,0,0\x80,49,2;") */
    SNVT_time_min cpMaxRcvTime;               /* sd_string("1,0,0\x80,48,2;") */
}

Wow... I hope this is clear enough for you !

1- I have only access to bintuils. (sh, awk, grep, sed etc...)
2- Is it possible to do in a sed script ?

I have to admit that i'm not familiar with all the shell scripting

I will ne some help to do this !

regards

Jonathan

Last edited by dumarjo; 03-13-2012 at 01:49 PM..
# 2  
Old 03-13-2012
It is best to use a XML tool to process XML file.

Install Perl-XML-XPath
Code:
# yum install perl-XML-XPath.noarch
or
# perl -MCPAN -e 'install XML::XPath'

Try this:
Code:
$ cat test.h
typedef LON_STRUCT_BEGIN(LonWriteableValueFile)
{
    SCPTminSendTime cpMinSendTime;             /* sd_string("1,0,0\x80,52,2;") */
    SCPTmaxSendTime cpMaxSendTime;             /* sd_string("1,0,0\x80,49,2;") */
    SCPTmaxRcvTime cpMaxRcvTime;               /* sd_string("1,0,0\x80,48,2;") */
}

$ cat test.pl
#! /usr/bin/perl
use XML::XPath;

$xp=XML::XPath->new($ARGV[0]);
$nodes=$xp->find('/Template/Object/CPs/*');
foreach $n ($nodes->get_nodelist) {
        print $n->getName(), " ", $n->getAttribute('type'), "\n";
}

$ cat test.xml
<?xml version="1.0" encoding="utf-8"?>
<Template Name="MyTemplate">
  <Object Name="0">
    <CPs>
      <cpMinSendTime type="SNVT_time_sec" default="5"></cpMinSendTime>
      <cpMaxSendTime type="SNVT_time_sec" default="10"></cpMaxSendTime>
    </CPs>
    <SNVTs>
      <nviPercentOpen type="SNVT_lev_percent" default="0"></nviPercentOpen>
    </SNVTs>
  </Object>
</Template>

$ ./test.pl test.xml | while read k v
do 
  sed -i "s/[A-Za-z][A-Za-z]*[ \t][ \t]*$k/$v $k/" test.h
done

$ cat test.h
typedef LON_STRUCT_BEGIN(LonWriteableValueFile)
{
    SNVT_time_sec cpMinSendTime;             /* sd_string("1,0,0\x80,52,2;") */
    SNVT_time_sec cpMaxSendTime;             /* sd_string("1,0,0\x80,49,2;") */
    SCPTmaxRcvTime cpMaxRcvTime;               /* sd_string("1,0,0\x80,48,2;") */
}

# 3  
Old 03-14-2012
Hi,

Thanks for your answer, but like I specified in the first post, i can't install perl.

I agree with you that using an xml tool should be better. I will look into this path !

Jonathan
# 4  
Old 03-14-2012
See if this works:
Code:
awk '
  NR==FNR{
    if(/^cp/)
      for(i=1;i<NF;i++)
        if($i~/type=/)
          A[$1]=$(i+1)
    next
  } 
  A[$3]{
    sub($2,A[$3])
  }
  1
' RS=\< FS="[>\"]|[ \t]*" test.xml RS="\n" FS="[ \t]*|;" test.h

Or shorter:
Code:
awk '/^<cp/{t=$2} /^type/&&t {A[t]=$2;t=x} A[$3]{sub($2,A[$3])}FNR!=NR' RS=" " FS='<|"' test.xml RS="\n" FS="[ \t]*|;" test.h


Last edited by Scrutinizer; 03-14-2012 at 12:03 PM..
# 5  
Old 03-14-2012
Hi,

It's works..

I have no idea what you did, but it's works

is it possible to have it explained ?

Jonathan
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Output of sed command to another sed command

Hi All, I'm relatively new to Unix scripting and am trying to get my head around piping. I'm trying to take a header record from one file and prepend it to another file. I've done this by creating several temp files but i'm wondering if there is a cleaner way to do this. I'm thinking... (10 Replies)
Discussion started by: BigCroyd
10 Replies

2. Shell Programming and Scripting

sed and awk giving error ./sample.sh: line 13: sed: command not found

Hi, I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :- ./sample.sh: line... (12 Replies)
Discussion started by: satishmallidi
12 Replies

3. Shell Programming and Scripting

sed Command

Hello, I'm working with this command which I'm having trouble understanding it: sed -e '1,$ s/SUB/N/g' < $1 > file.txt Where SUB stand for an special character with code in ASCII is 0x1A, notepad read it as a right arrow. Any help will be appreciated. (5 Replies)
Discussion started by: emilioveras
5 Replies

4. Shell Programming and Scripting

sed returns error "sed: -e expression #1, char 18: unterminated `s' command"

Hello All, I have something like below LDC100/rel/prod/libinactrl.a LAA2000/rel/prod/libinactrl.a I want to remove till first forward slash that is outputshould be as below rel/prod/libinactrl.a rel/prod/libinactrl.a How can I do that ??? (8 Replies)
Discussion started by: anand.shah
8 Replies

5. UNIX for Advanced & Expert Users

sed command

Hi..... I'm using sed command for replace the words in a file cat >test.txt My test.txt contains Mary had a little ham Mary fried a lot of spam Jack ate a Spam sandwich Jill had a lamb spamwich Marry had a spicy wich $ sed 's/wich$/mirchi/g' test.txt output is: Mary had a little ham... (24 Replies)
Discussion started by: ksrivani
24 Replies

6. UNIX for Dummies Questions & Answers

sed insert command and variable expansion/command substitution

I know this script is crummy, but I was just messing around.. how do I get sed's insert command to allow variable expansion to show the filename? #!/bin/bash filename=`echo $0` /usr/bin/sed '/#include/ { i\ the filename is `$filename` }' $1 exit 0 (8 Replies)
Discussion started by: glev2005
8 Replies

7. Shell Programming and Scripting

Convert Sed command to perl command

Hello, Can any perl experts help me convert my sed string to perl. I am unsuccessful with this. I have to remove this string from html files OAS_AD('Top'); I have come up with this. However the requirement is in perl. for find in $(find . -type f -name "file1.html") ; do cat $find |... (2 Replies)
Discussion started by: abacus
2 Replies

8. UNIX for Dummies Questions & Answers

some help with the sed command please

hi all, attached you can find a small txt file ( .txt ), GIVEN that past_scheduler="islip" and scheduler="mucf" can somebody please tell me WHY sed 's/-u '$past_scheduler'/-u '$scheduler'/g' .txt > .txt.temp fails ? thanx (3 Replies)
Discussion started by: OneDreamCloser
3 Replies

9. Shell Programming and Scripting

Loop with sed command to replace line with sed command in it

Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line. ... (1 Reply)
Discussion started by: cbo0485
1 Replies

10. Shell Programming and Scripting

awk/sed Command : Parse parameter file / send the lines to the ksh export command

Sorry for the duplicate thread this one is similar to the one in https://www.unix.com/shell-programming-scripting/88132-awk-sed-script-read-values-parameter-files.html#post302255121 Since there were no responses on the parent thread since it got resolved partially i thought to open the new... (4 Replies)
Discussion started by: rajan_san
4 Replies
Login or Register to Ask a Question