Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

Hello I have a file that has a lot of lines that end with main.css">, and some of them are repeated.

I want to keep only one copy of these lines and delete the repeated line.

Could I use grep to search if a file has a line that ends with main.css"> ?

So far I have :

 grep ' main.css' file.txt |sort | uniq -u

This does nothing as I expected, it just gives me a blank output. Please help? This did not

share|improve this question

4 Answers 4

up vote 4 down vote accepted

You should use:

grep -n 'main\.css">$' file.txt

$ denotes the end of the line, obviously, nothing can follow after this.

You do not need the * here, as you are searching for the pattern within the line, everything in front is automatically neglected if your pattern is found. The only thing you need to escape here is . which is the only regular expression operator in main.css">. Also -r is redundant when you supply only one file.

share|improve this answer
    
Guys please check the edit. :/ –  Unknown Jun 13 at 13:55

You could use awk also,

$ cat file
foo main.css">
ghj
brar main.css">

$ awk '/main\.css">$/{print NR}' file
1
3

Or sed:

$ sed '/main\.css">$/=' file
1
3
share|improve this answer

I think you wanted as well to keep also other lines which are not having main.css> even if those were duplicates:

Line 1
Line 2
Line 1
Line 2 main.css">
Line 2 main.css">
Line 3 
Line 3 main.css">
Line 4

So that after the command you would get:

Line 1
Line 1
Line 2
Line 2 main.csv>
Line 3 
Line 3 main.csv>
Line 4

In this case below command should do the trick: sort your_file | awk '!/main.css">$/;/main.css">$/,/main.css">$/{if($0!=p){print;p=$0}}'

share|improve this answer
sed  '/main.css/d' file.txt  > ~/Desktop/solution.txt

That worked perfectly fine :)

share|improve this answer
    
This will delete the lines rather that print the line numbers (and not just the ones that end this way as per the question). If this is what you wanted to accomplish maybe you could edit your question to actually ask that. –  Caleb Jun 13 at 14:15
    
Actually now that I checked, it did not work as I expected, it deleted the line which had main.css but I want it to delete main.css"> –  Unknown Jun 13 at 14:17
    
Please update your question with a more explicit description of what you have and what you want to have happen to it and I'm sure we can make it happen. –  Caleb Jun 13 at 14:20
    
Hey @Caleb I have updated the question. :/ –  Unknown Jun 13 at 14:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.