Using sed get substring between two double quotes
I have a file
xyz... rsync: "/home/path/to/file": Permission denied (13) rsync:
"/home/path/to/file1": Permission denied (13) rsync:
"/home/path/to/file2": Permission denied (13) rsync:
"/home/path/to/file3": Permission denied (13)
Now I want to extract the file paths only and store it to another file. Output file is like:
/home/path/to/file
/home/path/to/file1
/home/path/to/file2
/home/path/to/file3
Using sed or awk how can I do this?
I have tried sed -n '/"/,/"/p' myfile
but its not working.
Solution 1:
You can pipe stderr of your rsync command to a awk script:
awk -F '"' '{print $2}'
Or to a cut command like this:
cut -d'"' -f2
Solution 2:
Using sed
:
sed 's/^[^"]*"\([^"]*\)".*/\1/'
That looks for: beginning of line, a series of non-quotes, a double quote, captures a series of non-quotes, a double quote and anything else on the line, and replaces it by the captured material.
$ sed 's/^[^"]*"\([^"]*\)".*/\1/' <<'EOF'
> xyz... rsync: "/home/path/to/file": Permission denied (13) rsync:
> "/home/path/to/file1": Permission denied (13) rsync:
> "/home/path/to/file2": Permission denied (13) rsync:
> "/home/path/to/file3": Permission denied (13)
> EOF
/home/path/to/file
/home/path/to/file1
/home/path/to/file2
/home/path/to/file3
$
Test on RHEL 5 Linux with GNU sed
, but only using features that would have worked in 7th Edition UNIX™ version of sed
.
Incidentally, a slightly simpler way to do it is with two substitute commands; change everything up to and including the first double quote to an empty string (that's a sequence of zero or more non quotes followed by a double quote); change everything after what is now the first double quote to nothing:
sed 's/^[^"]*"//; s/".*//'
Incidentally, the command you tried (`sed -n '/"/,/"/p') prints from one line containing a double quote to the next line containing a double quote, without editing the lines at all. Which was why it didn't seem to work for you — it did what you asked, but what you asked it to do wasn't what you intended to ask it to do.
Efficiency-wise, there's unlikely to be a measurable difference in the performance. In terms of ease of maintenance, I suspect the latter is less taxing on the brain cells.