UNIX Sed remove add
|
Original Message
|
Name: mlbedole
Date: November 5, 2005 at 10:29:35 Pacific
Subject: UNIX Sed remove addOS: XPCPU/Ram: 512 |
Comment: Hi, I have a address book and am making a change program for it. I have 6 fields per record Name:Street:City:State:Zip:Phonenumber I have asked for the record and using an if statement asked which field they would like to change. How do I take the answer for the new field and replace the old with the new filed. I am trying to use: sed `s/$name/$newname/' < addressbook I get the following error: Change: s/Mike/Michael/: not found any sugestions? Thanks Mike
Report Offensive Message For Removal
|
|
Response Number 1
|
Name: nails
Date: November 6, 2005 at 07:28:52 Pacific
Subject: UNIX Sed remove add |
Reply: (edit)If you are using variables in sed, you must use double quotes instead of single: #!/bin/ksh name="Mike" newname="Michael" sed "s/$name/$newname/" addressbook Are you sure sed is what you want to use? The above command replaces the first occurrence of "Mike" per line no matter where it is in the line.
Report Offensive Follow Up For Removal
|
|
Response Number 2
|
Name: mlbedsole
Date: November 6, 2005 at 11:05:14 Pacific
Subject: UNIX Sed remove add |
Reply: (edit)I think it is. This sed statment is inside an if statement pertaining to a specific record in the address book. This did work and printed it to the screen but did not actually replace the field in the address book. I would take any sugestions if you have a better way of doing it. Thanks, Mike
Report Offensive Follow Up For Removal
|
|
Response Number 3
|
Name: ericelysia
Date: November 7, 2005 at 04:13:02 Pacific
Subject: UNIX Sed remove add |
Reply: (edit)That sounds similar to what I am working on. cat<<here Please enter the record number of the particular record to change: here read number record=`sed -n ${number}p phonebook` name=`echo $record|cur -d: -f1` echo $name echo Do you want to change the name: "$name" read answer if[ "$answer" = "y" ] then echo "Please enter new name" read newname name=$newname fi I am now trying to use the sed command to remove the record (the number the user entered). I also am trying to add the new fields back into the file. sed -d '${number}' Is this right? Thanks, Eric
Report Offensive Follow Up For Removal
|
|
Response Number 5
|
Name: nails
Date: November 7, 2005 at 09:11:59 Pacific
Subject: UNIX Sed remove add |
Reply: (edit)Instead of sed, use awk which allows you to modify certain fields: #!/bin/ksh no=1 # field number name="Mike" newname="Michael" awk ' BEGIN { FS="|"; OFS="|" } { if($v1 == v2) $v1=v3 print }' v1="$no" v2="$name" v3="$newname" addressbook # end script In the script above, only field 1 is modified. Any instance of "Mike" in the other fields is ignored.
Report Offensive Follow Up For Removal
|
Use following form to reply to current message: