user3353499
user3353499

Reputation: 137

bash sed a quoted string from file but only at the Nth line

i know those two questions have been covered many time but i can't figure how to mix the two command in one:

get string between quote

sed 's/[^"]*"\([^"]*\)".*/\1/' "$file"

get line 2 from file

sed '2q;d' "$file"

many thanks for help.

EDIT:

input files are as follow:

#!/bin/bash
# "/path/to/folder/with/file.ext"
some others lines with quoted string

output

/path/to/folder/with/file.ext

Upvotes: 4

Views: 82

Answers (3)

anubhava
anubhava

Reputation: 784958

You can combine 2 sed command using this sed:

sed '2s/[^"]*"\([^"]*\)".*/\1/p;d;q' file
/path/to/folder/with/file.ext

Upvotes: 4

devnull
devnull

Reputation: 123458

You could combine the two commands by saying:

sed -n '2s/[^"]*"\([^"]*\)".*/\1/p' filename
  • -n would suppress automatic printing of pattern space
  • 2 before s causes the substitution to be performed on line 2
  • p prints the current pattern space

For your input, it'd produce:

/path/to/folder/with/file.ext

Upvotes: 3

kojiro
kojiro

Reputation: 77059

Awk would be my preferred solution here.

awk -F'"' 'NR==2{print $2}'

Upvotes: 4

Related Questions