Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

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

Posted on 2004-10-08
9
Medium Priority
?
288 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
NFR key for Veeam Agent for Linux

Veeam is happy to provide a free NFR license for one year.  It allows for the non‑production use and valid for five workstations and two servers. Veeam Agent for Linux is a simple backup tool for your Linux installations, both on‑premises and in the public cloud.

 

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 85

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 85

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

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 video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Integration Management Part 2

971 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