ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Ok, this is what I have right now. Normally, if I need help with anything, I learn it on my own. Well, I thought I knew sed, and apparently I don't... I tried writing this for someone else, and this has given me trouble, so since the user pretty much figured it out on his own, here it goes.
Say VARR=1, so VARX and VARY contain the above text, appended by 1. What I am trying to do is replace the text "defaults.ctl.card 0" by VARX and "defaults.pcm.card 0" by VARY. The contents of FILE1 is the file being used to search for both text fields, and FILE2 is the output file. I tried using single quotes, double quotes, and a mixture of both, and no go whatsoever. So my question... What is the proper way of searching for text within a file and replacing with a variable?
You don't (shouldn't) need the single quotes at all in that case.
I suspect you need to look more carefully at the regex on the LHS of the substitution.
Without seeing the actual input and output, I can only guess at the real problem. But I think using * alone won't match what you want. You need to use .* instead. Otherwise, as long as you use double-quotes, you shouldn't have any problem with the variable expansion.
grep can take the filename as an argument, so you don't need cat.
cut -c 1-2 will also give you the blank space in front of the number, and I think you only want the number itself, so I used -c 2 instead.
You can bypass the intermediate variables and just use the original value in your sed expressions.
Finally, with a little regex back-referencing and some grouping, gnu sed will allow you to do the whole thing in a single expression. Or you could also do something like this, which should be more portable:
Without seeing the actual input and output, I can only guess at the real problem. But I think using * alone won't match what you want. You need to use .* instead. Otherwise, as long as you use double-quotes, you shouldn't have any problem with the variable expansion.
grep can take the filename as an argument, so you don't need cat.
cut -c 1-2 will also give you the blank space in front of the number, and I think you only want the number itself, so I used -c 2 instead.
You can bypass the intermediate variables and just use the original value in your sed expressions.
Finally, with a little regex back-referencing and some grouping, gnu sed will allow you to do the whole thing in a single expression. Or you could also do something like this, which should be more portable:
Wow, thanks for the clear description. One thing though.... you mentioned the following - "cut -c 1-2 will also give you the blank space in front of the number, and I think you only want the number itself, so I used -c 2 instead." On my machine, even though your example worked flawlessly, my cut command worked too. But I'm not complaining, and thank you for the example. I learned a lot from that.
Well, I was basing it on the output I got from my own /proc/asound/cards, which shows a space at the beginning of each card line. I suppose it's possible you get something different. More likely it's actually there in yours too, but isn't affecting anything, as the size of the blank space doesn't usually matter in most configuration-type files.
But since you only want to extract a single digit, it's hard to see how having it cut two characters would be desirable no matter what the actual effect.
I know what I did - I copied the cut command out of one of my other scripts and pasted it in (Yes, I was that lazy - I was drinking....) and when I did paste it in, it worked. I guess bash took the space out. Or maybe gremlins inside my computer saw the space and killed it? Oh well, works as advertised
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.