Can BASH handle mathematical operations and do a Search & Replace?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Can BASH handle mathematical operations and do a Search & Replace?
# 1  
Old 01-22-2011
Can BASH handle mathematical operations and do a Search & Replace?

Hello,
I have a bunch of xml file that needs to have edits made and I was wondering if a BASH script could handle it.

I would like the script to look within my xml files and replace all integers greater than 5px with a value that is 25% smaller. For example, 100px = 75px. Since the integers I'm looking to change has the following format: xCssStyle="margin-right:3px;">, I'm guessing I should do a grep xCssStyle="margin-right:3px;"> file.xml

I'm just not sure if BASH can look for a string containing "px" as a suffix, do a mathematical calculation, and then do a replace.

Last edited by jl487; 01-22-2011 at 11:03 PM.. Reason: wrong calculation
# 2  
Old 01-22-2011
100px=25px is 25% smaller? looks like it's 75% smaller.
Please post a sample input file and a desired output.
Do the posting using the code tags.
# 3  
Old 01-22-2011
vgersh99, you are correct. Decreasing by 25% will give me 75% of the original.

Here is a sample xml input:
Code:
<xControl xId="" xType="Label" xRow="1" xColumn="1" xCssStyle="margin-right:100px;">
                    <xValue xType="constant">LEVEL1</xValue>
</xControl>
<xControl xId="" xType="Label" xRow="1" xColumn="1" xCssStyle="margin-right:200px;">
                    <xValue xType="constant">LEVEL2</xValue>
</xControl>
<xControl xId="" xType="Label" xRow="1" xColumn="1" xCssStyle="margin-right:4px;">
                    <xValue xType="constant">LEVEL3</xValue>
</xControl>

and the output should be a file with the following changes:
Code:
<xControl xId="" xType="Label" xRow="1" xColumn="1" xCssStyle="margin-right:75px;">
                    <xValue xType="constant">LEVEL1</xValue>
</xControl>
<xControl xId="" xType="Label" xRow="1" xColumn="1" xCssStyle="margin-right:150px;">
                     <xValue xType="constant">LEVEL2</xValue>
 </xControl>
<xControl xId="" xType="Label" xRow="1" xColumn="1" xCssStyle="margin-right:4px;">
                     <xValue xType="constant">LEVEL3</xValue>
 </xControl>

I know the starting point would be to have a FOR loop that will look in the current directory, but I'm not sure how I should proceed from there.

---------- Post updated at 08:28 PM ---------- Previous update was at 08:26 PM ----------

OK, I've been running some tests to see if I can do a mathematical expression followed by a "Search and Replace," but didn't have much luck. Since the pixel sizes I want to change is between 5-200, would it be wise to create a bunch of case statements?
# 4  
Old 01-22-2011
something along these line:
nawk -f jl.awk myFile.xml

jl.awk:
Code:
match($0,"margin-right:[^;][^;]*") {
    str=substr($0,RSTART,RLENGTH)
    gsub("[^0-9]","",str)
    str-=(str/4)
    $0=substr($0,1,RSTART-1) "margin-right:" str "px;" substr($0,RSTART+RLENGTH+1)
}
1

# 5  
Old 01-22-2011
vgersh99, thanks for your help (both in this thread and throughout the forums). After searching the forums, I found something that may work (code that you originally worked on!), but I'm having some trouble modifying it.

What I did was create a file called convert, that has the conversions already made:
6px 5px
7px 5px
8px 6px
9px 7px
10px 8px
11px 8px
12px 9px
.....

I also created another file called file (for testing purposes) to test the replacements. I figured if a keyword is found in column one, replace it with the conversion in column two.
Code:
#!/bin/bash

while read line
do
KEY=$(echo $line | awk '{ print $1 }')
VALUE=$(echo $line | awk '{ print $2 }')
sed -e "s/${KEY}/${VALUE}/g" file > file.tmp
mv file.tmp file
done < convert

Since (I believe) awk is used for files in column format, I don't think that this code will work. Is there a way around this?
# 6  
Old 01-22-2011
have you tried the code I've posted?
This User Gave Thanks to vgersh99 For This Post:
# 7  
Old 01-23-2011
I tried running your code and realized that it prints out the output, rather than saving the file. How come when I do a
Quote:
> file.tmp
command, it doesn't create an output file?

---------- Post updated at 10:58 PM ---------- Previous update was at 10:29 PM ----------

Quote:
Originally Posted by jl487
I tried running your code and realized that it prints out the output, rather than saving the file. How come when I do a command, it doesn't create an output file?
nevermind.... made a dumb mistake. vgersh99 thanks for the help!

---------- Post updated at 11:46 PM ---------- Previous update was at 10:58 PM ----------

sorry for bringing back what I thought was a "dead thread"

