Solved

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

Posted on 2004-10-08
9
239 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
  • 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
 

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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now