Unix - Remove Leading/Trailing Spaces (Column Wise)
I have text lines like these below:
P, 123456 ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,
I want them to be like below:
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
Requirements:
- Remove all leading & trailing spaces from 2,6,7 column only
- Values in 7th Column should remain separated by single space only
Research:
I have tried many combinations with awk
, sed
, tr
but could not succeed according to my requirements. I would like to have a preferable solution in awk
, sed
, tr
only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.
unix awk sed tr
add a comment |
I have text lines like these below:
P, 123456 ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,
I want them to be like below:
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
Requirements:
- Remove all leading & trailing spaces from 2,6,7 column only
- Values in 7th Column should remain separated by single space only
Research:
I have tried many combinations with awk
, sed
, tr
but could not succeed according to my requirements. I would like to have a preferable solution in awk
, sed
, tr
only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.
unix awk sed tr
see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...
– Sundeep
Nov 3 '16 at 8:42
I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305
– user3676305
Nov 3 '16 at 8:49
add a comment |
I have text lines like these below:
P, 123456 ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,
I want them to be like below:
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
Requirements:
- Remove all leading & trailing spaces from 2,6,7 column only
- Values in 7th Column should remain separated by single space only
Research:
I have tried many combinations with awk
, sed
, tr
but could not succeed according to my requirements. I would like to have a preferable solution in awk
, sed
, tr
only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.
unix awk sed tr
I have text lines like these below:
P, 123456 ,01,A,H, 123456 ,123456 123456 ,,
P,123456 ,01,A,H, 123456, 123456 123456,,
P, 123456,01,A,H,123456 ,123456 123456 ,,
P, 123456,01,A,H, 123456, 123456 123456,,
P,123456 ,01,A,H,123456 ,123456 123456 ,,
I want them to be like below:
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
P,123456,01,A,H,123456,123456 123456,,
Requirements:
- Remove all leading & trailing spaces from 2,6,7 column only
- Values in 7th Column should remain separated by single space only
Research:
I have tried many combinations with awk
, sed
, tr
but could not succeed according to my requirements. I would like to have a preferable solution in awk
, sed
, tr
only because all my clients have limited CYGWIN installed. So I cannot ask all my clients to install new executable.
unix awk sed tr
unix awk sed tr
edited Nov 3 '16 at 8:46
sat
11.7k23056
11.7k23056
asked Nov 3 '16 at 8:37
user3676305user3676305
408
408
see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...
– Sundeep
Nov 3 '16 at 8:42
I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305
– user3676305
Nov 3 '16 at 8:49
add a comment |
see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...
– Sundeep
Nov 3 '16 at 8:42
I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305
– user3676305
Nov 3 '16 at 8:49
see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...
– Sundeep
Nov 3 '16 at 8:42
see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...
– Sundeep
Nov 3 '16 at 8:42
I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305
– user3676305
Nov 3 '16 at 8:49
I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305
– user3676305
Nov 3 '16 at 8:49
add a comment |
2 Answers
2
active
oldest
votes
You can use this awk
:
awk -F' *, *' '$1=$1' OFS=, file
Understandable way:
awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file
As commented by @glennjackman, safer to use
awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file
Explanation:
FS
- Set input field separator
OFS
- Set output field separator
$1=$1
- This will makeawk
to format the fields withOFS
. This returnstrue
which makesawk
to print current line (reformatted).
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
1
Pedantically,$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with inputecho -e '0,firstn1,secondn,third'
. Safer to use{$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
|
show 2 more comments
sed is a good choice too.
sed 's/ *, */,/g' file
We're assuming this CSV file does not contain a line like this:
a, b, "this field, this very one, should not be touched", d
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%2f40396947%2funix-remove-leading-trailing-spaces-column-wise%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
You can use this awk
:
awk -F' *, *' '$1=$1' OFS=, file
Understandable way:
awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file
As commented by @glennjackman, safer to use
awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file
Explanation:
FS
- Set input field separator
OFS
- Set output field separator
$1=$1
- This will makeawk
to format the fields withOFS
. This returnstrue
which makesawk
to print current line (reformatted).
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
1
Pedantically,$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with inputecho -e '0,firstn1,secondn,third'
. Safer to use{$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
|
show 2 more comments
You can use this awk
:
awk -F' *, *' '$1=$1' OFS=, file
Understandable way:
awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file
As commented by @glennjackman, safer to use
awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file
Explanation:
FS
- Set input field separator
OFS
- Set output field separator
$1=$1
- This will makeawk
to format the fields withOFS
. This returnstrue
which makesawk
to print current line (reformatted).
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
1
Pedantically,$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with inputecho -e '0,firstn1,secondn,third'
. Safer to use{$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
|
show 2 more comments
You can use this awk
:
awk -F' *, *' '$1=$1' OFS=, file
Understandable way:
awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file
As commented by @glennjackman, safer to use
awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file
Explanation:
FS
- Set input field separator
OFS
- Set output field separator
$1=$1
- This will makeawk
to format the fields withOFS
. This returnstrue
which makesawk
to print current line (reformatted).
You can use this awk
:
awk -F' *, *' '$1=$1' OFS=, file
Understandable way:
awk 'BEGIN{FS=" *, *"; OFS=","} $1=$1' file
As commented by @glennjackman, safer to use
awk 'BEGIN{FS=" *, *"; OFS=","} {$1=$1; print}' file
Explanation:
FS
- Set input field separator
OFS
- Set output field separator
$1=$1
- This will makeawk
to format the fields withOFS
. This returnstrue
which makesawk
to print current line (reformatted).
edited Nov 3 '16 at 11:13
answered Nov 3 '16 at 8:47
satsat
11.7k23056
11.7k23056
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
1
Pedantically,$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with inputecho -e '0,firstn1,secondn,third'
. Safer to use{$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
|
show 2 more comments
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
1
Pedantically,$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with inputecho -e '0,firstn1,secondn,third'
. Safer to use{$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
worked flawlessly !!! Thanks a lot. It would be nice if you can describe your solution a bit, that would be more helpful in further developments.
– user3676305
Nov 3 '16 at 9:00
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
Did it solve your problem, @user3676305 ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. You can do it clicking on the check mark beside the answer to toggle it from hollow to green. See Help Center > Asking if you have any question!
– fedorqui
Nov 3 '16 at 9:21
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
@user3676305, Updated answer.
– sat
Nov 3 '16 at 9:28
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
Thanks for the explanation. Your solution has been marked as "Accepted"
– user3676305
Nov 3 '16 at 9:47
1
1
Pedantically,
$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'
. Safer to use {$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
Pedantically,
$1=$1
returns true if $1 is a non-empty string or is not the number 0. That happens to be the case for this input, but if not, you'll be missing rows in the output. Test with input echo -e '0,firstn1,secondn,third'
. Safer to use {$1=$1; print}
– glenn jackman
Nov 3 '16 at 10:49
|
show 2 more comments
sed is a good choice too.
sed 's/ *, */,/g' file
We're assuming this CSV file does not contain a line like this:
a, b, "this field, this very one, should not be touched", d
add a comment |
sed is a good choice too.
sed 's/ *, */,/g' file
We're assuming this CSV file does not contain a line like this:
a, b, "this field, this very one, should not be touched", d
add a comment |
sed is a good choice too.
sed 's/ *, */,/g' file
We're assuming this CSV file does not contain a line like this:
a, b, "this field, this very one, should not be touched", d
sed is a good choice too.
sed 's/ *, */,/g' file
We're assuming this CSV file does not contain a line like this:
a, b, "this field, this very one, should not be touched", d
answered Nov 3 '16 at 10:53
glenn jackmanglenn jackman
166k26143235
166k26143235
add a comment |
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%2f40396947%2funix-remove-leading-trailing-spaces-column-wise%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
see stackoverflow.com/editing-help for formatting and add the commands you tried instead of just saying that you tried...
– Sundeep
Nov 3 '16 at 8:42
I also tried this solution, but its not working stackoverflow.com/a/28548655/3676305
– user3676305
Nov 3 '16 at 8:49