Bash sed quotes a line from a file, but only on the Nth line

I know that these two questions have been considered many times, but I can’t figure out how to mix two teams in one:

get a line between quote

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

get line 2 from file

sed '2q;d' "$file"

Thank you very much for your help.

EDIT:

input files:

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

Output

/path/to/folder/with/file.ext
+4
source share
3 answers

Awk will be my preferred solution here.

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

You can combine the 2 sed command with this sed:

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

, :

sed -n '2s/[^"]*"\([^"]*\)".*/\1/p' filename
  • -n
  • 2 s , 2
  • p

:

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

Source: https://habr.com/ru/post/1529577/


All Articles