Copy first six characters of filenames and amend it as the top line of the same .txt files
I have a dir/folder of .txt files and I'd like to grab the first six characters of each .txt file filename and paste it into the beginning of each, same .txt file (followed by a TAB).
Filename example:
- 123456 Something Awesome.txt
- 234567 Another Awesome Thing.txt
So "123456 Something Awesome.txt" would begin with "123456[TAB]" and so on.
I'm doing this as part of a batch (.bat) on a Mac.
(I'll be importing the .txt files into Excel so the first 6 chars/numbers of the filename would essentially become the first data cell for each file/line of data - which is also why I need the TAB, so that the rest of the file will fill the following cells.)
Thanks!
macos
add a comment |
I have a dir/folder of .txt files and I'd like to grab the first six characters of each .txt file filename and paste it into the beginning of each, same .txt file (followed by a TAB).
Filename example:
- 123456 Something Awesome.txt
- 234567 Another Awesome Thing.txt
So "123456 Something Awesome.txt" would begin with "123456[TAB]" and so on.
I'm doing this as part of a batch (.bat) on a Mac.
(I'll be importing the .txt files into Excel so the first 6 chars/numbers of the filename would essentially become the first data cell for each file/line of data - which is also why I need the TAB, so that the rest of the file will fill the following cells.)
Thanks!
macos
2
Mac doesn't use.bat
files, that's a Windows thing. Also please show what you've attempted.
– l'L'l
Nov 15 '18 at 21:33
You took the tour, did you read How to Ask, and Minimal, Complete, and Verifiable example?
– jwdonahue
Nov 15 '18 at 22:03
.bat does work on Mac. It's run through Mac's Terminal.app. What I have used for this specific process has failed, so iI thought it worthless for me to post sample code.
– pjeigh
Nov 16 '18 at 13:43
Mac OSX and higher will run .bat files - in.answers.yahoo.com/question/… (you can end it in ".bat" - I've been running one for weeks).
– pjeigh
Nov 21 '18 at 16:41
add a comment |
I have a dir/folder of .txt files and I'd like to grab the first six characters of each .txt file filename and paste it into the beginning of each, same .txt file (followed by a TAB).
Filename example:
- 123456 Something Awesome.txt
- 234567 Another Awesome Thing.txt
So "123456 Something Awesome.txt" would begin with "123456[TAB]" and so on.
I'm doing this as part of a batch (.bat) on a Mac.
(I'll be importing the .txt files into Excel so the first 6 chars/numbers of the filename would essentially become the first data cell for each file/line of data - which is also why I need the TAB, so that the rest of the file will fill the following cells.)
Thanks!
macos
I have a dir/folder of .txt files and I'd like to grab the first six characters of each .txt file filename and paste it into the beginning of each, same .txt file (followed by a TAB).
Filename example:
- 123456 Something Awesome.txt
- 234567 Another Awesome Thing.txt
So "123456 Something Awesome.txt" would begin with "123456[TAB]" and so on.
I'm doing this as part of a batch (.bat) on a Mac.
(I'll be importing the .txt files into Excel so the first 6 chars/numbers of the filename would essentially become the first data cell for each file/line of data - which is also why I need the TAB, so that the rest of the file will fill the following cells.)
Thanks!
macos
macos
edited Nov 15 '18 at 21:53
LotPings
19.8k61633
19.8k61633
asked Nov 15 '18 at 21:32
pjeighpjeigh
11
11
2
Mac doesn't use.bat
files, that's a Windows thing. Also please show what you've attempted.
– l'L'l
Nov 15 '18 at 21:33
You took the tour, did you read How to Ask, and Minimal, Complete, and Verifiable example?
– jwdonahue
Nov 15 '18 at 22:03
.bat does work on Mac. It's run through Mac's Terminal.app. What I have used for this specific process has failed, so iI thought it worthless for me to post sample code.
– pjeigh
Nov 16 '18 at 13:43
Mac OSX and higher will run .bat files - in.answers.yahoo.com/question/… (you can end it in ".bat" - I've been running one for weeks).
– pjeigh
Nov 21 '18 at 16:41
add a comment |
2
Mac doesn't use.bat
files, that's a Windows thing. Also please show what you've attempted.
– l'L'l
Nov 15 '18 at 21:33
You took the tour, did you read How to Ask, and Minimal, Complete, and Verifiable example?
– jwdonahue
Nov 15 '18 at 22:03
.bat does work on Mac. It's run through Mac's Terminal.app. What I have used for this specific process has failed, so iI thought it worthless for me to post sample code.
– pjeigh
Nov 16 '18 at 13:43
Mac OSX and higher will run .bat files - in.answers.yahoo.com/question/… (you can end it in ".bat" - I've been running one for weeks).
– pjeigh
Nov 21 '18 at 16:41
2
2
Mac doesn't use
.bat
files, that's a Windows thing. Also please show what you've attempted.– l'L'l
Nov 15 '18 at 21:33
Mac doesn't use
.bat
files, that's a Windows thing. Also please show what you've attempted.– l'L'l
Nov 15 '18 at 21:33
You took the tour, did you read How to Ask, and Minimal, Complete, and Verifiable example?
– jwdonahue
Nov 15 '18 at 22:03
You took the tour, did you read How to Ask, and Minimal, Complete, and Verifiable example?
– jwdonahue
Nov 15 '18 at 22:03
.bat does work on Mac. It's run through Mac's Terminal.app. What I have used for this specific process has failed, so iI thought it worthless for me to post sample code.
– pjeigh
Nov 16 '18 at 13:43
.bat does work on Mac. It's run through Mac's Terminal.app. What I have used for this specific process has failed, so iI thought it worthless for me to post sample code.
– pjeigh
Nov 16 '18 at 13:43
Mac OSX and higher will run .bat files - in.answers.yahoo.com/question/… (you can end it in ".bat" - I've been running one for weeks).
– pjeigh
Nov 21 '18 at 16:41
Mac OSX and higher will run .bat files - in.answers.yahoo.com/question/… (you can end it in ".bat" - I've been running one for weeks).
– pjeigh
Nov 21 '18 at 16:41
add a comment |
1 Answer
1
active
oldest
votes
Something like this could probably work. Put your *.txt files in a directory and create file myscript.sh
. Execute chmod +x myscript.sh
in the same directory so you are able to execute your shell script. Put the following code into you shell script and execute it with ./myscript.sh
in your directory.
#!/bin/bash
for filename in ./*.txt; do
text="${filename:2:6}"
echo $text | cat - $filename > temp && mv temp $filename
done
Thats a basic construction, just insert an additional tab
and you are good to go.
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328203%2fcopy-first-six-characters-of-filenames-and-amend-it-as-the-top-line-of-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Something like this could probably work. Put your *.txt files in a directory and create file myscript.sh
. Execute chmod +x myscript.sh
in the same directory so you are able to execute your shell script. Put the following code into you shell script and execute it with ./myscript.sh
in your directory.
#!/bin/bash
for filename in ./*.txt; do
text="${filename:2:6}"
echo $text | cat - $filename > temp && mv temp $filename
done
Thats a basic construction, just insert an additional tab
and you are good to go.
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
add a comment |
Something like this could probably work. Put your *.txt files in a directory and create file myscript.sh
. Execute chmod +x myscript.sh
in the same directory so you are able to execute your shell script. Put the following code into you shell script and execute it with ./myscript.sh
in your directory.
#!/bin/bash
for filename in ./*.txt; do
text="${filename:2:6}"
echo $text | cat - $filename > temp && mv temp $filename
done
Thats a basic construction, just insert an additional tab
and you are good to go.
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
add a comment |
Something like this could probably work. Put your *.txt files in a directory and create file myscript.sh
. Execute chmod +x myscript.sh
in the same directory so you are able to execute your shell script. Put the following code into you shell script and execute it with ./myscript.sh
in your directory.
#!/bin/bash
for filename in ./*.txt; do
text="${filename:2:6}"
echo $text | cat - $filename > temp && mv temp $filename
done
Thats a basic construction, just insert an additional tab
and you are good to go.
Something like this could probably work. Put your *.txt files in a directory and create file myscript.sh
. Execute chmod +x myscript.sh
in the same directory so you are able to execute your shell script. Put the following code into you shell script and execute it with ./myscript.sh
in your directory.
#!/bin/bash
for filename in ./*.txt; do
text="${filename:2:6}"
echo $text | cat - $filename > temp && mv temp $filename
done
Thats a basic construction, just insert an additional tab
and you are good to go.
answered Nov 15 '18 at 21:52
TabmanRekojTabmanRekoj
14119
14119
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
add a comment |
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
That yields a lot of "no such file or directory" errors, but that may be because the file names have paces(?). The results come out like: cat: 123456: No such file or directory cat: Something: No such file or directory cat: Awesome.txt: No such file or directory
– pjeigh
Nov 16 '18 at 13:54
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
...has SPACES :)
– pjeigh
Nov 16 '18 at 15:13
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
Any other suggestions?
– pjeigh
Nov 20 '18 at 14:55
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
How can I get it to disregard blank spaces in the filename(s)? Looks like that is the issue :) Also, it's adding a line return after the 6 chars (when I need a tab).
– pjeigh
Nov 20 '18 at 15:39
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
My "fix" for removing the spaces was to copy the files to a new dir, replace the spaces with underscores and then run the process above on those files. Then it worked :)
– pjeigh
Nov 20 '18 at 20:25
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
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
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328203%2fcopy-first-six-characters-of-filenames-and-amend-it-as-the-top-line-of-the-same%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
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
Required, but never shown
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
Required, but never shown
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
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
Mac doesn't use
.bat
files, that's a Windows thing. Also please show what you've attempted.– l'L'l
Nov 15 '18 at 21:33
You took the tour, did you read How to Ask, and Minimal, Complete, and Verifiable example?
– jwdonahue
Nov 15 '18 at 22:03
.bat does work on Mac. It's run through Mac's Terminal.app. What I have used for this specific process has failed, so iI thought it worthless for me to post sample code.
– pjeigh
Nov 16 '18 at 13:43
Mac OSX and higher will run .bat files - in.answers.yahoo.com/question/… (you can end it in ".bat" - I've been running one for weeks).
– pjeigh
Nov 21 '18 at 16:41