How to avoid data trimming while formatting file in unix
up vote
0
down vote
favorite
I have a file in below format
A |b -c | c | d -c | | |
And I need to format the above file like below
A|b -c|c|d -c|||
Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).
text-formatting
add a comment |
up vote
0
down vote
favorite
I have a file in below format
A |b -c | c | d -c | | |
And I need to format the above file like below
A|b -c|c|d -c|||
Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).
text-formatting
Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have a file in below format
A |b -c | c | d -c | | |
And I need to format the above file like below
A|b -c|c|d -c|||
Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).
text-formatting
I have a file in below format
A |b -c | c | d -c | | |
And I need to format the above file like below
A|b -c|c|d -c|||
Please note, I don't want to remove the space from data.I just want to remove the extra space before the data terminator i.e | (pipe).
text-formatting
text-formatting
edited Nov 16 at 20:22
steeldriver
33.6k34982
33.6k34982
asked Nov 16 at 17:56
user313150
113
113
Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25
add a comment |
Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25
Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38
Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25
add a comment |
2 Answers
2
active
oldest
votes
up vote
2
down vote
One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:
sed 's/ *|/|/g' < input > output
To also remove spaces after the pipe:
sed 's/ *|/|/g; s/| */|/g' < input > output
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
No..it didn't remove
– user313150
Nov 16 at 18:46
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
|
show 3 more comments
up vote
0
down vote
You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:
awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:
sed 's/ *|/|/g' < input > output
To also remove spaces after the pipe:
sed 's/ *|/|/g; s/| */|/g' < input > output
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
No..it didn't remove
– user313150
Nov 16 at 18:46
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
|
show 3 more comments
up vote
2
down vote
One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:
sed 's/ *|/|/g' < input > output
To also remove spaces after the pipe:
sed 's/ *|/|/g; s/| */|/g' < input > output
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
No..it didn't remove
– user313150
Nov 16 at 18:46
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
|
show 3 more comments
up vote
2
down vote
up vote
2
down vote
One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:
sed 's/ *|/|/g' < input > output
To also remove spaces after the pipe:
sed 's/ *|/|/g; s/| */|/g' < input > output
One option would be to use sed to say "replace one or more spaces followed by a pipe with a pipe" -- and do that for every occurrence of those spaces & pipes:
sed 's/ *|/|/g' < input > output
To also remove spaces after the pipe:
sed 's/ *|/|/g; s/| */|/g' < input > output
edited Nov 16 at 20:39
answered Nov 16 at 18:00
Jeff Schaller
36.3k952119
36.3k952119
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
No..it didn't remove
– user313150
Nov 16 at 18:46
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
|
show 3 more comments
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
No..it didn't remove
– user313150
Nov 16 at 18:46
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
Thanks a lot ..It worked.
– user313150
Nov 16 at 18:08
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
what If I want to remove extra spaces before pipe | too..
– user313150
Nov 16 at 18:10
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
doesn't the above remove extra spaces before the pipe?
– Jeff Schaller
Nov 16 at 18:25
No..it didn't remove
– user313150
Nov 16 at 18:46
No..it didn't remove
– user313150
Nov 16 at 18:46
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
Do you have two spaces between the first slash and the asterisk?
– Jeff Schaller
Nov 16 at 18:49
|
show 3 more comments
up vote
0
down vote
You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:
awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file
add a comment |
up vote
0
down vote
You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:
awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file
add a comment |
up vote
0
down vote
up vote
0
down vote
You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:
awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file
You could use Awk, setting the input field separator to a pipe surrounded by any amount of whitespace and the output field separator to just a pipe:
awk 'BEGIN {FS="[ t]*\|[ t]*"; OFS="|"} {$1=$1} 1' file
answered Nov 16 at 20:23
steeldriver
33.6k34982
33.6k34982
add a comment |
add a comment |
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%2f482208%2fhow-to-avoid-data-trimming-while-formatting-file-in-unix%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
Is it fair to say that you now want to remove space from after the pipe as well?
– Jeff Schaller
Nov 16 at 20:38
If any of the answers solved your problem, please accept it by clicking the checkmark next to it. Thank you!
– Jeff Schaller
Nov 18 at 13:25