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? |
Quote:
|
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 |
I have try with awk, sed grep but nothing...Help Me...
|
So delete the first line that has both strings?
|
Here is an awk solution:
Code:
$ p1="Queen" |
Thanks a lot.
One last thing. I saw that it is case sensitive. Is there a way to get it case insensitive? |
Quote:
|
Quote:
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 |
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 Code:
p1=queen |
Yes, your script adds the ability to vary the order, but deletes all entries and not just one.
|
I think adding ;q inside the block will stop processing the file after changing the first occurrence.
{x;/^$/d;q} |
Actually it exchanges the occurrences (each of them is replaced by the previous one). Example:
Code:
$ cat file |
Quote:
Code:
$ cat file |
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. |