Read line from file and process somethingWhat is the difference between touch file and > file?How to...
Is there really no use for MD5 anymore?
Pre-plastic human skin alternative
Is Diceware more secure than a long passphrase?
Could the terminal length of components like resistors be reduced?
How do I deal with a coworker that keeps asking to make small superficial changes to a report, and it is seriously triggering my anxiety?
Relationship between strut and baselineskip
How to have a sharp product image?
Does tea made with boiling water cool faster than tea made with boiled (but still hot) water?
What does ゆーか mean?
How do I check if a string is entirely made of the same substring?
Was there a shared-world project before "Thieves World"?
Does a large simulator bay have standard public address announcements?
Is there a way to generate a list of distinct numbers such that no two subsets ever have an equal sum?
Mistake in years of experience in resume?
Aligning equation numbers vertically
Why must Chinese maps be obfuscated?
Can an Area of Effect spell cast outside a Prismatic Wall extend inside it?
A Note on N!
Contradiction proof for inequality of P and NP?
Can SQL Server create collisions in system generated constraint names?
Phrase for the opposite of "foolproof"
a sore throat vs a strep throat vs strep throat
Why didn't the Space Shuttle bounce back into space as many times as possible so as to lose a lot of kinetic energy up there?
How to display Aura JS Errors Lightning Out
Read line from file and process something
What is the difference between touch file and > file?How to replace a string of text with input from another filePrint only one line at a time from text fileReading lines from a text file and creating a text file for each name on each lineHow to squash all the contents of a multi-line file onto one line?Why does `read -r` still only get the first word of each line?Remove the first part of each line of a text fileRead from text1 and write to text2How to read line by line multiple filesSelect the remaining text in a line from a log fileread input from a file and append that values to variables using shell
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ margin-bottom:0;
}
There are multiple lines in my input file but my output file Token.csv
only has one output line like this:
4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X
How to process every line?
My Code:
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"
command-line bash scripts
New contributor
add a comment |
There are multiple lines in my input file but my output file Token.csv
only has one output line like this:
4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X
How to process every line?
My Code:
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"
command-line bash scripts
New contributor
1
and your code is not working or what is the problem? Btw, you don't need the doubleddate
.
– RoVo
17 hours ago
add a comment |
There are multiple lines in my input file but my output file Token.csv
only has one output line like this:
4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X
How to process every line?
My Code:
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"
command-line bash scripts
New contributor
There are multiple lines in my input file but my output file Token.csv
only has one output line like this:
4TF16B7GA129E
4TF16B7GA129S
4TF16B7GA129D
4TF16B7GA129X
How to process every line?
My Code:
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum > Token.csv
done < "$StringCsv"
command-line bash scripts
command-line bash scripts
New contributor
New contributor
edited 16 hours ago
WinEunuuchs2Unix
48.7k1198187
48.7k1198187
New contributor
asked 17 hours ago
Shubham AgrawalShubham Agrawal
191
191
New contributor
New contributor
1
and your code is not working or what is the problem? Btw, you don't need the doubleddate
.
– RoVo
17 hours ago
add a comment |
1
and your code is not working or what is the problem? Btw, you don't need the doubleddate
.
– RoVo
17 hours ago
1
1
and your code is not working or what is the problem? Btw, you don't need the doubled
date
.– RoVo
17 hours ago
and your code is not working or what is the problem? Btw, you don't need the doubled
date
.– RoVo
17 hours ago
add a comment |
4 Answers
4
active
oldest
votes
Put the output redirection on the entire loop, not just the sha256sum
command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv
add a comment |
You are writing to a file in a loop with this command:
echo "$serial"|sha256sum > Token.csv
However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:
echo "$serial"|sha256sum >> Token.csv
A single >
tells bash to erase the file Token.csv
and write the contents. A double >>
tells bash to add to the end of the file.
The bash script would now look like this:
!/bin/bash
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
> Token.csv # Empty file from last run
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum >> Token.csv # Append new record to end
done < "$StringCsv"
There are two ways to empty the file > Token.csv
as used above and touch Token.csv
. See:
- What is the difference between touch file and > file?
There should probably be an explicit removal/truncation ofToken.csv
prior to the loop, to assure this run doesn't append to any existing file contents.
– Monty Harder
13 hours ago
add a comment |
Use the command below for reading n lines from file:
head -n 1 filename
to write it to variable use this:
var=$(head -n 1 filename);
Or you can read nth line from file:
sed -n '2p' filename
The comand above will return second line of file. For your example you can use this:
sed -n $i'p' filename
where i is index.
BUT, for your code you need an index that will increase every iteration.
New contributor
add a comment |
Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:
gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
BEGIN{cmd="sha256sum"; s = systime()}
{
print $0 s stringToken |& cmd; close(cmd,"to");
cmd |& getline; close(cmd,"from")
} 1
' SerialNos.csv > Token.csv
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
});
}
});
Shubham Agrawal is a new contributor. Be nice, and check out our Code of Conduct.
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%2f1138318%2fread-line-from-file-and-process-something%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
4 Answers
4
active
oldest
votes
4 Answers
4
active
oldest
votes
active
oldest
votes
active
oldest
votes
Put the output redirection on the entire loop, not just the sha256sum
command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv
add a comment |
Put the output redirection on the entire loop, not just the sha256sum
command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv
add a comment |
Put the output redirection on the entire loop, not just the sha256sum
command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv
Put the output redirection on the entire loop, not just the sha256sum
command. Every time you redirect, you're recreating the output file from scratch. This will just create it once, and write to it repeatedly within the loop.
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum
done < "$StringCsv" > Token.csv
answered 12 hours ago
BarmarBarmar
1756
1756
add a comment |
add a comment |
You are writing to a file in a loop with this command:
echo "$serial"|sha256sum > Token.csv
However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:
echo "$serial"|sha256sum >> Token.csv
A single >
tells bash to erase the file Token.csv
and write the contents. A double >>
tells bash to add to the end of the file.
The bash script would now look like this:
!/bin/bash
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
> Token.csv # Empty file from last run
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum >> Token.csv # Append new record to end
done < "$StringCsv"
There are two ways to empty the file > Token.csv
as used above and touch Token.csv
. See:
- What is the difference between touch file and > file?
There should probably be an explicit removal/truncation ofToken.csv
prior to the loop, to assure this run doesn't append to any existing file contents.
– Monty Harder
13 hours ago
add a comment |
You are writing to a file in a loop with this command:
echo "$serial"|sha256sum > Token.csv
However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:
echo "$serial"|sha256sum >> Token.csv
A single >
tells bash to erase the file Token.csv
and write the contents. A double >>
tells bash to add to the end of the file.
The bash script would now look like this:
!/bin/bash
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
> Token.csv # Empty file from last run
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum >> Token.csv # Append new record to end
done < "$StringCsv"
There are two ways to empty the file > Token.csv
as used above and touch Token.csv
. See:
- What is the difference between touch file and > file?
There should probably be an explicit removal/truncation ofToken.csv
prior to the loop, to assure this run doesn't append to any existing file contents.
– Monty Harder
13 hours ago
add a comment |
You are writing to a file in a loop with this command:
echo "$serial"|sha256sum > Token.csv
However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:
echo "$serial"|sha256sum >> Token.csv
A single >
tells bash to erase the file Token.csv
and write the contents. A double >>
tells bash to add to the end of the file.
The bash script would now look like this:
!/bin/bash
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
> Token.csv # Empty file from last run
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum >> Token.csv # Append new record to end
done < "$StringCsv"
There are two ways to empty the file > Token.csv
as used above and touch Token.csv
. See:
- What is the difference between touch file and > file?
You are writing to a file in a loop with this command:
echo "$serial"|sha256sum > Token.csv
However each time you loop you are erasing the file and writing a new entry. What you want to do is append (add to) the file each time you loop with this command:
echo "$serial"|sha256sum >> Token.csv
A single >
tells bash to erase the file Token.csv
and write the contents. A double >>
tells bash to add to the end of the file.
The bash script would now look like this:
!/bin/bash
epoch=$(date -d "`date`" +"%s")
StringCsv="/home/Desktop/TokenGenScript/SerialNos.csv"
StringToken=b5242a2d7973c1aca3723c834ba0d239
> Token.csv # Empty file from last run
while IFS=$'n' read -r line || [ -n "$line" ]
do
j=$line
serial=${j}:${epoch}:${StringToken}
echo "$serial"|sha256sum >> Token.csv # Append new record to end
done < "$StringCsv"
There are two ways to empty the file > Token.csv
as used above and touch Token.csv
. See:
- What is the difference between touch file and > file?
edited 2 hours ago
answered 16 hours ago
WinEunuuchs2UnixWinEunuuchs2Unix
48.7k1198187
48.7k1198187
There should probably be an explicit removal/truncation ofToken.csv
prior to the loop, to assure this run doesn't append to any existing file contents.
– Monty Harder
13 hours ago
add a comment |
There should probably be an explicit removal/truncation ofToken.csv
prior to the loop, to assure this run doesn't append to any existing file contents.
– Monty Harder
13 hours ago
There should probably be an explicit removal/truncation of
Token.csv
prior to the loop, to assure this run doesn't append to any existing file contents.– Monty Harder
13 hours ago
There should probably be an explicit removal/truncation of
Token.csv
prior to the loop, to assure this run doesn't append to any existing file contents.– Monty Harder
13 hours ago
add a comment |
Use the command below for reading n lines from file:
head -n 1 filename
to write it to variable use this:
var=$(head -n 1 filename);
Or you can read nth line from file:
sed -n '2p' filename
The comand above will return second line of file. For your example you can use this:
sed -n $i'p' filename
where i is index.
BUT, for your code you need an index that will increase every iteration.
New contributor
add a comment |
Use the command below for reading n lines from file:
head -n 1 filename
to write it to variable use this:
var=$(head -n 1 filename);
Or you can read nth line from file:
sed -n '2p' filename
The comand above will return second line of file. For your example you can use this:
sed -n $i'p' filename
where i is index.
BUT, for your code you need an index that will increase every iteration.
New contributor
add a comment |
Use the command below for reading n lines from file:
head -n 1 filename
to write it to variable use this:
var=$(head -n 1 filename);
Or you can read nth line from file:
sed -n '2p' filename
The comand above will return second line of file. For your example you can use this:
sed -n $i'p' filename
where i is index.
BUT, for your code you need an index that will increase every iteration.
New contributor
Use the command below for reading n lines from file:
head -n 1 filename
to write it to variable use this:
var=$(head -n 1 filename);
Or you can read nth line from file:
sed -n '2p' filename
The comand above will return second line of file. For your example you can use this:
sed -n $i'p' filename
where i is index.
BUT, for your code you need an index that will increase every iteration.
New contributor
edited 11 hours ago
New contributor
answered 11 hours ago
Vlad GavriukVlad Gavriuk
1113
1113
New contributor
New contributor
add a comment |
add a comment |
Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:
gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
BEGIN{cmd="sha256sum"; s = systime()}
{
print $0 s stringToken |& cmd; close(cmd,"to");
cmd |& getline; close(cmd,"from")
} 1
' SerialNos.csv > Token.csv
add a comment |
Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:
gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
BEGIN{cmd="sha256sum"; s = systime()}
{
print $0 s stringToken |& cmd; close(cmd,"to");
cmd |& getline; close(cmd,"from")
} 1
' SerialNos.csv > Token.csv
add a comment |
Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:
gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
BEGIN{cmd="sha256sum"; s = systime()}
{
print $0 s stringToken |& cmd; close(cmd,"to");
cmd |& getline; close(cmd,"from")
} 1
' SerialNos.csv > Token.csv
Taking Why is using a shell loop to process text considered bad practice? way too seriously, and using GNU Awk's getline from a Coprocess:
gawk -v stringToken=b5242a2d7973c1aca3723c834ba0d239 '
BEGIN{cmd="sha256sum"; s = systime()}
{
print $0 s stringToken |& cmd; close(cmd,"to");
cmd |& getline; close(cmd,"from")
} 1
' SerialNos.csv > Token.csv
answered 9 hours ago
steeldriversteeldriver
71.4k11115187
71.4k11115187
add a comment |
add a comment |
Shubham Agrawal is a new contributor. Be nice, and check out our Code of Conduct.
Shubham Agrawal is a new contributor. Be nice, and check out our Code of Conduct.
Shubham Agrawal is a new contributor. Be nice, and check out our Code of Conduct.
Shubham Agrawal is a new contributor. Be nice, and check out our Code of Conduct.
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%2f1138318%2fread-line-from-file-and-process-something%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
1
and your code is not working or what is the problem? Btw, you don't need the doubled
date
.– RoVo
17 hours ago