LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Shell Script to Delete line if pattern exists (http://www.linuxquestions.org/questions/programming-9/shell-script-to-delete-line-if-pattern-exists-197539/)

topcat 06-25-2004 07:23 AM

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!

jkobrien 06-25-2004 08:13 AM

awk 'match($0,"username@email.com") == 0 {print $0}' FILENAME > NEWFILENAME

John

linuxxed 06-25-2004 09:40 AM

Re: Shell Script to Delete line if pattern exists
 
Quote:

Originally posted by topcat
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!


sed '/^\[email\]username\@email\.com\[\/email\]/d' FILENAME

topcat 06-25-2004 09:46 AM

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!!!

linuxxed 06-25-2004 10:23 AM

Quote:

Originally posted by topcat
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!!!



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]

jkobrien 06-25-2004 10:49 AM

Q1 & Q2

Code:

awk 'match($0,"username@email.com") != 0 {print "#"$0}
        match($0,"username@email.com") == 0 {print $0}
        END {print "username333@mydomain.com:149.0.3.4:1"}' FILENAME

John

topcat 06-29-2004 02:38 AM

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!

jkobrien 06-29-2004 05:24 AM

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

topcat 06-29-2004 05:41 AM

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!!

jkobrien 06-29-2004 06:13 AM

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

suraj_sharma1981 02-22-2007 05:54 AM

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

jkobrien 02-22-2007 06:58 AM

comm -13 File-A File-B > TEMP ; mv TEMP File-B

jr1 02-23-2007 03:35 PM

Quote:

Originally Posted by jkobrien
comm -13 File-A File-B > TEMP ; mv TEMP File-B

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
Code:

$ cat A
b
a
c
$ cat B
a
c
$ comm -13 B A
b
a


firstfire 02-23-2007 07:17 PM

Quote:

Originally Posted by topcat
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.

Anant Khaitan 12-26-2007 09:09 AM

Guys please tell me how do I delete a particular string.. Like for
Code:

echo abcdef.abc | <additional command>
should return only
Code:

abcdef
i.e delete everything after the dot..
Is it possible with tr


All times are GMT -5. The time now is 02:07 AM.