Delete lines based on Rules


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Delete lines based on Rules
# 1  
Old 04-02-2013
Delete lines based on Rules

Hi My requirement is very simple .


I juts need to delte some lines from a file.


here comes theactual scenario

I have some data in file like say

Code:
 
srinivasa prabhu kumar antony 
srinivas king prabhu antony 
srinivas prabhu king yar venkata 
venkata kingson srinivas prabhu 
prabhu srinivas prabhu kingson


I want to delete the lines which contain both the words srinivas and prabhu.


please help

Moderator's Comments:
Mod Comment edit by bakunin: edited the title to make it better searchable. In general (and in sight of your many typos) you might consider exerting as much effort into writing your posts as you perhaps want us to show in answering them

Last edited by bakunin; 04-02-2013 at 03:50 AM..
# 2  
Old 04-02-2013
Quote:
Originally Posted by ptappeta
I want to delete the lines which contain both the words srinivas and prabhu
You can do this using sed with two nested ranges:

Code:
sed '/srinivas/ {
                  /prabhu/d
                }' /path/to/input

I hope this helps.

bakunin
# 3  
Old 04-02-2013
Hi i tried this and is not workig ...means the lines are not getting deleted.
# 4  
Old 04-02-2013
Are you getting the output correctly or is that the file remains unchanged? To edit the file inline, you need to use sed -i ...

--ahamed
# 5  
Old 04-02-2013
In your above example, every single line should be deleted, meaning you would have no output at all.

Try also
Code:
$ grep -Ev "srinivas.*prabhu|prabhu.*srinivas" file

# 6  
Old 04-02-2013
Code:
awk '!(/srinivas/ && /prabhu/)' file

I am not sure if line #1 has a typo srinivasa prabhu kumar antony
If you only like to remove line containing exact words like srinivas and not srinivasa, use this

Code:
awk '!(/\<srinivas\>/ && /\<prabhu\>/)' file


Last edited by Jotne; 04-02-2013 at 06:26 AM..
# 7  
Old 04-02-2013
Quote:
Originally Posted by ptappeta
Hi i tried this and is not workig ...means the lines are not getting deleted.
Note that the output of sed always goes to <stdout>, which in most cases is the terminal. Try it, watch the output and if you are satisfied redirect it to some file and move this file over the original file:

Code:
sed '<commands>' /path/to/input > path/to/output
mv path/to/output /path/to/input

Quote:
Originally Posted by ahamed101
To edit the file inline, you need to use sed -i ...
Yes and no. You should not use this feature, even if it is there, which is solely(!) with the GNU-variants of sed.

I hope this helps.

bakunin
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Delete lines based on pattern match

BASH in Solaris 10 I have a log file like below. Whenever the pattern ORA-39083 is encountered, I want to delete the line which has this pattern and 3 lines below it. $ cat someLogfile.txt ORA-39083: Object type OBJECT_GRANT failed to create with error: ORA-01917: user or role 'CMPA' does... (4 Replies)
Discussion started by: kraljic
4 Replies

3. Shell Programming and Scripting

Delete lines from file based on condition

I want to keep last 2 days data from a file and want to delete others data from the file. Please help me. Sample Input # cat messages-2 Apr 15 11:25:03 test1 kernel: imklog 4.6.2, log source = /proc/kmsg started. Apr 15 11:25:03 test1 rsyslogd: (re)start Apr 16 19:42:03 test1 kernel:... (2 Replies)
Discussion started by: makauser
2 Replies

4. Shell Programming and Scripting

Delete specific lines from files based on another file

I have some text files in a folder named ff as follows. I need to delete the lines (in-place editing)in these files based on another file aa.txt. 32bm.txt: 249 253 A P - 0 0 8 0, 0.0 6,-1.4 0, 0.0 2,-0.4 -0.287 25.6-102.0 -74.4 161.1 37.1 13.3 10.9 250... (2 Replies)
Discussion started by: aden
2 Replies

5. UNIX for Advanced & Expert Users

Delete all ebtables rules

Hi All, I configured a bridged debian firewall using bridging utilities. This works fine & I have been monitoring all users connecting to the internet & bandwidth usage. There are 2 interfaces eth0 & eth1 which i have configured as bridged interface br0. Today, I installed squid on the... (1 Reply)
Discussion started by: coolatt
1 Replies

6. Shell Programming and Scripting

Need To Delete Lines Based On Search Criteria

Hi All, I have following input file. I wish to retain those lines which match multiple search criteria. The search criteria is stored in a variable seperated from each other by comma(,). SEARCH_CRITERIA = "REJECT, DUPLICATE" Input File: ERROR,MYFILE_20130214_11387,9,37.75... (3 Replies)
Discussion started by: angshuman
3 Replies

7. Shell Programming and Scripting

sed/awk : how to delete lines based on IP pattern ?

Hi, I would like to delete lines in /etc/hosts on few workstations, basically I want to delete all the lines for a list of machines like this : for HOST in $(cat stations.lst |uniq) do # echo -n "$HOST" if ping -c 1 $HOST > /dev/null 2>&1 then HOSTNAME_val=`rsh $HOST "sed... (3 Replies)
Discussion started by: albator1932
3 Replies

8. Shell Programming and Scripting

Delete lines based on line number

I have a file with ~200K lines, I need to delete 4K lines in it. There is no range. I do have the line numbers of the lines which I want to be deleted. I did tried using > cat del.lines sed '510d;12d;219d;......;3999d' file > source del.lines Word too long. I even tried... (2 Replies)
Discussion started by: novice_man
2 Replies

9. Shell Programming and Scripting

Delete new lines based on search criteria

Hi all! A bit of background: I am trying to create a script that formats SQL statements. I have gotten so far as to add new lines based on certain match criteria like commas, keywords etc. In the process, I end up adding newlines where I don't want. For example: substr(colName, 1, 10)... (3 Replies)
Discussion started by: jayarkay
3 Replies

10. UNIX for Dummies Questions & Answers

Delete lines with duplicate strings based on date

Hey all, a relative bash/script newbie trying solve a problem. I've got a text file with lots of lines that I've been able to clean up and format with awk/sed/cut, but now I'd like to remove the lines with duplicate usernames based on time stamp. Here's what the data looks like 2007-11-03... (3 Replies)
Discussion started by: mattv
3 Replies
Login or Register to Ask a Question