LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 04-09-2011, 09:45 AM   #1
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Rep: Reputation: 0
Bash How to delete only one line from a file


Hi, i have a big problem.

I would like to delete a single line from a file that contains many lines passing through the same values ​​as the two parameters. Again, I would like to delete a single line and not all those that contain parameters. How can I make bash?
 
Old 04-09-2011, 09:47 AM   #2
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
Quote:
Originally Posted by wizard211990 View Post
Hi, i have a big problem.

I would like to delete a single line from a file that contains many lines passing through the same values ​​as the two parameters. Again, I would like to delete a single line and not all those that contain parameters. How can I make bash?
So you want a script that takes two parameters, and deletes the lines from a file that do not contain both of the strings that were passed as parameters?
 
Old 04-09-2011, 09:53 AM   #3
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Original Poster
Rep: Reputation: 0
No, I want to delete one line among all those who have those parameters.

For Example

1 parameter: Queen 2 parameter: News of the World

Queen News of the World
Beatles Revolver
Queen News of the World
....
...
I want to delete only the first Queen News of the World but not the last
 
Old 04-09-2011, 10:18 AM   #4
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Original Poster
Rep: Reputation: 0
I have try with awk, sed grep but nothing...Help Me...
 
Old 04-09-2011, 10:20 AM   #5
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723Reputation: 723
So delete the first line that has both strings?
 
Old 04-09-2011, 10:22 AM   #6
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Here is an awk solution:
Code:
$ p1="Queen"
$ p2="News of the World"
$ awk -v p1="$p1" -v p2="$p2" '{ if ( $0 ~ p1 && $0 ~ p2 ) { if ( c == 1 ) print; c = 1 } else print }' file
Beatles Revolver
Queen News of the World
 
Old 04-09-2011, 11:13 AM   #7
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Original Poster
Rep: Reputation: 0
Thanks a lot.
One last thing. I saw that it is case sensitive.
Is there a way to get it case insensitive?
 
Old 04-09-2011, 11:18 AM   #8
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by MTK358 View Post
So delete the first line that has both strings?
Yes, exactly.
 
Old 04-09-2011, 01:12 PM   #9
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by wizard211990 View Post
One last thing. I saw that it is case sensitive.
Is there a way to get it case insensitive?
In GNU awk you can set the internal variable IGNORECASE to a non-zero value (that is true). Example:
Code:
awk -v p1="$p1" -v p2="$p2" 'BEGIN{ IGNORECASE = 1 }{ if ( $0 ~ p1 && $0 ~ p2 ) { if ( c == 1 ) print; c = 1 } else print }' file
More details in the GNU awk manual: http://www.gnu.org/software/gawk/man...02dsensitivity
 
Old 04-09-2011, 05:44 PM   #10
crts
Senior Member
 
Registered: Jan 2010
Posts: 2,020

Rep: Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757Reputation: 757
Hi,

if I understood correctly then there can be also an arbitrary amount of characters between the patterns and the order of the patterns does not matter, right? So these first lines would also be deleted if we use the parameters from your previous post:
Code:
Queen has News of the World  <-- delete if first occurence
News of the World Queen      <-- delete if first occurence
Here is how you can do it with sed:
Code:
p1=queen
p2='news of the world'
sed -r "/${p1}.*${p2}|${p2}.*${p1}/I {x;/^$/d}" file
 
Old 04-10-2011, 02:51 AM   #11
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Original Poster
Rep: Reputation: 0
Yes, your script adds the ability to vary the order, but deletes all entries and not just one.
 
Old 04-10-2011, 03:04 AM   #12
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
I think adding ;q inside the block will stop processing the file after changing the first occurrence.
{x;/^$/d;q}
 
Old 04-10-2011, 03:11 AM   #13
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Actually it exchanges the occurrences (each of them is replaced by the previous one). Example:
Code:
$ cat file
Something Here
Queen News of the World 1
Beatles Revolver
Queen News of the World 2
Something There
Queen News of the World 3
Goofy
Queen News of the World 4
Queen News of the World 5
Stop Now
$ sed -r "/${p1}.*${p2}|${p2}.*${p1}/I {x;/^$/d}" file
Something Here
Beatles Revolver
Queen News of the World 1
Something There
Queen News of the World 2
Goofy
Queen News of the World 3
Queen News of the World 4
Stop Now
 
Old 04-10-2011, 03:15 AM   #14
colucix
LQ Guru
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983Reputation: 1983
Quote:
Originally Posted by jschiwal View Post
I think adding ;q inside the block will stop processing the file after changing the first occurrence.
{x;/^$/d;q}
Doesn't this stop processing the rest of the file?
Code:
$ cat file
Something Here
Queen News of the World 1
Beatles Revolver
Queen News of the World 2
Something There
Queen News of the World 3
Goofy
Queen News of the World 4
Queen News of the World 5
Stop Now
$ sed -r "/${p1}.*${p2}|${p2}.*${p1}/I {x;/^$/d;q}" file
Something Here
Beatles Revolver
Queen News of the World 1
 
Old 04-10-2011, 03:37 AM   #15
wizard211990
LQ Newbie
 
Registered: Apr 2011
Posts: 9

Original Poster
Rep: Reputation: 0
yes, it works. Ubuntu no problem, but on the Mac of my friend has a problem. But it's ok
 
  


Reply



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



Similar Threads
Thread Thread Starter Forum Replies Last Post
need to delete a line if a field of that line matches using awf in bash scripting accesskarthi Linux - Newbie 8 06-29-2009 03:15 AM
Perl question: delete line from text file with duplicate match at beginning of line mrealty Programming 7 04-01-2009 06:46 PM
How do I actually delete a line using bash? gerben12 Linux - General 2 12-03-2008 01:54 AM
delete a line containing a pattern and the next line of a text file powah Programming 3 01-31-2007 05:34 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:05 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration