Computing.Net > Forums > Unix > how to seach and replace a value using shell

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

how to seach and replace a value using shell

Reply to Message Icon

Name: pacsailor
Date: October 28, 2009 at 17:32:37 Pacific
OS: Linux
Product: Red hat / Fedora 10
Subcategory: General
Tags: linux, shell script
Comment:

I hope someone can help me in writing a shell script to do the following,

- in a file there are multiple lines, and some of them have the following,
Mxxx a=6 b=2 c=2

- I want to parse this file and if the line has parameter "b" then it will chnage the values as follows,

Mxxx a=6/b b=2 c=2*b



Sponsored Link
Ads by Google

Response Number 1
Name: tvc
Date: October 31, 2009 at 06:40:12 Pacific
Reply:

Can you clarify the question ?


0

Response Number 2
Name: pacsailor
Date: October 31, 2009 at 07:34:40 Pacific
Reply:

If there is a parameter "b" on a line, there will always be two other parameters on the same line "a" and "c". Then take the value of parameter "b" which is 2 in this example, and divide the value of "a" ( which is 6 in this case) by the value of "b" (6/2), and multiply the value of "c" (which is 2 in this case) by the value of "b" (2*2).

The line that contains these parameters will look like this,

Mxxx a=6 b=2 c=2

The values of b and c are always integers, but sometimes indicated by 6.0 instead of just 6.

The value of "a" can be a decimal like 4.5u where u is for micron, sometimes the value of "a" is a variable that is defined somewhere else in the file.

Then do the same procedure to every line that has a parameter "b" in the whole file.


0

Response Number 3
Name: tvc
Date: November 7, 2009 at 02:54:09 Pacific
Reply:

The decimals are the hardest part, since maths on unix tend to be non-decimal based. Let's start with non-decimals:

You can get the values of those letters you look for (a, b and c) like this:

<whatever command> | awk -F"a=" ' { print $2 } ' | awk ' { print "a=" $1 } '


0

Sponsored Link
Ads by Google
Reply to Message Icon

Related Posts

See More


printing all lines after ... Separating the Columns ba...


Use following form to reply to current message:

Login or Register to Reply
LoginRegister


Sponsored links

Ads by Google


Results for: how to seach and replace a value using shell

how to unzip and find a file? www.computing.net/answers/unix/how-to-unzip-and-find-a-file/8343.html

Search string and how to get it replaced www.computing.net/answers/unix/search-string-and-how-to-get-it-replaced/2517.html

How to disconnect DB in a UNIX script www.computing.net/answers/unix/how-to-disconnect-db-in-a-unix-script/8505.html