13

I am tired of this sed :D So I have a small file:

version = "1.1" group= "com.centurion.eye" archivesBaseName = "eye" impmet { version = "4.1614" runDir = "eclipse" } 

And here is my sed command:

sed -n -e '/version/ s/.* = *//p' "build.gradle" 

And I need to get ONLY version 1.1. So when I execute this command, the output is:

"1.3" "4.1614" 

But desired one is:

"1.3" 

How can I achieve that? Thanks!

1
  • did you get a chance to check my solution? Commented Jul 23, 2017 at 19:27

3 Answers 3

19

Quit after matching the first one.

sed -n -e '/version/ {s/.* = *//p;q}' build.gradle 
Sign up to request clarification or add additional context in comments.

Comments

10

pipe your commands output to head -1 to get only the first entry.

sed -n -e '/version/ s/.* = *//p' "build.gradle" | head -1 

Sample Run

[ /c]$ cat build.gradle version = "1.1" group= "com.centurion.eye" archivesBaseName = "eye" impmet { version = "4.1614" runDir = "eclipse" } [ /c]$ sed -n -e '/version/ s/.* = *//p' build.gradle | head -1 "1.1" 

2 Comments

Thanks, I will try it!
While this is definitely the simplest approach I will say it doesn't work for my use-case at all. I have a MASSIVE file that I'm running sed against and I really need to stop processing the millisecond I get a match. This command will still wait for sed to complete before outputting the top result.
4

With GNU sed, you can also use 0 in the address range to apply substitution only on the first match:

sed -n -e '0,/version/s/.* = *//p' build.gradle 

1 Comment

This is bad, because this will find and print all lines before the first match, if the first match is not at the first line

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.