LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 06-25-2004, 06:23 AM   #1
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Rep: Reputation: 15
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!
 
Old 06-25-2004, 07:13 AM   #2
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

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

John
 
Old 06-25-2004, 08:40 AM   #3
linuxxed
Member
 
Registered: Feb 2004
Posts: 273

Rep: Reputation: 30
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
 
Old 06-25-2004, 08:46 AM   #4
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Original Poster
Rep: Reputation: 15
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!!!
 
Old 06-25-2004, 09:23 AM   #5
linuxxed
Member
 
Registered: Feb 2004
Posts: 273

Rep: Reputation: 30
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]
 
Old 06-25-2004, 09:49 AM   #6
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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
 
Old 06-29-2004, 01:38 AM   #7
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Original Poster
Rep: Reputation: 15
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!
 
Old 06-29-2004, 04:24 AM   #8
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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
 
Old 06-29-2004, 04:41 AM   #9
topcat
Member
 
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62

Original Poster
Rep: Reputation: 15
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!!
 
Old 06-29-2004, 05:13 AM   #10
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
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
 
Old 02-22-2007, 04:54 AM   #11
suraj_sharma1981
LQ Newbie
 
Registered: Feb 2007
Posts: 1

Rep: Reputation: 0
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

Last edited by suraj_sharma1981; 02-22-2007 at 04:56 AM.
 
Old 02-22-2007, 05:58 AM   #12
jkobrien
Member
 
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507

Rep: Reputation: 30
comm -13 File-A File-B > TEMP ; mv TEMP File-B
 
Old 02-23-2007, 02:35 PM   #13
jr1
LQ Newbie
 
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14

Rep: Reputation: 0
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
 
Old 02-23-2007, 06:17 PM   #14
firstfire
Member
 
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 608

Rep: Reputation: 352Reputation: 352Reputation: 352Reputation: 352
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.

Last edited by firstfire; 02-23-2007 at 06:24 PM.
 
Old 12-26-2007, 08:09 AM   #15
Anant Khaitan
LQ Newbie
 
Registered: Feb 2007
Location: NIT, Bhopal
Distribution: Fedora
Posts: 16

Rep: Reputation: 0
Unhappy

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
 
  


Reply

Tags
awk, delete, file, line, sed, string


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
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to replace string pattern with multi-line text in bash script? brumela Linux - Newbie 6 04-21-2011 06:56 AM
unix shell script:How to delete the first line in a file?? rche3252 Programming 6 03-03-2010 07:32 AM
printing pattern match and not whole line that matches pattern Avatar33 Programming 13 05-06-2009 06:17 AM
Procmail: match pattern then pass to shell script essdeeay Linux - Software 1 11-08-2004 02:19 PM
How to delete a line from a text file with shell script programming Bassam General 1 01-28-2004 08:51 PM


All times are GMT -5. The time now is 12:51 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration