How do I grep file paths out of a text file?
Clash Royale CLAN TAG#URR8PPP
up vote
1
down vote
favorite
I have a file that has a list of paths like so:
"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"
Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?
text-processing sed grep
add a comment |Â
up vote
1
down vote
favorite
I have a file that has a list of paths like so:
"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"
Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?
text-processing sed grep
you can usesed -r 's/.*"(.*)"$/1/'
as answered to your newly duplicated question.
â Ã±ÃÂsýù÷
Sep 7 at 4:10
add a comment |Â
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I have a file that has a list of paths like so:
"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"
Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?
text-processing sed grep
I have a file that has a list of paths like so:
"1" "/user/bin/share"
"2" "/home/user/.local"
"3" "/root/"
Is there a way to extract just the paths? I dont want the numbers or quotation marks. How can I sed or grep the paths out of the file? What regex would be required for such a task?
text-processing sed grep
text-processing sed grep
edited Sep 7 at 4:12
ñÃÂsýù÷
15.8k92563
15.8k92563
asked Sep 7 at 0:10
James Heald
126115
126115
you can usesed -r 's/.*"(.*)"$/1/'
as answered to your newly duplicated question.
â Ã±ÃÂsýù÷
Sep 7 at 4:10
add a comment |Â
you can usesed -r 's/.*"(.*)"$/1/'
as answered to your newly duplicated question.
â Ã±ÃÂsýù÷
Sep 7 at 4:10
you can use
sed -r 's/.*"(.*)"$/1/'
as answered to your newly duplicated question.â Ã±ÃÂsýù÷
Sep 7 at 4:10
you can use
sed -r 's/.*"(.*)"$/1/'
as answered to your newly duplicated question.â Ã±ÃÂsýù÷
Sep 7 at 4:10
add a comment |Â
4 Answers
4
active
oldest
votes
up vote
6
down vote
accepted
If all the paths start with /
, you could just match /
followed by a sequence of non-"
characters:
$ grep -o '/[^"]*' file
/user/bin/share
/home/user/.local
/root/
Alternatively, for a more structured approach, use awk
to strip quotes from and print just the second field:
awk 'gsub(/"/,"",$2); print $2' file
awk
solution would not work if there was a whitespaces inpath
â Ã±ÃÂsýù÷
Sep 7 at 4:08
add a comment |Â
up vote
1
down vote
Assuming that the paths do not contain newline characters,
$ sed 's/^.*[[:blank:]]"//; s/"$//' <file
/user/bin/share
/home/user/.local
/root/
The sed
code first removes everything on each line up to and including the first "
character preceded by a blank (space or tab). It then removes the "
at the end.
This allows the paths to contain spaces and embedded "
characters, but not the combination blank+"
.
add a comment |Â
up vote
1
down vote
Why wouldn't a simple
awk -F" 'print $4' file
/user/bin/share
/home/user/.local
/root/
work?
add a comment |Â
up vote
1
down vote
Simple approach. Pluck out field #4, with quotes as delimiters, using cut
.
$ cut -f4 -d" file
/user/bin/share
/home/user/.local
/root/
$
add a comment |Â
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
6
down vote
accepted
If all the paths start with /
, you could just match /
followed by a sequence of non-"
characters:
$ grep -o '/[^"]*' file
/user/bin/share
/home/user/.local
/root/
Alternatively, for a more structured approach, use awk
to strip quotes from and print just the second field:
awk 'gsub(/"/,"",$2); print $2' file
awk
solution would not work if there was a whitespaces inpath
â Ã±ÃÂsýù÷
Sep 7 at 4:08
add a comment |Â
up vote
6
down vote
accepted
If all the paths start with /
, you could just match /
followed by a sequence of non-"
characters:
$ grep -o '/[^"]*' file
/user/bin/share
/home/user/.local
/root/
Alternatively, for a more structured approach, use awk
to strip quotes from and print just the second field:
awk 'gsub(/"/,"",$2); print $2' file
awk
solution would not work if there was a whitespaces inpath
â Ã±ÃÂsýù÷
Sep 7 at 4:08
add a comment |Â
up vote
6
down vote
accepted
up vote
6
down vote
accepted
If all the paths start with /
, you could just match /
followed by a sequence of non-"
characters:
$ grep -o '/[^"]*' file
/user/bin/share
/home/user/.local
/root/
Alternatively, for a more structured approach, use awk
to strip quotes from and print just the second field:
awk 'gsub(/"/,"",$2); print $2' file
If all the paths start with /
, you could just match /
followed by a sequence of non-"
characters:
$ grep -o '/[^"]*' file
/user/bin/share
/home/user/.local
/root/
Alternatively, for a more structured approach, use awk
to strip quotes from and print just the second field:
awk 'gsub(/"/,"",$2); print $2' file
answered Sep 7 at 0:21
steeldriver
32.2k34979
32.2k34979
awk
solution would not work if there was a whitespaces inpath
â Ã±ÃÂsýù÷
Sep 7 at 4:08
add a comment |Â
awk
solution would not work if there was a whitespaces inpath
â Ã±ÃÂsýù÷
Sep 7 at 4:08
awk
solution would not work if there was a whitespaces in path
â Ã±ÃÂsýù÷
Sep 7 at 4:08
awk
solution would not work if there was a whitespaces in path
â Ã±ÃÂsýù÷
Sep 7 at 4:08
add a comment |Â
up vote
1
down vote
Assuming that the paths do not contain newline characters,
$ sed 's/^.*[[:blank:]]"//; s/"$//' <file
/user/bin/share
/home/user/.local
/root/
The sed
code first removes everything on each line up to and including the first "
character preceded by a blank (space or tab). It then removes the "
at the end.
This allows the paths to contain spaces and embedded "
characters, but not the combination blank+"
.
add a comment |Â
up vote
1
down vote
Assuming that the paths do not contain newline characters,
$ sed 's/^.*[[:blank:]]"//; s/"$//' <file
/user/bin/share
/home/user/.local
/root/
The sed
code first removes everything on each line up to and including the first "
character preceded by a blank (space or tab). It then removes the "
at the end.
This allows the paths to contain spaces and embedded "
characters, but not the combination blank+"
.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Assuming that the paths do not contain newline characters,
$ sed 's/^.*[[:blank:]]"//; s/"$//' <file
/user/bin/share
/home/user/.local
/root/
The sed
code first removes everything on each line up to and including the first "
character preceded by a blank (space or tab). It then removes the "
at the end.
This allows the paths to contain spaces and embedded "
characters, but not the combination blank+"
.
Assuming that the paths do not contain newline characters,
$ sed 's/^.*[[:blank:]]"//; s/"$//' <file
/user/bin/share
/home/user/.local
/root/
The sed
code first removes everything on each line up to and including the first "
character preceded by a blank (space or tab). It then removes the "
at the end.
This allows the paths to contain spaces and embedded "
characters, but not the combination blank+"
.
answered Sep 7 at 5:53
Kusalananda
106k14209329
106k14209329
add a comment |Â
add a comment |Â
up vote
1
down vote
Why wouldn't a simple
awk -F" 'print $4' file
/user/bin/share
/home/user/.local
/root/
work?
add a comment |Â
up vote
1
down vote
Why wouldn't a simple
awk -F" 'print $4' file
/user/bin/share
/home/user/.local
/root/
work?
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Why wouldn't a simple
awk -F" 'print $4' file
/user/bin/share
/home/user/.local
/root/
work?
Why wouldn't a simple
awk -F" 'print $4' file
/user/bin/share
/home/user/.local
/root/
work?
answered Sep 7 at 8:27
RudiC
1,2168
1,2168
add a comment |Â
add a comment |Â
up vote
1
down vote
Simple approach. Pluck out field #4, with quotes as delimiters, using cut
.
$ cut -f4 -d" file
/user/bin/share
/home/user/.local
/root/
$
add a comment |Â
up vote
1
down vote
Simple approach. Pluck out field #4, with quotes as delimiters, using cut
.
$ cut -f4 -d" file
/user/bin/share
/home/user/.local
/root/
$
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Simple approach. Pluck out field #4, with quotes as delimiters, using cut
.
$ cut -f4 -d" file
/user/bin/share
/home/user/.local
/root/
$
Simple approach. Pluck out field #4, with quotes as delimiters, using cut
.
$ cut -f4 -d" file
/user/bin/share
/home/user/.local
/root/
$
answered Sep 7 at 8:45
steve
12.9k22149
12.9k22149
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f467412%2fhow-do-i-grep-file-paths-out-of-a-text-file%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
you can use
sed -r 's/.*"(.*)"$/1/'
as answered to your newly duplicated question.â Ã±ÃÂsýù÷
Sep 7 at 4:10