LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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

Reply
 
Search this Thread
Old 05-18-2011, 01:36 AM   #1
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Rep: Reputation: 15
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
 
Old 05-18-2011, 01:44 AM   #2
EricTRA
Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 18 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290Reputation: 1290
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
 
Old 05-18-2011, 01:48 AM   #3
Telengard
Member
 
Registered: Apr 2007
Location: USA
Distribution: Kubuntu 8.04
Posts: 579
Blog Entries: 8

Rep: Reputation: 147Reputation: 147
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
 
Old 05-18-2011, 03:04 AM   #4
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by Telengard View Post
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
Code:
/ +|[+-]/
in your code? Thanks very much.
 
Old 05-18-2011, 05:01 AM   #5
grail
Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 7,426

Rep: Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876Reputation: 1876
Quote:
/ +|[+-]/
One or more spaces or a plus or minus

Alternative:
Code:
awk -F"[ -+]*" '$1=$1' file
 
1 members found this post helpful.
Old 05-18-2011, 06:56 AM   #6
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713Reputation: 713
Code:
sed 's/[-+ ]\+/ /g'
 
Old 05-18-2011, 07:14 AM   #7
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 12,103

Rep: Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982Reputation: 982
Personally I always use a class for whitespace ...
 
Old 05-18-2011, 10:12 PM   #8
dhodho
LQ Newbie
 
Registered: Jun 2010
Location: Indonesia
Distribution: Ubuntu 10.04
Posts: 26

Original Poster
Rep: Reputation: 15
Quote:
Originally Posted by grail View Post
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.
 
  


Reply

Tags
awk, regular expression, sed


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to remove everything before the first space in Sed or Awk OutThere Linux - General 1 04-05-2009 10:45 PM
How can I strip white space from the start and end of fields using awk? jonnymorris Programming 7 10-02-2008 10:52 PM
LXer: White Space LXer Syndicated Linux News 0 09-22-2007 08:40 PM
m4 and white space, does it exist? The_Nerd Programming 3 04-18-2007 05:19 PM
removing white space accent11 Linux - Software 4 10-06-2004 01:30 AM


All times are GMT -5. The time now is 08:06 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration