How to read first four characters of file name?
I have several files whose extensions are .gz with similar filenames to:
ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
I need to read the first four characters of these files' names and store these characters in a file. How can I do this?
scripts
add a comment |
I have several files whose extensions are .gz with similar filenames to:
ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
I need to read the first four characters of these files' names and store these characters in a file. How can I do this?
scripts
The first 4 bytes of the compressed file or of the uncompressed file?
– PerlDuck
Jan 7 at 12:04
compressed file
– deepblue_86
Jan 7 at 12:05
add a comment |
I have several files whose extensions are .gz with similar filenames to:
ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
I need to read the first four characters of these files' names and store these characters in a file. How can I do this?
scripts
I have several files whose extensions are .gz with similar filenames to:
ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
I need to read the first four characters of these files' names and store these characters in a file. How can I do this?
scripts
scripts
edited Jan 7 at 12:31
Arronical
13.2k84791
13.2k84791
asked Jan 7 at 12:02
deepblue_86deepblue_86
5851023
5851023
The first 4 bytes of the compressed file or of the uncompressed file?
– PerlDuck
Jan 7 at 12:04
compressed file
– deepblue_86
Jan 7 at 12:05
add a comment |
The first 4 bytes of the compressed file or of the uncompressed file?
– PerlDuck
Jan 7 at 12:04
compressed file
– deepblue_86
Jan 7 at 12:05
The first 4 bytes of the compressed file or of the uncompressed file?
– PerlDuck
Jan 7 at 12:04
The first 4 bytes of the compressed file or of the uncompressed file?
– PerlDuck
Jan 7 at 12:04
compressed file
– deepblue_86
Jan 7 at 12:05
compressed file
– deepblue_86
Jan 7 at 12:05
add a comment |
2 Answers
2
active
oldest
votes
Here the code to take first four character of filenames;
filename=ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
tmp=${filename:0:4}
echo $tmp
Result:
echo "$tmp"
ANKR
Source
For a list
for file in ./*.gz
do
echo "${file:2:4}" | tee -a list.txt
done
previous for cycle will print on the screen the result and also append to list.txt
Source from Documentation on The Linux Documentation Project (TLDP)
Update
Improvement of code based on @steeldriver suggestion in comment.
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
1
Don't use$(ls *.gz)
- see Bash Pitfalls #1
– steeldriver
Jan 7 at 13:19
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
add a comment |
To fetch the first N bytes of a string, cut
can be used:
echo ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz | cut --bytes=1-4 > output
This will extract the first 4 bytes from the given string and store it in the file output
.
To apply the above code to multiple filenames, use
ls *.gz | cut --bytes=1-4 > output
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
1
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
add a comment |
Your Answer
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "89"
};
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%2faskubuntu.com%2fquestions%2f1107705%2fhow-to-read-first-four-characters-of-file-name%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Here the code to take first four character of filenames;
filename=ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
tmp=${filename:0:4}
echo $tmp
Result:
echo "$tmp"
ANKR
Source
For a list
for file in ./*.gz
do
echo "${file:2:4}" | tee -a list.txt
done
previous for cycle will print on the screen the result and also append to list.txt
Source from Documentation on The Linux Documentation Project (TLDP)
Update
Improvement of code based on @steeldriver suggestion in comment.
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
1
Don't use$(ls *.gz)
- see Bash Pitfalls #1
– steeldriver
Jan 7 at 13:19
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
add a comment |
Here the code to take first four character of filenames;
filename=ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
tmp=${filename:0:4}
echo $tmp
Result:
echo "$tmp"
ANKR
Source
For a list
for file in ./*.gz
do
echo "${file:2:4}" | tee -a list.txt
done
previous for cycle will print on the screen the result and also append to list.txt
Source from Documentation on The Linux Documentation Project (TLDP)
Update
Improvement of code based on @steeldriver suggestion in comment.
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
1
Don't use$(ls *.gz)
- see Bash Pitfalls #1
– steeldriver
Jan 7 at 13:19
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
add a comment |
Here the code to take first four character of filenames;
filename=ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
tmp=${filename:0:4}
echo $tmp
Result:
echo "$tmp"
ANKR
Source
For a list
for file in ./*.gz
do
echo "${file:2:4}" | tee -a list.txt
done
previous for cycle will print on the screen the result and also append to list.txt
Source from Documentation on The Linux Documentation Project (TLDP)
Update
Improvement of code based on @steeldriver suggestion in comment.
Here the code to take first four character of filenames;
filename=ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz
tmp=${filename:0:4}
echo $tmp
Result:
echo "$tmp"
ANKR
Source
For a list
for file in ./*.gz
do
echo "${file:2:4}" | tee -a list.txt
done
previous for cycle will print on the screen the result and also append to list.txt
Source from Documentation on The Linux Documentation Project (TLDP)
Update
Improvement of code based on @steeldriver suggestion in comment.
edited Jan 7 at 13:40
answered Jan 7 at 12:17
AtomiX84AtomiX84
68019
68019
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
1
Don't use$(ls *.gz)
- see Bash Pitfalls #1
– steeldriver
Jan 7 at 13:19
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
add a comment |
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
1
Don't use$(ls *.gz)
- see Bash Pitfalls #1
– steeldriver
Jan 7 at 13:19
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
How can I define 'filename' for all the files (apprx 300) and apply the above code?
– deepblue_86
Jan 7 at 12:19
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
they are all in a directory?
– AtomiX84
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
yes they are all in a directory.
– deepblue_86
Jan 7 at 12:20
1
1
Don't use
$(ls *.gz)
- see Bash Pitfalls #1– steeldriver
Jan 7 at 13:19
Don't use
$(ls *.gz)
- see Bash Pitfalls #1– steeldriver
Jan 7 at 13:19
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
thanks @steeldriver, I'm always get enough lucky and never encountered problems describe in the link you provide. I have improved the answer based on your suggestion.
– AtomiX84
Jan 7 at 13:41
add a comment |
To fetch the first N bytes of a string, cut
can be used:
echo ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz | cut --bytes=1-4 > output
This will extract the first 4 bytes from the given string and store it in the file output
.
To apply the above code to multiple filenames, use
ls *.gz | cut --bytes=1-4 > output
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
1
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
add a comment |
To fetch the first N bytes of a string, cut
can be used:
echo ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz | cut --bytes=1-4 > output
This will extract the first 4 bytes from the given string and store it in the file output
.
To apply the above code to multiple filenames, use
ls *.gz | cut --bytes=1-4 > output
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
1
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
add a comment |
To fetch the first N bytes of a string, cut
can be used:
echo ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz | cut --bytes=1-4 > output
This will extract the first 4 bytes from the given string and store it in the file output
.
To apply the above code to multiple filenames, use
ls *.gz | cut --bytes=1-4 > output
To fetch the first N bytes of a string, cut
can be used:
echo ANKR00TUR_R_20183350000_01D_30S_MO.crx.gz | cut --bytes=1-4 > output
This will extract the first 4 bytes from the given string and store it in the file output
.
To apply the above code to multiple filenames, use
ls *.gz | cut --bytes=1-4 > output
edited Jan 7 at 12:37
answered Jan 7 at 12:09
PerlDuckPerlDuck
5,86211333
5,86211333
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
1
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
add a comment |
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
1
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
I applied the command but it produced compressed output file and it cannot be extracted.
– deepblue_86
Jan 7 at 12:13
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
Yes, that is exactly what you asked for in your question: The first four bytes of the compressed file. Or do you mean the first four bytes of the filename (i.e. ANKR) which is a different case?
– PerlDuck
Jan 7 at 12:14
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
yes, i meant the first four bytes of the filename (i.e. ANKR)
– deepblue_86
Jan 7 at 12:16
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
@deepblue_86 I updated my answer. Now it fetches the first 4 bytes of the filename instead of the file content.
– PerlDuck
Jan 7 at 12:20
1
1
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
@deepblue_86 Please don't ask follow-up questions in comments. Edit your post instead and clarify what you actually need.
– PerlDuck
Jan 7 at 12:26
add a comment |
Thanks for contributing an answer to Ask Ubuntu!
- 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%2faskubuntu.com%2fquestions%2f1107705%2fhow-to-read-first-four-characters-of-file-name%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
The first 4 bytes of the compressed file or of the uncompressed file?
– PerlDuck
Jan 7 at 12:04
compressed file
– deepblue_86
Jan 7 at 12:05