Process a list of filenames to split directory and basename
Clash Royale CLAN TAG#URR8PPP
up vote
0
down vote
favorite
I have a file containing a bunch of filenames:
$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt
For each filename, I want to print the directory name and the base filename, comma-separated. For example:
/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt
How can I do that?
centos text-processing filenames
add a comment |Â
up vote
0
down vote
favorite
I have a file containing a bunch of filenames:
$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt
For each filename, I want to print the directory name and the base filename, comma-separated. For example:
/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt
How can I do that?
centos text-processing filenames
2
You might want to check out thebasename
anddirname
commands
â hhoke1
Aug 31 at 14:50
add a comment |Â
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file containing a bunch of filenames:
$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt
For each filename, I want to print the directory name and the base filename, comma-separated. For example:
/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt
How can I do that?
centos text-processing filenames
I have a file containing a bunch of filenames:
$ cat test_as1
/var/incoming/foo.txt
/var/incoming/bar.txt
/var/incoming/baz.txt
For each filename, I want to print the directory name and the base filename, comma-separated. For example:
/var/incoming,foo.txt
/var/incoming,bar.txt
/var/incoming,baz.txt
How can I do that?
centos text-processing filenames
centos text-processing filenames
edited Aug 31 at 16:54
Michael Mrozekâ¦
58.7k27184207
58.7k27184207
asked Aug 31 at 14:41
user210912
12
12
2
You might want to check out thebasename
anddirname
commands
â hhoke1
Aug 31 at 14:50
add a comment |Â
2
You might want to check out thebasename
anddirname
commands
â hhoke1
Aug 31 at 14:50
2
2
You might want to check out the
basename
and dirname
commandsâ hhoke1
Aug 31 at 14:50
You might want to check out the
basename
and dirname
commandsâ hhoke1
Aug 31 at 14:50
add a comment |Â
3 Answers
3
active
oldest
votes
up vote
1
down vote
Like @hhoke1 mentioned in a comment above, you can use the basename
and dirname
commands:
$ basename /var/incoming/new_items.txt
new_items.txt
$ dirname /var/incoming/new_items.txt
/var/incoming
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
2
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
1
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
add a comment |Â
up vote
1
down vote
Do you mean something like this ?
sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt
add a comment |Â
up vote
1
down vote
You could loop over the file and then print the parts before and after the last slash with parameter expansion:
while IFS= read -r line; do
printf '%s,%sn' "$line%/*" "$line##*/"
done < test_as1
"$line%/*"
removes the shortest match of /*
from the end of the line, and "$line##*/"
removes the longest match of */
from the beginning of the line.
add a comment |Â
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
Like @hhoke1 mentioned in a comment above, you can use the basename
and dirname
commands:
$ basename /var/incoming/new_items.txt
new_items.txt
$ dirname /var/incoming/new_items.txt
/var/incoming
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
2
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
1
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
add a comment |Â
up vote
1
down vote
Like @hhoke1 mentioned in a comment above, you can use the basename
and dirname
commands:
$ basename /var/incoming/new_items.txt
new_items.txt
$ dirname /var/incoming/new_items.txt
/var/incoming
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
2
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
1
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Like @hhoke1 mentioned in a comment above, you can use the basename
and dirname
commands:
$ basename /var/incoming/new_items.txt
new_items.txt
$ dirname /var/incoming/new_items.txt
/var/incoming
Like @hhoke1 mentioned in a comment above, you can use the basename
and dirname
commands:
$ basename /var/incoming/new_items.txt
new_items.txt
$ dirname /var/incoming/new_items.txt
/var/incoming
answered Aug 31 at 14:54
Andy Dalton
4,8391520
4,8391520
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
2
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
1
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
add a comment |Â
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
2
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
1
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
Thanks Andy basename /var/incoming/new_items.txt and dirname /var/incoming/new_items.txt seem to do the trick. Do you know how I can do it for about 150 similar items
â user210912
Aug 31 at 15:00
2
2
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
@user210912 Can you tell us what you're trying to do?
â Andy Dalton
Aug 31 at 15:05
1
1
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
Edited the question with what I believe user210912 is looking for based on a comment thread
â Michael Mrozekâ¦
Aug 31 at 16:56
add a comment |Â
up vote
1
down vote
Do you mean something like this ?
sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt
add a comment |Â
up vote
1
down vote
Do you mean something like this ?
sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt
add a comment |Â
up vote
1
down vote
up vote
1
down vote
Do you mean something like this ?
sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt
Do you mean something like this ?
sed -e 's/(.*)//1,/' -i /var/incoming/new_item.txt
edited Aug 31 at 17:14
ñÃÂsýù÷
16k92563
16k92563
answered Aug 31 at 14:48
Alexander
80413
80413
add a comment |Â
add a comment |Â
up vote
1
down vote
You could loop over the file and then print the parts before and after the last slash with parameter expansion:
while IFS= read -r line; do
printf '%s,%sn' "$line%/*" "$line##*/"
done < test_as1
"$line%/*"
removes the shortest match of /*
from the end of the line, and "$line##*/"
removes the longest match of */
from the beginning of the line.
add a comment |Â
up vote
1
down vote
You could loop over the file and then print the parts before and after the last slash with parameter expansion:
while IFS= read -r line; do
printf '%s,%sn' "$line%/*" "$line##*/"
done < test_as1
"$line%/*"
removes the shortest match of /*
from the end of the line, and "$line##*/"
removes the longest match of */
from the beginning of the line.
add a comment |Â
up vote
1
down vote
up vote
1
down vote
You could loop over the file and then print the parts before and after the last slash with parameter expansion:
while IFS= read -r line; do
printf '%s,%sn' "$line%/*" "$line##*/"
done < test_as1
"$line%/*"
removes the shortest match of /*
from the end of the line, and "$line##*/"
removes the longest match of */
from the beginning of the line.
You could loop over the file and then print the parts before and after the last slash with parameter expansion:
while IFS= read -r line; do
printf '%s,%sn' "$line%/*" "$line##*/"
done < test_as1
"$line%/*"
removes the shortest match of /*
from the end of the line, and "$line##*/"
removes the longest match of */
from the beginning of the line.
answered Aug 31 at 18:17
Benjamin W.
413211
413211
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%2f466057%2fprocess-a-list-of-filenames-to-split-directory-and-basename%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
2
You might want to check out the
basename
anddirname
commandsâ hhoke1
Aug 31 at 14:50