Solved

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

Posted on 2004-10-08
9
277 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
Is Your Team Achieving Their Full Potential?

74% of employees feel they are not achieving their full potential. With Linux Academy, not only will you strengthen your team's core competencies but also their knowledge of of the newest IT topics.

With new material every week, we'll make sure that you stay ahead of the game.

 

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 250 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

628 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