Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Need help.. I want to replace a line in a file with a variable

Posted on 2004-10-08
9
Medium Priority
?
283 Views
Last Modified: 2010-04-21
Ok,

Here is what I have...

the file:

12345:Firstline:98765:blahblahblah
12345:Targetline:98765:blahblahblah
12345:Thirdline:98765:blahblahblah

the value of the variable is actually the modified Targetline:

echo $rplc
54321:Targetline:56789:blahblahblah

So I have been trying to find a sed line or awk that would assist me. I want to search the file based on the second field (":" delimited btw). The second field will not be changing and should be unique throughout the file.

I am using bash,awk,and sed.
Please help.

Thank in advance!!
0
Comment
Question by:g8ballz0fyre
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
9 Comments
 

Author Comment

by:g8ballz0fyre
ID: 12265142
One more thing about that second field...

There may be instances of the second field where it will read "Electric" and the next line will have "Electric Heat".

Just sat back and realized that if I search on "Electric" it may return more than one line......hmmm more brain cell burning.. need coffee..
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 12265729
Try this:

#!/bin/bash

file=./file
rplc="54321:Targetline:56789:blahblahblah"

search=`echo $rlpc | awk -F: ' { print $2 } '`
grep :${search}: $file
0
 
LVL 44

Expert Comment

by:Karl Heinz Kremer
ID: 12266321
Forgot about the "replace" requirement (which is only mentioned in the subject). Does the replaced line have to be in the same location as the original line was, or is it sufficient to place the new information at the end of the file?
0
Create CentOS 7 Newton Packstack Running Keystone

A bug was filed against RDO for the installation of Keystone v3. This guide is designed to walk you through the configuration for using Keystone v3 with Packstack. You will accomplish this using various repos and the Answers file.

 

Author Comment

by:g8ballz0fyre
ID: 12267383
The replacement doesn't neccesarily need to be in the same place...
It looks like this will grab the target line correctly.. so once I have it, how can I delete it?

Then all I would have to do is echo $rplc >> $file.

Sorry about the replacement statement only being in the subject.
0
 
LVL 84

Expert Comment

by:ozo
ID: 12267439
grep -v :`echo $rplc | awk -F: ' { print $2 } '`: file > file.$$
mv file.$$ > file
0
 

Author Comment

by:g8ballz0fyre
ID: 12267497
OK,

Thanks so much for your advice khkremer and ozo, it all is beginning to work.

Do you have any suggestions for like instance of the second field??

There may be instances where the second field will be Admin and Super Admin.
As is sits now running:
-----------------------------------------
#!/bin/bash

file=./file
rplc="54321:Admin:56789:blahblahblah"
search=`echo $rlpc | awk -F: ' { print $2 } '`
grep -v $search > newfile
mv newfile oldfile

-------------------------------------------
It would remove both, Admin and Super Admin, lines from the list...
0
 
LVL 44

Accepted Solution

by:
Karl Heinz Kremer earned 1000 total points
ID: 12267575
Try this:

#!/bin/bash

file=./file
rplc="54321:Super Admin:56789:blahblahblah"
search=`echo $rplc | awk -F: ' { print $2 } '`

grep -q ":${search}:" $file
ret=$?
if [ $ret -eq 0 ] ; then
    grep -v ":${search}:" $file  > $file.$$
    echo $rplc  >> $file.$$
    mv -f $file.$$ $file
fi

.. and I'm sorry for the typo in my first script. This script does two grep operations: The first one is to find out if the search string is on the file. If it is, we replace it. If it is not, we are not doing anything.
0
 
LVL 84

Expert Comment

by:ozo
ID: 12267611
grep -v :$search: $file > newfile
0
 

Author Comment

by:g8ballz0fyre
ID: 12267823
Thanks for the help..both of you!!!

I just put your script as it is and it seems to work flawlessly...

Thanks again!!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

722 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question