LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 01-23-2013, 12:48 PM   #1
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Rep: Reputation: Disabled
How to edit some specific lines of a file using script


i wish to edit a file and write some new values at place of previous one.

Eg. lets assume our file "bash.bashrc" contains

Code:
.
.
.
export http://user:pass@server:port
export https://user:pass@server:port
export ftp://user:pass@server:port
Now given that i dont know what values do server and port have. i wish to update these srings with new values.

Code:
.
.
.
export http://user:pass@new_server:new_port
export https://user:pass@new_server:new_port
export ftp://user:pass@new_server:new_port
How do i do that?
i have rad something about sed but cant get it to use because of dots in ip addresses.

Help please!
 
Old 01-23-2013, 01:28 PM   #2
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
It can be simply done, as:-
Code:
~$ sed 's/server/new_server/; s/port/new_port/' bash.bashrc
Also you can use:-
Code:
~$ awk 'BEGIN{FS=" "}; {gsub(/server/,"new_server",$0); gsub(/port/,"new_port",$0); print $0}' bash.bashrc
For understanding, go through manual (see here) or online tutorials of sed/awk.

Last edited by shivaa; 01-23-2013 at 01:39 PM. Reason: Typo+Code updated
 
Old 01-23-2013, 01:50 PM   #3
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
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
 
1 members found this post helpful.
Old 01-23-2013, 01:51 PM   #4
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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
 
Old 01-23-2013, 02:00 PM   #5
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390
Give this a try:
Code:
sed -r 's%((http|https|ftp)://user:pass@).*:port%\1new_server:new_port%' infile
 
1 members found this post helpful.
Old 01-23-2013, 02:05 PM   #6
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
@druuna: Thanks for modification.

@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?
 
Old 01-23-2013, 02:09 PM   #7
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
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 02:11 PM. Reason: New post before posting
 
Old 01-23-2013, 02:20 PM   #8
shivaa
Senior Member
 
Registered: Jul 2012
Location: Grenoble, Fr.
Distribution: Sun Solaris, RHEL, Ubuntu, Debian 6.0
Posts: 1,800
Blog Entries: 4

Rep: Reputation: 286Reputation: 286Reputation: 286
Quote:
And one extra finishing question, if my new_server contains "."(dots), sed rejects it, how to i pass dots in sed.
Can you try with escaping special characters, like
Code:
192\.1\.2\.3
 
1 members found this post helpful.
Old 01-23-2013, 02:27 PM   #9
unkn(0)wn
Member
 
Registered: Aug 2011
Distribution: Slackware 14, Debian 7.0.0 Wheezy, Windows 7, Windows 8
Posts: 117

Original Poster
Rep: Reputation: Disabled
Thanks, everything is working like a charm.
Solutions provided above are acceptable.
 
Old 01-23-2013, 02:45 PM   #10
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390Reputation: 2390


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
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
awk commands/script to extract lines with a specific format oldfogey Linux - Software 5 10-02-2012 02:50 PM
bash script to count number of lines with a specific property7 hhamid Programming 10 08-13-2010 02:35 AM
Printing specific lines of a file using script. barunparichha Linux - Software 6 05-20-2009 01:31 AM
Substitute specific lines with lines from another file rahmathullakm Programming 4 01-10-2009 06:47 AM
[bash] edit lines in a file pieperp Programming 4 01-31-2007 09:56 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:58 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration