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.
The code in my previous mail should be entered all on one line if you're doing it from the command line. To have the output go to a file, use a redirect "> NEWFILE".
Alternatively you can put all the code between the single quotes into a text file and then use the command as follows:
Cant I do a replace without using an intermediary file? Because newfile needs to be renamed into file and this script is run as user apache (called via PHP).
So each time anything is done, such as renaming file, etc, the owner group is changed to root.root so the php script cannot act on it further.
This is why i needed something that can be done while keeping same file name..
like a replace command..
Step 1: Look for string email@domain.com:127.0.0.1:1
replace it with #email@domain.com:127.0.0.1:1
this will be called by php and the permissions, owner should remain same..
I have a same question, but with different pattern I have two files and the files have following lines
File-A
---------
A
B
C
D
E
F
G
H
I
J
K
L
M
File-B
-------
A
B
C
D
E
I want to delete the lines which are in File-B (A,,B,C,D,E) from File-A. I mean after executing the command the output of File-B would be F,G,H,I,J,K,L,M.
Please help me out in this.
Thanks,
Suraj
Last edited by suraj_sharma1981; 02-22-2007 at 04:56 AM.
This will work for the example given by suraj_sharma1981 because the two files are both sorted in the same way (actually, it won't quite work because File-A and File-B appear to have gotten mixed up in Suraj's question). It won't necessarily work if the two files are sorted differently. For example
Cant I do a replace without using an intermediary file? Because newfile needs to be renamed into file and this script is run as user apache (called via PHP).
So each time anything is done, such as renaming file, etc, the owner group is changed to root.root so the php script cannot act on it further.
This is why i needed something that can be done while keeping same file name..
like a replace command..
Step 1: Look for string email@domain.com:127.0.0.1:1
replace it with #email@domain.com:127.0.0.1:1
this will be called by php and the permissions, owner should remain same..
thanks!!
You can use `-i' flag in sed. One more interesting approach is to use the `ed' editor for doing changes in your file.
Here are simple examples:
Code:
$ cat dat
username@email.com:149.0.3.4:1
username1@email.com:149.0.3.4:1
username1@email.net:149.0.3.4:1
username1@email.edu:149.0.3.4:1
$ str=name@
$ (echo "g/$str/s/username/john/"; echo ',p') | ed -s dat
john@email.com:149.0.3.4:1
username1@email.com:149.0.3.4:1
username1@email.net:149.0.3.4:1
username1@email.edu:149.0.3.4:1
$ # `s' -- repeates last substitute
$ (echo "0s/^/#/"; echo 'g/net\|com/s'; echo ',p')|ed dat
#username@email.com:149.0.3.4:1
#username1@email.com:149.0.3.4:1
#username1@email.net:149.0.3.4:1
username1@email.edu:149.0.3.4:1
$ # `w' saves file; this will delete 3rd line and save result
$ (echo 'g/net/d'; echo 'w' )| ed dat
$ cat script
g/net/ s/1/2/\
s/email/other_host/
g/edu/s/1/3/
w
$ # patch file
$ ed -s dat < script
$ cat dat
username@email.com:149.0.3.4:1
username1@email.com:149.0.3.4:1
username2@other_host.net:149.0.3.4:1
username3@email.edu:149.0.3.4:1
Read info ed and man ed.
This approach may be useful if you have many lines to modify. In this case you can generate a ed script and then run command `ed dat < script' to patch your file.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.