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.
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
Guru
Registered: May 2009
Location: Barcelona, Spain
Distribution: LMDE Gnome with Awesome WM + Kernel 3.3.0-1 amd64
Posts: 6,529
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
Guru
Registered: Sep 2009
Location: Perth
Distribution: Mint
Posts: 5,403
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
Distribution: Arch x86_64
Posts: 6,443
Code:
sed 's/[-+ ]\+/ /g'
05-18-2011, 07:14 AM
#7
LQ Veteran
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 10,467
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.
Thread Tools
Search this Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT -5. The time now is 05: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