Shell Script to Delete line if pattern exists
Hello,
I would like to know how i can write a shell script to delete a line if a particular pattern exists? E.g. I have a text file with multiple lines. Say 1000s. in the following pattern. 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 If the pattern username@email.com exists then the line "username@email.com:149.0.3.4:1 should be deleted from the file. thanks! |
awk 'match($0,"username@email.com") == 0 {print $0}' FILENAME > NEWFILENAME
John |
Re: Shell Script to Delete line if pattern exists
Quote:
sed '/^\[email\]username\@email\.com\[\/email\]/d' FILENAME |
Thank you!!!
I have a couple of other questions on the same file. hope you can help... Question1: Now, what if i want to look for the string and just add a # to the start of the line. #username@email.com:149.0.3.4:1 Question2: Now what if I want to add a line to the file at the very end... username333@mydomain.com:149.0.3.4:1 thanks!!! |
Quote:
Question 1: awk '{ print "\#"; print $0}' [FILENAME] or in sed sed 's/\(BEGINPART\)\(MATCHPART\)\(ENDPART\)/\#\1\2\3/g' [FILENAME] substitute BEGINPART MATCHPART and ENDPART accordingly Read the sed FAQ. There are many ways to do it. Also read GNU awk guide from gnu.org Question 2: echo 'username333@mydomain.com:149.0.3.4:1' >> [FILENAME] |
Q1 & Q2
Code:
awk 'match($0,"username@email.com") != 0 {print "#"$0} |
prints to screen instead of file..
i tried this
awk 'match($0,"username@email.com") != 0 {print "#"$0} it prints to the screen instead of replacing the current string (username@email.com) with the string #username@email.com how do i go about doing it? thanks! |
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: awk -f CODEFILE FILENAME > NEWFILE John |
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 'chown' to change the ownership of a file, so your script would be something like this...
awk/sed to make the changes to your file mv -f NEWFILE OLDFILE chown apache NEWFILE John |
Shell script
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 |
comm -13 File-A File-B > TEMP ; mv TEMP File-B
|
Quote:
Code:
$ cat A |
Quote:
Here are simple examples: Code:
$ cat dat 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. |
Guys please tell me how do I delete a particular string.. Like for
Code:
echo abcdef.abc | <additional command> Code:
abcdef Is it possible with tr |
All times are GMT -5. The time now is 01:21 AM. |