Unix/Linux Go Back    


Homework & Coursework Questions Students must use and complete the template provided. If you don't, your post may be deleted! Special homework rules apply here.

Attributes value substitution with sed command

Homework & Coursework Questions


Tags
sed, sed - replace text with variable, sed append, shell

Closed    
 
Thread Tools Search this Thread Display Modes
    #8  
Old Unix and Linux 08-12-2015
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 3:15 AM EDT
Location: Aachen, Germany
Posts: 11,346
Thanks: 297
Thanked 3,521 Times in 3,240 Posts
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course)

Quote:
School Name:Jalpaiguri Govt Engg College
Professor:Swapan kr Sengupta
Course Number:CSE502
City and Country are missing...
Sponsored Links
    #9  
Old Unix and Linux 08-12-2015
Bijayan Sarkar Bijayan Sarkar is offline
Registered User
 
Join Date: Jun 2015
Last Activity: 20 August 2015, 1:36 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by RudiC View Post
4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course)


City and Country are missing...
School Name:Jalpaiguri Govt Engg College
Professor:Swapan kr Sengupta
Course Number:CSE502
City:Kolkata Country:India
Sponsored Links
    #10  
Old Unix and Linux 08-12-2015
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 19 September 2017, 2:34 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,561
Thanks: 106
Thanked 1,569 Times in 1,161 Posts
Quote:
Originally Posted by Bijayan Sarkar View Post
I used the following script
Code:
#!/bin/bash
echo "Enter the attribute name"
read att
echo "enter new value"
read value

sed "s|$att\=\S*\S|$att=\"$value\"|g" test.xml>>out.xml

Well, your script is "almost good". Let us take a step back to analyse what is going wrong:

Suppose you have following input file (only one line):


Code:
Attribute="old"

How would you write a sed-script to change "old" to "new"?

I hope this helps.

bakunin
    #11  
Old Unix and Linux 08-12-2015
Bijayan Sarkar Bijayan Sarkar is offline
Registered User
 
Join Date: Jun 2015
Last Activity: 20 August 2015, 1:36 PM EDT
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Hi Sir,
I have almost tried it 50 times today with no hope.
it is possible to change "old" to "new" with my code. but it is not possibe to change "old value" to "new value" with this code.
it will be great if you could help with some code.
Thanks/
Sponsored Links
    #12  
Old Unix and Linux 08-12-2015
RudiC RudiC is online now Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 3:15 AM EDT
Location: Aachen, Germany
Posts: 11,346
Thanks: 297
Thanked 3,521 Times in 3,240 Posts
Look at the \S*\S. This might match sth. that you don't intend to; you may want to replace those.

BTW, I'm not sure every sed version understands \S.
Sponsored Links
    #13  
Old Unix and Linux 08-12-2015
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is online now Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 20 September 2017, 2:57 AM EDT
Location: San Jose, CA, USA
Posts: 10,502
Thanks: 542
Thanked 3,675 Times in 3,135 Posts
To expand on what RudiC said; I can assure you that "\S" in a search pattern in sed means different things to different versions of the sed utility. What operating system are you using? (If you don't know, what is the output from the command: uname -a?) On your operating system, what is the full pathname of the sed utility you are using? (If you don't know, what is the output from the command: type sed?)

What does the sequence \S match with the version of sed that you are using?

Step back and explain what you are hoping to match with the pattern $att\=\S*\S when you have attribute value pairs in your sample input like:

Code:
CustAddrStreet="AV ENGENHEIRO RICHARD" CustAddrState="RJ" CustAddrZip="20561-090"

If you feed your current script the input CustAddrState in response to the request: Enter the attribute name and New State in response to the request: enter new value, what output does your current script add to the end of your output file? Did "RJ" change to "New State"? Did it really produce the output you wanted?
Sponsored Links
    #14  
Old Unix and Linux 08-13-2015
bakunin bakunin is offline Forum Staff  
Bughunter Extraordinaire
 
Join Date: May 2005
Last Activity: 19 September 2017, 2:34 PM EDT
Location: In the leftmost byte of /dev/kmem
Posts: 5,561
Thanks: 106
Thanked 1,569 Times in 1,161 Posts
Quote:
Originally Posted by Bijayan Sarkar View Post
it is possible to change "old" to "new" with my code. but it is not possibe to change "old value" to "new value" with this code.
Actually it is NOT possible with your code and i tried to show you why using a simple example (instead of the complex you brought with you - we will come back to it once you can solve simple tasks).

So, again: suppose you have file containing only one this one line:


Code:
Attribute="old"

How would a sed-script look like to change "old" to "new"? Post the script and we proceed to the next step.

Quote:
Originally Posted by Bijayan Sarkar View Post
it will be great if you could help with some code.
One thing which is not going to happen here is we provide the code to do it. My colleagues might have different takes about how to explain to you what you did wrong, but in this regard we all agree. Nobody here will write you a script (or part of a script) to do your homework for you. Instead we will try to help you do it yourself.

I hope this helps.

bakunin
Sponsored Links
Closed

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
sed insert command and variable expansion/command substitution glev2005 UNIX for Dummies Questions & Answers 8 03-09-2010 02:18 PM
Single command to change the attributes of all luns presented to an AIX host kanna_geekworkz AIX 1 11-02-2009 05:53 PM
sed substitution som.nitk Shell Programming and Scripting 5 03-21-2009 11:39 PM
SED Substitution shubhranshu Shell Programming and Scripting 1 11-11-2008 03:50 AM
Substitution using SED shubhranshu Shell Programming and Scripting 2 11-11-2008 12:13 AM



All times are GMT -4. The time now is 03:16 AM.