Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to
LinuxQuestions.org , a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free.
Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please
contact us . If you need to reset your password,
click here .
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a
virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month.
Click here for more info.
05-18-2011, 01:36 AM
#1
LQ Newbie
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26
Rep:
Remove white space, +, and -, by awk
Hi, everyone
I have some data whose no certain pattern. Below is as an example.
20110101 1+1-0 1-1 1+1-2 8- 5 3 0 3 4 5 3 7 4
20110102 1+2-0+1-1-1+2-1+ 9 5 6 2 3 3 5 6 5 4
20110103 1-1-0+1 1 2 2 2+10 3 3 2 4 4 7 7 9 5
20110104 1+3-1 1 1+2-2-1-11+ 5 12 4 4 5 6 6 3 6
20110105 0 0 0 1-1-1-1+1 4+ 0 0 0 3 3 3 5 4 2
20110106 1+0+0 0+0+1 2 4+10- 5 2 0 2 2 4 7 32 7
I want to remove space, + and - mark so that I can get the below such data :
20110101 1 1 0 1 1 1 1 2 8 5 3 0 3 4 5 3 7 4
20110102 1 2 0 1 1 1 2 1 9 5 6 2 3 3 5 6 5 4
20110103 1 1 0 1 1 2 2 2 10 3 3 2 4 4 7 7 9 5
20110104 1 3 1 1 1 2 2 1 11 5 12 4 4 5 6 6 3 6
20110105 0 0 0 1 1 1 1 1 4 0 0 0 3 3 3 5 4 2
20110106 1 0 0 0 0 1 2 4 10 5 2 0 2 2 4 7 32 7
Is it possible by using awk? Thanks very much for advance.
Regards,
dhodho
05-18-2011, 01:44 AM
#2
LQ Guru
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Hello,
I'm sure it's possible using awk, sed and lots of other tools. This one is by far the simplest I think:
Code:
cat yourfile | tr "+,-" " "
which will print the contents of yourfile to your screen. If you want it in a file then redirect the output.
Kind regards,
Eric
05-18-2011, 01:48 AM
#3
Member
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Rep:
You say you want to remove spaces, but your output data contains spaces. I'm not sure I understand.
Code:
foo$ cat input-file.txt
20110101 1+1-0 1-1 1+1-2 8- 5 3 0 3 4 5 3 7 4
20110102 1+2-0+1-1-1+2-1+ 9 5 6 2 3 3 5 6 5 4
20110103 1-1-0+1 1 2 2 2+10 3 3 2 4 4 7 7 9 5
20110104 1+3-1 1 1+2-2-1-11+ 5 12 4 4 5 6 6 3 6
20110105 0 0 0 1-1-1-1+1 4+ 0 0 0 3 3 3 5 4 2
20110106 1+0+0 0+0+1 2 4+10- 5 2 0 2 2 4 7 32 7
foo$ awk '{gsub(/ +|[+-]/," ");print}' input-file.txt
20110101 1 1 0 1 1 1 1 2 8 5 3 0 3 4 5 3 7 4
20110102 1 2 0 1 1 1 2 1 9 5 6 2 3 3 5 6 5 4
20110103 1 1 0 1 1 2 2 2 10 3 3 2 4 4 7 7 9 5
20110104 1 3 1 1 1 2 2 1 11 5 12 4 4 5 6 6 3 6
20110105 0 0 0 1 1 1 1 1 4 0 0 0 3 3 3 5 4 2
20110106 1 0 0 0 0 1 2 4 10 5 2 0 2 2 4 7 32 7
The output data block exactly matches your example output data block.
HTH
Last edited by Telengard; 05-18-2011 at 01:55 AM .
Reason: remove copy/paste fail
05-18-2011, 03:04 AM
#4
LQ Newbie
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26
Original Poster
Rep:
Quote:
Originally Posted by
Telengard
You say you want to remove spaces, but your output data contains spaces. I'm not sure I understand.
Code:
foo$ cat input-file.txt
20110101 1+1-0 1-1 1+1-2 8- 5 3 0 3 4 5 3 7 4
20110102 1+2-0+1-1-1+2-1+ 9 5 6 2 3 3 5 6 5 4
20110103 1-1-0+1 1 2 2 2+10 3 3 2 4 4 7 7 9 5
20110104 1+3-1 1 1+2-2-1-11+ 5 12 4 4 5 6 6 3 6
20110105 0 0 0 1-1-1-1+1 4+ 0 0 0 3 3 3 5 4 2
20110106 1+0+0 0+0+1 2 4+10- 5 2 0 2 2 4 7 32 7
foo$ awk '{gsub(/ +|[+-]/," ");print}' input-file.txt
20110101 1 1 0 1 1 1 1 2 8 5 3 0 3 4 5 3 7 4
20110102 1 2 0 1 1 1 2 1 9 5 6 2 3 3 5 6 5 4
20110103 1 1 0 1 1 2 2 2 10 3 3 2 4 4 7 7 9 5
20110104 1 3 1 1 1 2 2 1 11 5 12 4 4 5 6 6 3 6
20110105 0 0 0 1 1 1 1 1 4 0 0 0 3 3 3 5 4 2
20110106 1 0 0 0 0 1 2 4 10 5 2 0 2 2 4 7 32 7
The output data block exactly matches your example output data block.
HTH
Thanks very much for your quick response. Yes, you are right. The output which I want to is the same as your output. I also tried below code :
Code:
awk '{gsub(/[+-]/," ");print}' input-file.txt
The code gave the same result with your result. If you don't mind could you give some explanation about
in your code? Thanks very much.
05-18-2011, 05:01 AM
#5
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007
One or more spaces or a plus or minus
Alternative:
Code:
awk -F"[ -+]*" '$1=$1' file
1 members found this post helpful.
05-18-2011, 06:56 AM
#6
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Code:
sed 's/[-+ ]\+/ /g'
05-18-2011, 07:14 AM
#7
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 21,126
Personally I always use a class for whitespace ...
05-18-2011, 10:12 PM
#8
LQ Newbie
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26
Original Poster
Rep:
Quote:
Originally Posted by
grail
One or more spaces or a plus or minus
Alternative:
Code:
awk -F"[ -+]*" '$1=$1' file
Thanks very much for your reply. Now, it is clear for me.
All times are GMT -5. The time now is 09:13 AM .
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know .
Latest Threads
LQ News