Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get first match with sed?

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!

like image 531
Andrew Gorpenko Avatar asked Jul 22 '17 20:07

Andrew Gorpenko


People also ask

How do you insert a first line using sed?

Use sed 's insert ( i ) option which will insert the text in the preceding line.

What does sed '/ $/ D do?

It means that sed will read the next line and start processing it. nothing will be printed other than the blank lines, three times each. Save this answer.

How do you use sed substitution?

Find and replace text within a file using sed command Use Stream EDitor (sed) as follows: sed -i 's/old-text/new-text/g' input.txt. The s is the substitute command of sed for find and replace. It tells sed to find all occurrences of 'old-text' and replace with 'new-text' in a file named input.txt.


1 Answers

Quit after matching the first one.

sed -n -e '/version/ {s/.* = *//p;q}' build.gradle
like image 174
Ignacio Vazquez-Abrams Avatar answered Oct 08 '22 22:10

Ignacio Vazquez-Abrams