Solved

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

Posted on 2004-10-08
9
246 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

910 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

20 Experts available now in Live!

Get 1:1 Help Now