How to get first match with sed?
12,676
Solution 1
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"
Solution 2
Quit after matching the first one.
sed -n -e '/version/ {s/.* = *//p;q}' build.gradle
Solution 3
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
Author by
Andrew Gorpenko
Updated on June 06, 2022Comments
-
Andrew Gorpenko almost 2 years
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!