I realized that some of the xml files has the following lines:
Code:
<xControl xId="" xType="Label" xRow="1" xColumn="3"  xCssStyle="margin-left:20px;margin-right:3px;">

Therefore, both margin-left and margin-right would need to be changed. What I did was create the following bash file:
Code:
nawk -f jl1.awk myFile.xml > myFile.tmp
mv myFile.tmp myFile.xml

nawk -f jl2.awk myFile.xml > myFile.tmp
mv myFile.tmp myFile.xml

where jl1.awk is the following:
Code:
match($0,"margin-right:[^;][^;]*") {
    str=substr($0,RSTART,RLENGTH)
    gsub("[^0-9]","",str)
    str-=(str/4)
    $0=substr($0,1,RSTART-1) "margin-right:" str "px;\">"
substr($0,RSTART+RLENGTH+1)
}
1

and jl2.awk is:
Code:
match($0,"margin-left:[^;][^;]*") {
    str=substr($0,RSTART,RLENGTH)
    gsub("[^0-9]","",str)
    str-=(str/4)
    $0=substr($0,1,RSTART-1) "margin-left:" str "px;"
substr($0,RSTART+RLENGTH+1)
}
1

jl1.awk seems to work fine, but when I run jl2.awk, the margin-right information is deleted. Why is this?
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Search & Replace

Hi Gurus, I have two files. I want to read sessoin_name from the file1 and replace $Param4 & $Param5 in file2 with connection_name in specified in file1. The file1 will have data in following format File 1 session_name,connection_name s_abcd,Listener_2 s_def,Listener_1 source file... (7 Replies)
Discussion started by: r_t_1601
7 Replies

2. Shell Programming and Scripting

Mathematical Operations on Column

Hi All, I want to perform a mathematical operation on column. Can anyone please help? Here is the sample of operation to be performed: 123 996 100 123 996 200 123 996 200 2015-09-21 123 996 100 123 996 200 123 996 100 What I want is to multiple all values of column # 3 by 100 and... (3 Replies)
Discussion started by: Zaib
3 Replies

3. Shell Programming and Scripting

Search & Replace

Hi all Please can you help me with a script to check several files for the following string: encoding=""and replace it with: encoding="UTF-8"I did the following, : #!/bin/sh string1="encoding=""" string2="encoding="UTF-8" sed 's/'"$string1"'/'"$string2"'/g'but does not work. Please can... (18 Replies)
Discussion started by: fretagi
18 Replies

4. Shell Programming and Scripting

search & replace pattern

Hi, My problem is that I have to search a changing pattern and replace it with the wild card char "*" i/p: 99_*_YYYYMMDD_SRC.txt.tar.gz o/p: 99_*_*_SRC.txt.tar.gz The problem is that YYYYMMDD pattern is not static. It could be YYYYMMDDHHMI or could be YYYYMMDDHHMISS. Can... (10 Replies)
Discussion started by: dips_ag
10 Replies

5. UNIX for Dummies Questions & Answers

Search & Replace

Hi , I ahve a text file which has several instances of the text such as run_time: 09:30 I need to add double quotes before and after the time value i.e: run_time: "09:30" Any suggestions on how to go about the same (4 Replies)
Discussion started by: jobbyjoseph
4 Replies

6. Shell Programming and Scripting

Need help with search & replace

I have a file that has some accent characters in it when viewed in some text editors, but when viewed in vi they come in as ~R and ~U. I need to make a script to remove these characters from the file, but have been unsuccessful. I am not sure how sed or awk, or something similar is viewing them,... (8 Replies)
Discussion started by: tcovert
8 Replies

7. UNIX for Dummies Questions & Answers

String Search & Replace

Hey, I want to have a C program which, for an existing file supplied by the command line argument (E.g. File1.txt) replaces all the occurrences of the words: "We” or “we” by “I” “a” by “the” “A” by “The”. Then print the replaced file. All other characters of the file are to be left... (1 Reply)
Discussion started by: IwishIknewC
1 Replies

8. 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

9. Shell Programming and Scripting

Search & replace

Is there any way we can achieve search & replace with awk? I could achieve the same with sed in following way - sed 's/A/B/g' file1 > file2 But the same regex if I try with using awk following way, awk 's/A/B/g' file1 > file2 it gives me Syntax error. I strongly believe I am... (1 Reply)
Discussion started by: videsh77
1 Replies

10. Shell Programming and Scripting

Help, sed search&replace

Plzzzz, tell me some script about this... What does this mean ? sed '/^ */s///' sed '/^/s// /' and why it's diferent ??? sed '/ */s// /g' and sed 's/ */ /g'. It's all the same ??? Thanks you very much (2 Replies)
Discussion started by: mle
2 Replies
Login or Register to Ask a Question