Add Columns to .csv from Multiple Filescsv file with multiple columns to zenityPick columns from a variable length csv fileMerging contents of multiple .csv files into single .csv fileHow to swap certain columns in .csv fileRemove specific columns from csv using awkTransform existing .CSV by comparing previous row with current and minor calculationsMatching columns of different csv files, not working when column value is different lengthUsing bash to swap first and second columns in CSVSwitch columns in CSV using awk?Connecting files like columns
On studying Computer Science vs. Software Engineering to become a proficient coder
Ito`s Lemma problem
What are the components of a legend (in the sense of a tale, not a figure legend)?
Labeling matrices/rectangles and drawing Sigma inside rectangle
If current results hold, Man City would win PL title
Why are solar panels kept tilted?
correct spelling of "carruffel" (fuzz, hustle, all that jazz)
Automatically anti-predictably assemble an alliterative aria
Is there any good reason to write "it is easy to see"?
When a land becomes a creature, is it untapped?
Is there anything special about -1 (0xFFFFFFFF) regarding ADC?
What are the implications of the new alleged key recovery attack preprint on SIMON?
Why is it harder to turn a motor/generator with shorted terminals?
Jumping frame contents with beamer and pgfplots
How do I tell my supervisor that he is choosing poor replacements for me while I am on maternity leave?
Longest Text in Latin
Why is a set not a partition of itself?
As programers say: Strive to be lazy
What's tha name for when you write multiple voices on same staff? And are there any cons?
Smallest Guaranteed hash collision cycle length
Is 12 minutes connection in Bristol Temple Meads long enough?
declared variable inside void setup is forgotten in void loop
Does Lawful Interception of 4G / the proposed 5G provide a back door for hackers as well?
Would an 8% reduction in drag outweigh the weight addition from this custom CFD-tested winglet?
Add Columns to .csv from Multiple Files
csv file with multiple columns to zenityPick columns from a variable length csv fileMerging contents of multiple .csv files into single .csv fileHow to swap certain columns in .csv fileRemove specific columns from csv using awkTransform existing .CSV by comparing previous row with current and minor calculationsMatching columns of different csv files, not working when column value is different lengthUsing bash to swap first and second columns in CSVSwitch columns in CSV using awk?Connecting files like columns
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;
I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.
while read i; do
awk 'print $4' $i.txt > $i_temp
awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
done < file_list
The file list is just a list of accession numbers:
NA123
NA124
NA125
...
The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:
NA123.txt:
April-18 10:00 18:00 8
April-19 09:00 19:00 10
April-20 11:00 16:00 5
...
NA124.txt:
April-18 14:00 18:00 4
April-19 09:00 15:00 6
April-20 07:00 16:00 9
...
NA125.txt:
April-18 10:00 22:00 12
April-19 09:00 12:00 3
April-20 06:00 16:00 10
...
test.csv:
0,
1,
2,
...
I would like the output to be:
0,8,4,12
1,10,6,3
2,5,9,10
...
What do I need to change about this or is there a more efficient way to do this?
text-processing awk csv
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.
while read i; do
awk 'print $4' $i.txt > $i_temp
awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
done < file_list
The file list is just a list of accession numbers:
NA123
NA124
NA125
...
The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:
NA123.txt:
April-18 10:00 18:00 8
April-19 09:00 19:00 10
April-20 11:00 16:00 5
...
NA124.txt:
April-18 14:00 18:00 4
April-19 09:00 15:00 6
April-20 07:00 16:00 9
...
NA125.txt:
April-18 10:00 22:00 12
April-19 09:00 12:00 3
April-20 06:00 16:00 10
...
test.csv:
0,
1,
2,
...
I would like the output to be:
0,8,4,12
1,10,6,3
2,5,9,10
...
What do I need to change about this or is there a more efficient way to do this?
text-processing awk csv
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.
while read i; do
awk 'print $4' $i.txt > $i_temp
awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
done < file_list
The file list is just a list of accession numbers:
NA123
NA124
NA125
...
The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:
NA123.txt:
April-18 10:00 18:00 8
April-19 09:00 19:00 10
April-20 11:00 16:00 5
...
NA124.txt:
April-18 14:00 18:00 4
April-19 09:00 15:00 6
April-20 07:00 16:00 9
...
NA125.txt:
April-18 10:00 22:00 12
April-19 09:00 12:00 3
April-20 06:00 16:00 10
...
test.csv:
0,
1,
2,
...
I would like the output to be:
0,8,4,12
1,10,6,3
2,5,9,10
...
What do I need to change about this or is there a more efficient way to do this?
text-processing awk csv
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
I'm trying to add columns to a csv file from multiple files. This is what I have tried so far, but keep ending up with either an empty file or just the column from the last file.
while read i; do
awk 'print $4' $i.txt > $i_temp
awk 'NR==FNRa[NR]=$0;nextprint a[FNR],$0' OFS=, test.csv $i_temp >> test.csv
done < file_list
The file list is just a list of accession numbers:
NA123
NA124
NA125
...
The text files that they correspond to have 4 columns in them, and I want to copy the last column and add it as the next column in a csv file. The contents of the files look like this:
NA123.txt:
April-18 10:00 18:00 8
April-19 09:00 19:00 10
April-20 11:00 16:00 5
...
NA124.txt:
April-18 14:00 18:00 4
April-19 09:00 15:00 6
April-20 07:00 16:00 9
...
NA125.txt:
April-18 10:00 22:00 12
April-19 09:00 12:00 3
April-20 06:00 16:00 10
...
test.csv:
0,
1,
2,
...
I would like the output to be:
0,8,4,12
1,10,6,3
2,5,9,10
...
What do I need to change about this or is there a more efficient way to do this?
text-processing awk csv
text-processing awk csv
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited May 8 at 17:48
thebriterican
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
asked May 8 at 17:11
thebritericanthebriterican
234
234
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
thebriterican is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
3 Answers
3
active
oldest
votes
Maybe you could try to use paste and one additional temp file
touch temp
while read i; do
awk 'print $4' $i.txt > $i_temp
paste temp $i_temp > test.csv
cp test.csv temp
done < file_list
rm temp
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
try:
<file_list xargs -I % awk '
system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt
the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.
with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.
note that sed is writing in-place any changes, so you will not notice terminal ouput, do:
$ cat test.csv
0,8,4,12
1,10,6,3
2,5,9,10
add a comment |
Why not just use paste?
$ cat in1; cat in2
row1,col2,col3
row2,col2,col3
row3,col2,col3
row4,col2,col3
row1
row2
row3
row4
$ paste -d, in1 in2
row1,col2,col3,row1
row2,col2,col3,row2
row3,col2,col3,row3
row4,col2,col3,row4
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
add a comment |
Your Answer
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "106"
;
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
);
);
thebriterican 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%2funix.stackexchange.com%2fquestions%2f517836%2fadd-columns-to-csv-from-multiple-files%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
Maybe you could try to use paste and one additional temp file
touch temp
while read i; do
awk 'print $4' $i.txt > $i_temp
paste temp $i_temp > test.csv
cp test.csv temp
done < file_list
rm temp
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Maybe you could try to use paste and one additional temp file
touch temp
while read i; do
awk 'print $4' $i.txt > $i_temp
paste temp $i_temp > test.csv
cp test.csv temp
done < file_list
rm temp
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
Maybe you could try to use paste and one additional temp file
touch temp
while read i; do
awk 'print $4' $i.txt > $i_temp
paste temp $i_temp > test.csv
cp test.csv temp
done < file_list
rm temp
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
Maybe you could try to use paste and one additional temp file
touch temp
while read i; do
awk 'print $4' $i.txt > $i_temp
paste temp $i_temp > test.csv
cp test.csv temp
done < file_list
rm temp
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
edited May 8 at 19:07
αғsнιη
18.1k113271
18.1k113271
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
answered May 8 at 18:41
user352003user352003
261
261
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
New contributor
user352003 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.
add a comment |
add a comment |
try:
<file_list xargs -I % awk '
system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt
the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.
with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.
note that sed is writing in-place any changes, so you will not notice terminal ouput, do:
$ cat test.csv
0,8,4,12
1,10,6,3
2,5,9,10
add a comment |
try:
<file_list xargs -I % awk '
system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt
the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.
with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.
note that sed is writing in-place any changes, so you will not notice terminal ouput, do:
$ cat test.csv
0,8,4,12
1,10,6,3
2,5,9,10
add a comment |
try:
<file_list xargs -I % awk '
system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt
the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.
with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.
note that sed is writing in-place any changes, so you will not notice terminal ouput, do:
$ cat test.csv
0,8,4,12
1,10,6,3
2,5,9,10
try:
<file_list xargs -I % awk '
system("sed -i ''' " NR "s/,\?$/," $4 "/''' test.csv" ) ' %.txt
the xargs is reading filename from file_list file and feed to awk thought xarags's variable called % with .txt suffix as filename contains.
with sed '#s/$/something/' test.csv" command that used to append something at the end $ of a specific line number # in a file like test.csv; so at above, $4 is the string that need to appended to specific line number taking from NR; ,?$ is saying might your line ends with comma , which I used because you have it in your test.csv. this sed command is calling by awk's system() function.
note that sed is writing in-place any changes, so you will not notice terminal ouput, do:
$ cat test.csv
0,8,4,12
1,10,6,3
2,5,9,10
answered May 8 at 18:53
αғsнιηαғsнιη
18.1k113271
18.1k113271
add a comment |
add a comment |
Why not just use paste?
$ cat in1; cat in2
row1,col2,col3
row2,col2,col3
row3,col2,col3
row4,col2,col3
row1
row2
row3
row4
$ paste -d, in1 in2
row1,col2,col3,row1
row2,col2,col3,row2
row3,col2,col3,row3
row4,col2,col3,row4
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
add a comment |
Why not just use paste?
$ cat in1; cat in2
row1,col2,col3
row2,col2,col3
row3,col2,col3
row4,col2,col3
row1
row2
row3
row4
$ paste -d, in1 in2
row1,col2,col3,row1
row2,col2,col3,row2
row3,col2,col3,row3
row4,col2,col3,row4
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
add a comment |
Why not just use paste?
$ cat in1; cat in2
row1,col2,col3
row2,col2,col3
row3,col2,col3
row4,col2,col3
row1
row2
row3
row4
$ paste -d, in1 in2
row1,col2,col3,row1
row2,col2,col3,row2
row3,col2,col3,row3
row4,col2,col3,row4
Why not just use paste?
$ cat in1; cat in2
row1,col2,col3
row2,col2,col3
row3,col2,col3
row4,col2,col3
row1
row2
row3
row4
$ paste -d, in1 in2
row1,col2,col3,row1
row2,col2,col3,row2
row3,col2,col3,row3
row4,col2,col3,row4
answered May 8 at 17:20
DopeGhotiDopeGhoti
47.8k56195
47.8k56195
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
add a comment |
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
OP needs merge column 4 from all files where their partial name is in file_list with test.csv file
– αғsнιη
May 9 at 1:42
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
That was not clear in the original presentation of the question which I addressed with this answer.
– DopeGhoti
May 9 at 15:40
add a comment |
thebriterican is a new contributor. Be nice, and check out our Code of Conduct.
thebriterican is a new contributor. Be nice, and check out our Code of Conduct.
thebriterican is a new contributor. Be nice, and check out our Code of Conduct.
thebriterican is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Unix & Linux Stack Exchange!
- 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%2funix.stackexchange.com%2fquestions%2f517836%2fadd-columns-to-csv-from-multiple-files%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