grep to see if a line ends with a particular string

5,623

Solution 1

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.

Solution 2

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}}'

Solution 3

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:
5,623

Related videos on Youtube

gkmohit
Author by

gkmohit

I am an Entrepreneur, Web Designer and Online Business Consultant. My mission is to help small businesses grow by leveraging the power of the internet. I believe in automating tasks by using tools so that you can focus on your core business. I have always been a curious person. The first time I used a computer was in grade 8 and fascinated by how you could create digital art using Corel Draw. In class 10, I had the opportunity to use the first mobile phone, and I was very intrigued by how the OS integrated with the hardware. That same curiosity led me to write my first piece of code in grade 10, and I then realized the power a programmer had in this world. In the mid-2011 family and I moved from Bangalore, India to Toronto, Canada, where I started my undergraduate degree in Computer Science at York University. As a student, I couldn't wait to get some industry experience, and I was fortunate to land my first job in IT at the University Information Technology department. I started as a Technical Analyst and slowly grew to be a software developer at the Student Information System. Gaining some industry experience gave me the confidence to go and attend a few hackathons across North America. I was fortunate to win a few awards from companies like Google, IBM, Bank of Nova Scotia and more while attending hackathons. With the help of my awards, experience and my skills, I started my internship at SAP Labs in Waterloo, Canada. My course was great, but I was seeking something more challenging, so my hackathon team members and I decided to start a fast-growing development shop Hyfer Technologies. At Hyfer Technologies, I stumbled upon Product Management and Business Analysis while managing a team of developers remotely. So far, I have been able to work with 10+ clients from conception to production. As a product manager, I have had a few failed projects but also some that are still growing strong. As of March 2020, I am working with The Ottawa Hospital as a Business Analyst. As a Product Manager & Business Analyst, my skills include but are not limited to: Management Strategy Growth Strategy Customer, partner and client relations, Organizational Design Process Improvements Statistical Analysis and Data Mining Marketing and Brand Strategy Running Product-Related Sessions Managing technical team Through these skills and experience, I am confident I can add a lot of values to any growing team. I am always open to learning more about you and your business. Feel free to reach out to me or follow me on LinkedIn.

Updated on September 18, 2022

Comments

  • gkmohit
    gkmohit over 1 year

    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

  • gkmohit
    gkmohit almost 10 years
    Guys please check the edit. :/
  • tcoolspy
    tcoolspy almost 10 years
    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.
  • gkmohit
    gkmohit almost 10 years
    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">
  • tcoolspy
    tcoolspy almost 10 years
    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.
  • gkmohit
    gkmohit almost 10 years
    Hey @Caleb I have updated the question. :/