[SOLVED] How to edit some specific lines of a file using script
Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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.
Here's one that's a bit more restrictive then shivaa's solution (which changes every server to new_server and port to new_port, not just those belonging to the lines shown):
Code:
sed -r 's%((http|https|ftp)://user:pass@)server:port%\1new_server:new_port%' bash.bashrc
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep:
Thanks for replying,
But this is not what i wanted. This solution assumes that i know the value of previous "server" to replace with "new_server".
Real problem is that i dont know that value of "server". Then how do i do it?
I tried using "*" but i didn'e succed.
Thanks
@unkn(0)wn: A script could do a better job. But answer few questions:
- How large that input file is?
- Server names are strings or in form of IP addresses in input file?
- Do you want to replace all server/port strings with only a new_server/new_port string OR all different server/port string with different new strings?
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117
Original Poster
Rep:
Thanks both of you!
Actually i am writing a script for changing proxies in multple system files. this is where i was stuck.ll ol
1.Few short files.
2. IP addresses.
3. All old serverort will be same. Thus they all will be replaced by new ones.
Finally its working
Code:
sed -r 's%((http|https|ftp)://user:pass@).*:.*%\1new_server:new_port%' hello > hellonew
This did the trick!
And one extra finishing question, if my new_server contains "."(dots), sed rejects it, how to i pass dots in sed.
Anyway Thank both of you for your time.
Last edited by unkn(0)wn; 01-23-2013 at 01:11 PM.
Reason: New post before posting
Can you put up the [SOLVED] tag.
- above the first post -> Please Mark this thread as solved if you feel a solution has been provided.
- -or- -
- first post -> Thread Tools -> Mark this thread as solved
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.