CSV Create a new column removing spaces
I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.
Example...
Input:
a b,xyz,d e f
a b c,xyz,d e f
a b c d,xyz,d e f
Output:
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
shell-script
add a comment |
I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.
Example...
Input:
a b,xyz,d e f
a b c,xyz,d e f
a b c d,xyz,d e f
Output:
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
shell-script
add a comment |
I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.
Example...
Input:
a b,xyz,d e f
a b c,xyz,d e f
a b c d,xyz,d e f
Output:
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
shell-script
I have a large csv file which contains multiple columns. I want the spaces to be removed from the first column and want to create that as a new column.
Example...
Input:
a b,xyz,d e f
a b c,xyz,d e f
a b c d,xyz,d e f
Output:
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
shell-script
shell-script
edited Dec 26 '18 at 23:05
ctrl-alt-delor
11.1k42058
11.1k42058
asked Nov 13 '17 at 3:22
ThomasThomas
193
193
add a comment |
add a comment |
5 Answers
5
active
oldest
votes
Using sed:
sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv
Explained:
h - stash current line to the hold space
s/,.*/,/ - remove everything after the first comma
s/ //g - remove spaces
G - append the line from the hold space back to the pattern space
s/n// - remove extra newline, as left by G
add a comment |
Using bash
with standard tools:
$ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
paste
combines the input from the two given files with a comma as the delimiter.
The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.
The second file is simply the original unmodified file.
For plain POSIX shells this also works:cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
add a comment |
bash
solution assuming a file test.txt
:
#!/bin/bash
while read -r line; do
IFS=',' read -ra fields <<<"$line"
(IFS=','; echo "${fields[0]// /},${fields[*]}")
done <"test.txt"
exit
This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt
into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using *
will output the elements separated by IFS
, which we previously set to a comma. The inline declaration of IFS
in the read
statement and inside a subshell ()
will preserve the value of IFS
for the rest of the script's execution.
add a comment |
awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file
Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.
add a comment |
with the great Miller is:
mlr --csv --implicit-csv-header --headerless-csv-output
put '$newField=gsub($1," +","")'
then reorder -f newField input.csv
$1
is the first field. Than I apply a regex search and replace to obtain the new field.
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
});
}
});
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%2f404148%2fcsv-create-a-new-column-removing-spaces%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
Using sed:
sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv
Explained:
h - stash current line to the hold space
s/,.*/,/ - remove everything after the first comma
s/ //g - remove spaces
G - append the line from the hold space back to the pattern space
s/n// - remove extra newline, as left by G
add a comment |
Using sed:
sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv
Explained:
h - stash current line to the hold space
s/,.*/,/ - remove everything after the first comma
s/ //g - remove spaces
G - append the line from the hold space back to the pattern space
s/n// - remove extra newline, as left by G
add a comment |
Using sed:
sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv
Explained:
h - stash current line to the hold space
s/,.*/,/ - remove everything after the first comma
s/ //g - remove spaces
G - append the line from the hold space back to the pattern space
s/n// - remove extra newline, as left by G
Using sed:
sed 'h;s/,.*/,/;s/ //g;G;s/n//' data.csv
Explained:
h - stash current line to the hold space
s/,.*/,/ - remove everything after the first comma
s/ //g - remove spaces
G - append the line from the hold space back to the pattern space
s/n// - remove extra newline, as left by G
answered Nov 15 '17 at 19:06
zeppelinzeppelin
3,130416
3,130416
add a comment |
add a comment |
Using bash
with standard tools:
$ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
paste
combines the input from the two given files with a comma as the delimiter.
The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.
The second file is simply the original unmodified file.
For plain POSIX shells this also works:cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
add a comment |
Using bash
with standard tools:
$ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
paste
combines the input from the two given files with a comma as the delimiter.
The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.
The second file is simply the original unmodified file.
For plain POSIX shells this also works:cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
add a comment |
Using bash
with standard tools:
$ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
paste
combines the input from the two given files with a comma as the delimiter.
The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.
The second file is simply the original unmodified file.
Using bash
with standard tools:
$ paste -d ',' <( cut -d ',' -f 1 file | tr -d ' ' ) file
ab,a b,xyz,d e f
abc,a b c,xyz,d e f
abcd,a b c d,xyz,d e f
paste
combines the input from the two given files with a comma as the delimiter.
The first file is produce by a process substitution which extracts the first comma-delimited column of the file and removes its spaces.
The second file is simply the original unmodified file.
answered Feb 10 '18 at 10:02
KusalanandaKusalananda
126k16239393
126k16239393
For plain POSIX shells this also works:cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
add a comment |
For plain POSIX shells this also works:cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
For plain POSIX shells this also works:
cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
For plain POSIX shells this also works:
cut -d, -f1 file | tr -d ' ' | paste -d, - file
– agc
Jan 16 at 19:58
add a comment |
bash
solution assuming a file test.txt
:
#!/bin/bash
while read -r line; do
IFS=',' read -ra fields <<<"$line"
(IFS=','; echo "${fields[0]// /},${fields[*]}")
done <"test.txt"
exit
This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt
into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using *
will output the elements separated by IFS
, which we previously set to a comma. The inline declaration of IFS
in the read
statement and inside a subshell ()
will preserve the value of IFS
for the rest of the script's execution.
add a comment |
bash
solution assuming a file test.txt
:
#!/bin/bash
while read -r line; do
IFS=',' read -ra fields <<<"$line"
(IFS=','; echo "${fields[0]// /},${fields[*]}")
done <"test.txt"
exit
This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt
into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using *
will output the elements separated by IFS
, which we previously set to a comma. The inline declaration of IFS
in the read
statement and inside a subshell ()
will preserve the value of IFS
for the rest of the script's execution.
add a comment |
bash
solution assuming a file test.txt
:
#!/bin/bash
while read -r line; do
IFS=',' read -ra fields <<<"$line"
(IFS=','; echo "${fields[0]// /},${fields[*]}")
done <"test.txt"
exit
This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt
into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using *
will output the elements separated by IFS
, which we previously set to a comma. The inline declaration of IFS
in the read
statement and inside a subshell ()
will preserve the value of IFS
for the rest of the script's execution.
bash
solution assuming a file test.txt
:
#!/bin/bash
while read -r line; do
IFS=',' read -ra fields <<<"$line"
(IFS=','; echo "${fields[0]// /},${fields[*]}")
done <"test.txt"
exit
This takes advantage of IFS to read the values of the csv into an array and output them. Read each line of test.txt
into a string, then read this line into an array by splitting on commas. Print the first element of this array with spaces removed, then the entire array. A quoted array using *
will output the elements separated by IFS
, which we previously set to a comma. The inline declaration of IFS
in the read
statement and inside a subshell ()
will preserve the value of IFS
for the rest of the script's execution.
answered Nov 15 '17 at 18:42
m0dularm0dular
70115
70115
add a comment |
add a comment |
awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file
Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.
add a comment |
awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file
Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.
add a comment |
awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file
Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.
awk 'BEGIN { FS=","; OFS="," } { col=$1; gsub(/ /, "", col); print col,$0 }' file
Use commas as input and output column separators. Extract first column and sub out spaces. Output the modified first column followed by the original line.
edited Dec 26 '18 at 22:55
Rui F Ribeiro
39.6k1479132
39.6k1479132
answered Nov 13 '17 at 3:50
B LayerB Layer
4,0241525
4,0241525
add a comment |
add a comment |
with the great Miller is:
mlr --csv --implicit-csv-header --headerless-csv-output
put '$newField=gsub($1," +","")'
then reorder -f newField input.csv
$1
is the first field. Than I apply a regex search and replace to obtain the new field.
add a comment |
with the great Miller is:
mlr --csv --implicit-csv-header --headerless-csv-output
put '$newField=gsub($1," +","")'
then reorder -f newField input.csv
$1
is the first field. Than I apply a regex search and replace to obtain the new field.
add a comment |
with the great Miller is:
mlr --csv --implicit-csv-header --headerless-csv-output
put '$newField=gsub($1," +","")'
then reorder -f newField input.csv
$1
is the first field. Than I apply a regex search and replace to obtain the new field.
with the great Miller is:
mlr --csv --implicit-csv-header --headerless-csv-output
put '$newField=gsub($1," +","")'
then reorder -f newField input.csv
$1
is the first field. Than I apply a regex search and replace to obtain the new field.
answered Dec 28 '18 at 7:13
aborrusoaborruso
20619
20619
add a comment |
add a comment |
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%2f404148%2fcsv-create-a-new-column-removing-spaces%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