Search and replace with variable


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Search and replace with variable
# 1  
Old 05-20-2015
Search and replace with variable

Hello all,

I stumbled upon a command line for multiple search and replace within given destination
Code:
perl -pi -w -e 's/SEARCH_FOR/REPLACE_WITH/g;' *.html

I want to replace the following line where the date is the variable, from
Code:
<div class="meta">
<ul>
<li>05.05.2015

with date tags, like so
Code:
<div class="meta">
<ul>
<li><date>05.05.2015</date>

Is there at all a solution to this? Please forgive my ignorance - this is all very new to me.

Any help and guidance is much appreciated.
# 2  
Old 05-21-2015
Hi

Must it be perl?
If not, try: (untested)
Code:
search="05.05.2015"
replace="<date>$search</date>"
sed s,"$search","$replace",g *html

If the changes are done, add an -i before the *html to implement the changes to the file/s.

Hope this helps
This User Gave Thanks to sea For This Post:
# 3  
Old 05-21-2015
Hi,

If I understand this correctly, the solution you provided will only search for the specific date (05.05.2015) - correct? I wanted to add the date tags before and after an ever changing date stamp, yet keep the date stamps intact.

Also, where should I apply the folder path?

Thank you for your reply!
# 4  
Old 05-21-2015
Yes, as you gave a hardcoded date Smilie
You could fill that variable with like:
Code:
search=$(date +%F)
search=$(date +'%d.%M.%Y')

You could apply the path either by changing there (cd /path/to) before the execution, or by PREFIX'ing the *html in the command itself.
eg: /path/to/*html.

hth
# 5  
Old 05-21-2015
Hi sea,

Thank you for taking your time replying.

I really need you to hold my hand through this. Could you please provide the command in whole with your latest solution? I don't even know how to apply the path correctly :P

Say my path is "Desktop/folder" - would you include this in the command as well?

Thank you again!
# 6  
Old 05-21-2015
You'll need to experiment a bit on your own... Try
Code:
search=$(date +'%d.%M.%Y')
replace="<date>$search</date>"
sed s,"$search","$replace",g Desktop/folder/*html

This User Gave Thanks to RudiC For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk variable search and line count between variable-search pattern

Input: |Running the Rsync|Sun Oct 16 22:48:01 BST 2016 |End of the Rsync|Sun Oct 16 22:49:54 BST 2016 |Running the Rsync|Sun Oct 16 22:54:01 BST 2016 |End of the Rsync|Sun Oct 16 22:55:45 BST 2016 |Running the Rsync|Sun Oct 16 23:00:02 BST 2016 |End of the Rsync|Sun Oct 16 23:01:44 BST 2016... (4 Replies)
Discussion started by: busyboy
4 Replies

2. Shell Programming and Scripting

awk search and replace nth column by using a variable.

I am passing a variable and replace nth value with the variable. I tried using many options in awk command but unable to ignore the special characters in the output and also unable to pass the actual value. Input : "1","2","3" Output : "1","1000","3" TempVal=`echo 1000` Cat... (2 Replies)
Discussion started by: onesuri
2 Replies

3. Shell Programming and Scripting

How to search, replace and multiply variable within awk?

I have a file that reports the size of disks GB's or TB's - I need the file to report everything in MB's. Here is an extract of the file - the last column is the disk size. 19BC 2363 20G 1AA3 2363 2.93T 1A94 2363 750G Whenever I come across a G I want to delete the G and multiply by... (2 Replies)
Discussion started by: kieranfoley
2 Replies

4. Shell Programming and Scripting

Nested search in a file and replace the inner search

Hi Team, I am new to unix, please help me in this. I have a file named properties. The content of the file is : ##Mobile props east.url=https://qa.east.corp.com/prop/end west.url=https://qa.west.corp.com/prop/end south.url=https://qa.south.corp.com/prop/end... (2 Replies)
Discussion started by: tolearn
2 Replies

5. Shell Programming and Scripting

search replace with loop and variable

Hi, could anyone help me with this, tried several times but still not getting it right or having enough grounding to do it outside of javascript: Using awk or sed or bash: need to go through a text file using a for next loop, replacing substrings in the file that consist of a potentially multi... (3 Replies)
Discussion started by: wind
3 Replies

6. Shell Programming and Scripting

search the pattern in a file and replace with variable already defined previously in csh

I want to replace a certain pattern with the variable already defined. e.g. set path_verilog = /home/priya/bin/verilogfile my file contents are : verilog new verilog is defined here verilog_path_comes I am using the below command sed 's/verilog_path_comes/'$path_verilog'/g' <filename>... (2 Replies)
Discussion started by: nehashine
2 Replies

7. Shell Programming and Scripting

awk - replace number of string length from search and replace for a serialized array

Hello, I really would appreciate some help with a bash script for some string manipulation on an SQL dump: I'd like to be able to rename "sites/WHATEVER/files" to "sites/SOMETHINGELSE/files" within the sql dump. This is quite easy with sed: sed -e... (1 Reply)
Discussion started by: otrotipo
1 Replies

8. Shell Programming and Scripting

Perl search and replace in range using variable

Hi. I have a file with asterisk field separators and backslash line terminators. The first field in each line names the line type. I am trying to process each range separately. Here's what the data looks like: BA*DATA\ LS*DATA1*DATA2*00020*\ TA*DATA1*DATA2*DATA3*\ TA*DATA1*DATA2*DATA3*\... (1 Reply)
Discussion started by: yoi2hot4ya
1 Replies

9. Shell Programming and Scripting

Perl: Search for string on line then search and replace text

Hi All, I have a file that I need to be able to find a pattern match on a line, search that line for a text pattern, and replace that text. An example of 4 lines in my file is: 1. MatchText_randomNumberOfText moreData ReplaceMe moreData 2. MatchText_randomNumberOfText moreData moreData... (4 Replies)
Discussion started by: Crypto
4 Replies

10. Shell Programming and Scripting

search & replace in variable

Can I use search & replace in any variable? Suppose I have one variable named var1 which holds value "abcabc" I need to search 'a' in var1 and want to replace with 'x' like 'xbcxbc'. Is it possible? Can you provide me an example? Malay (3 Replies)
Discussion started by: malaymaru
3 Replies
Login or Register to Ask a Question