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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
|
06-25-2004, 06:23 AM
|
#1
|
Member
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62
Rep:
|
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!
|
|
|
06-25-2004, 07:13 AM
|
#2
|
Member
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507
Rep:
|
awk 'match($0,"username@email.com") == 0 {print $0}' FILENAME > NEWFILENAME
John
|
|
|
06-25-2004, 08:40 AM
|
#3
|
Member
Registered: Feb 2004
Posts: 273
Rep:
|
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
|
|
|
06-25-2004, 08:46 AM
|
#4
|
Member
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62
Original Poster
Rep:
|
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!!!
|
|
|
06-25-2004, 09:23 AM
|
#5
|
Member
Registered: Feb 2004
Posts: 273
Rep:
|
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]
|
|
|
06-25-2004, 09:49 AM
|
#6
|
Member
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507
Rep:
|
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
|
|
|
06-29-2004, 01:38 AM
|
#7
|
Member
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62
Original Poster
Rep:
|
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!
|
|
|
06-29-2004, 04:24 AM
|
#8
|
Member
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507
Rep:
|
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
|
|
|
06-29-2004, 04:41 AM
|
#9
|
Member
Registered: Feb 2003
Distribution: ubuntu 6.06, ubuntu 7.04 AMD 64bit, 7.10 AMD 64bit
Posts: 62
Original Poster
Rep:
|
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!!
|
|
|
06-29-2004, 05:13 AM
|
#10
|
Member
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507
Rep:
|
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
|
|
|
02-22-2007, 04:54 AM
|
#11
|
LQ Newbie
Registered: Feb 2007
Posts: 1
Rep:
|
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.
|
|
|
02-22-2007, 05:58 AM
|
#12
|
Member
Registered: Jun 2003
Location: Dublin, Ireland
Distribution: Slackware, LFS, Ubuntu, RedHat, Slamd64
Posts: 507
Rep:
|
comm -13 File-A File-B > TEMP ; mv TEMP File-B
|
|
|
02-23-2007, 02:35 PM
|
#13
|
LQ Newbie
Registered: Feb 2007
Distribution: mostly Debian
Posts: 14
Rep:
|
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
|
|
|
02-23-2007, 06:17 PM
|
#14
|
Member
Registered: Mar 2006
Location: Ekaterinburg, Russia
Distribution: Debian, Ubuntu
Posts: 709
|
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.
|
|
|
12-26-2007, 08:09 AM
|
#15
|
LQ Newbie
Registered: Feb 2007
Location: NIT, Bhopal
Distribution: Fedora
Posts: 16
Rep:
|
Guys please tell me how do I delete a particular string.. Like for
Code:
echo abcdef.abc | <additional command>
should return only i.e delete everything after the dot..
Is it possible with tr
|
|
|
All times are GMT -5. The time now is 05:58 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|