[SOLVED] How to parse and modify these keywords using shell script?
Hello,
Code:
system information I don't think that's good format, but I cannot change that format. I needed to modify the model name, 'model = xxx' as 'model = abc'. So I tried like the following. Code:
sed -i "/system information/,/model = /s/model = .\+/model = abc/" filename And I don't know how to parse and modify 'model = iii' and 'model = ddd'. The only clue to parse 'model = ddd' is the second 'model = ' after the second 'system information'. But how to parse the second keyword? Is it possible with 'sed'? I sometimes have to modify the information of the file. using shell script if possible. Python is ok. (Shell script is better for me.) Thank you. |
Quote:
Code:
sed -e 's|^\([\t ]*model[\t ]*=[\t ]*\)xxx[\t ]*$|\1abc|' -i file Code:
awk -v "old=xxx" -v "new=abc" '($2 == "=" && tolower($1) == "model" && tolower($3) == tolower(old)) { $3 = new } { print $0 }' infile Code:
#!/bin/bash If you need to limit the replacement to under a specific header, and/or your value strings may contain whitespace, I'd use a bit more complex awk part in the bash + awk script: Code:
#!/bin/bash Note that the way I redefine RS in the awk scripts mean that it accepts the input file using any newline convention, converting them to standard Unix newlines (\n). Hope this helps. |
I must have missed something here?? Is there a reason something simple like:
Code:
sed -i '/model/s/xxx/abc/' file Based on the description I presume that the model to name (ie. xxx) is a unique combination, ie. no 2 models will have xxx as a name? Also, @ Nominal, just a query, as I have seen you use it before, what is the difference between: Code:
RS="(\r\n|\n\r|\r|\n)" |
Code:
$ ruby -ane '$F[-1]="abc" if /^model.*=.*xxx$/;puts $F.join("\s")' file |
Quote:
Code:
$ cat file |
@kurumi - thanks for the explanation. It makes sense, but I believe the idea was to catch the scenario if the file is written in dos :
Quote:
|
Thank you for your answers.
I am so happy that you gave me many answers.
Thank you. But I should have told you more. The above information is just an example. Code:
system information I just know the format. So the script should not parse the value, 'xxx' or 'yyy' from the file. That's why I mentioned 'the second model' and 'the second system information'. Please, give me more help. Thank you. |
Quote:
Quote:
_ _ _ As you might quess, I thought a bit further about processing configuration files, and reread the relevant sections in the GNU Awk User Manual. I realized there is a very simple way to set up RS and FS, and use RT and OFS to handle all this stuff. Here is my skeleton awk script for parsing name=value -type configuration files. It automatically retains all whitespace, including newline conventions, and even when replacing the value. Furthermore, it fully supports shell-type comments, as long as they either start at the beginning of the line, or are preceded by whitespace. The comments are not shown in $0, and your rules need not worry about them at all. Code:
BEGIN { |
Well whether you use Nominal's code (looks cool btw), sed or ruby, you are going to need to know something about what you need changed
otherwise everywhere that 'model' appears the corresponding 'name' will be changed. I am not saying you need to know the exact name, but you would have to know at least which section and which entry, ie first system information section and the second model entry. So I think you will need to know your data a little better before we can truly help you further. |
Quote:
Code:
#!/bin/bash Code:
./change-model.bash 2 3 'The New Model' models.txt |
Nominal's new script goes only further to proving my point that without knowing something about the data, ie second section and third model along, that all the solutions
are kind of void until you bed down some more particulars. |
Quote:
corone, could you elaborate a bit on exactly what you are doing? For example, if we knew that you need to modify say a hardware manifest, we could tell you that you'd save a lot of time by splitting the original manifest(s) into manageable parts (by, say, the header line, or by identifier in the section) -- for example, by splitting it into multiple files. Then it'd be much easier to modify each part separately. If you use a safe empty temporary directory to work in, you can name the parts as 001.part-identifier, 002.part-identifier, and so on. Then you can specify the part file name as *.part-identifier for each modification, to only modify specific parts, regardless of their order in the original manifest. Finally, merging the parts back to a single manifest is trivial: cat *.* (in the otherwise empty temporary directory). The key is that the more complete picture of the problem we have, the better the solution. It is always a good idea to describe what you've already tried. However, usually that's not enough. Your approach may be inefficient, for example. Therefore telling us also what is the entire task you wish to accomplish, not just the tricky bit you're having a problem with, is important for you to get a good solution. It also makes it much easier for others to give you advice. Sure, you'll probably get also advice that is not suitable for you for various reasons, but it never hurts to see how those solutions tick. You may be able to use some nugget in them to improve your solution. |
Thank you, Nominal Animal!!
Code:
./change-model.bash 2 3 'The New Model' models.txt That is exactly what I want. I can never thank you enough. I really really appreciate your helping me out. I hope you to see my thanks. And I wish I work with you in same office. =] |
Thank you for your advice, grail.
This is a little more expatiation. There are four devices. Code:
┌────┐ Code:
system information The second 'model =' is the model name of the Device2. The third 'model =' is the model name of the Device3. The fourth 'model =' is the model name of the Device4. When the device is changed, the file should be modified automatically using a script. I don't know the previous model name for the Device1. I just know which device is changed as which model. I think the format is very stupid. The following would be much better. Code:
system information Code:
system information |
In a way you have demonstrated that there is more information in that the devices run from top to bottom in line with the model and spec information.
This now means that your script simply needs to know which occurrence of model it is to change. Hence if you pass the number 2 it will wait until it finds the second occurrence of model before initiating a change and exit immediately after. So using Nominal's script you really only need the INDEX and NEWMODEL variables. Glad you got it sorted :) |
[solved]
Thank you for your help.
|
[duplicated]
[ d u p l i c a t e d ]
|
I am not sure I see how this change has made any difference to my last post?
The model's still appear in a specific order, according to your most recent example, so again you still only need to say wait until model X is reached and then perform the change and exit. Or have you now also made changes to the format compared to post #14? |
All times are GMT -5. The time now is 11:27 AM. |