LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Bash How to delete only one line from a file (https://www.linuxquestions.org/questions/programming-9/bash-how-to-delete-only-one-line-from-a-file-873984/)

wizard211990 04-09-2011 09:45 AM

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?

MTK358 04-09-2011 09:47 AM

Quote:

Originally Posted by wizard211990 (Post 4319166)
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?

wizard211990 04-09-2011 09:53 AM

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

wizard211990 04-09-2011 10:18 AM

I have try with awk, sed grep but nothing...Help Me...

MTK358 04-09-2011 10:20 AM

So delete the first line that has both strings?

colucix 04-09-2011 10:22 AM

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


wizard211990 04-09-2011 11:13 AM

Thanks a lot.
One last thing. I saw that it is case sensitive.
Is there a way to get it case insensitive?

wizard211990 04-09-2011 11:18 AM

Quote:

Originally Posted by MTK358 (Post 4319194)
So delete the first line that has both strings?

Yes, exactly.

colucix 04-09-2011 01:12 PM

Quote:

Originally Posted by wizard211990 (Post 4319232)
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

crts 04-09-2011 05:44 PM

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


wizard211990 04-10-2011 02:51 AM

Yes, your script adds the ability to vary the order, but deletes all entries and not just one.

jschiwal 04-10-2011 03:04 AM

I think adding ;q inside the block will stop processing the file after changing the first occurrence.
{x;/^$/d;q}

colucix 04-10-2011 03:11 AM

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


colucix 04-10-2011 03:15 AM

Quote:

Originally Posted by jschiwal (Post 4319743)
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


wizard211990 04-10-2011 03:37 AM

yes, it works. Ubuntu no problem, but on the Mac of my friend has a problem. But it's ok


All times are GMT -5. The time now is 04:23 AM.