Help with removal of spaces between operators and operands


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with removal of spaces between operators and operands
# 8  
Old 06-07-2011
Quote:
Originally Posted by abk07
hey I got the following message

Code:
sed: can't read s/ [\(\=\+\]//g: No such file or directory

You have to add -e before the first pattern too, anyway this command will not solve your problem.
# 9  
Old 06-07-2011
Code:
sed 's/[  \t]*//g' inputfile
prinf("Hello");
a=a+b;

# 10  
Old 06-07-2011
Quote:
Originally Posted by kumaran_5555
You have to add -e before the first pattern too, anyway this command will not solve your problem.

Hmm alright..
Thanks anyway..

---------- Post updated at 03:05 PM ---------- Previous update was at 02:58 PM ----------

Quote:
Originally Posted by Shahul
Code:
sed 's/[  \t]*//g' inputfile
prinf("Hello");
a=a+b;


Hey thanks for replying.. But it wont work in my case..
Look if the file consists of the following

Code:
int main(void)
{
int a,b;
/* Some other code
*
*
*/
a = a+b;
printf ("hello");
}

Then even
Code:
int main()

becomes
Code:
intmain()

!!
That is it replaces all the whitespaces!
# 11  
Old 06-07-2011
So the only place you want to leave spaces is when they appear in quotes?
Code:
~/$ cat data.txt 
a =  1
b = 2
a= a + b
printf ( "Hello $a\n"  )
~/$ cat stripSpaces.pl
#/usr/bin/perl
open (my $script ,'<', "$ARGV[0]");
while (<$script>){
   if (/["']/){
      my @matches=/((["'])[^\2]*\2|\S+)/g;
      for (0..$#matches){
         delete $matches[$_] if $matches[$_] =~ /^"$/;
      }
      print  @matches,"\n"
   }
   else{
      s/\s//g;print "$_\n";
   }
}

~/$stripSpaces.pl data.txt
a=1
b=2
a=a+b
printf("Hello $a\n")

So you need to tokenise some words but not others?
Or you only wish to run things together if there is a non word character between the word characters?
# 12  
Old 06-07-2011
Try this, I have added few opertors in the regex to match the line only with operands.

Code:
 
perl -lane '$_=~s/\s+//g if /[\+\-\*\/\%]/;print' in

# 13  
Old 06-07-2011
Did you try this ?
Quote:
Originally Posted by kumaran_5555
This is my try, in order to remove only spaces around operators, and braces, you may have to use sequence of seds

Code:
 sed -e 's/ (/(/g' -e 's/) /)/g' -e 's/ =/=/g' -e 's/= /=/g' -e  's/ +/+/g'   -e  's/+ /+/g' test_file

you can add few other operators like *,-
# 14  
Old 06-07-2011
Code:
nawk '{if($0~/\+|\=|print/) {gsub(/[  \t]/,"");print} else {print}}' filename

Thanks
Sha
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Removal of extra spaces in *.log files to allow extraction of frequencies

Our university has upgraded its version of a computational chemistry program that our group uses quite regularly. In the past we have been able to extract frequency spectra from log files that are generated. Since the upgrade, the viewing program errors out. I've been able to trace down the changes... (16 Replies)
Discussion started by: wsuchem
16 Replies

2. Shell Programming and Scripting

Array operators

Hi Lets say I have two arrays: VAR_1 = "File_A" "File_B" "File_C" "File_D" VAR_2 = "File_A" "File_D" Is there a simple command to get the difference of these list, i.e. VAR_1_2 = "File_B" "File_C" or do I have to write a script and loop through all elements and compare them one by one? ... (1 Reply)
Discussion started by: Wernfried
1 Replies

3. Shell Programming and Scripting

Help with removal of blank spaces from the second field!

Hi everyone.. I'm trying to eliminate multiple whitespaces from a file.. I must make use of shell script to eliminate whitespaces.. Take a look at the sample file 1 int main() 2 { 3 int a,b; 4 printf("Enter the values of a and b"); 5 scanf("%d%d",&a,&b); 6 if(a>b) ... (6 Replies)
Discussion started by: abk07
6 Replies

4. Shell Programming and Scripting

Help with removal of blank spaces in a file

Hello.. I have a text file. I want to remove all the blank spaces(except tab) from the file.. I tried using sed command as shown below sed 's/ //g' file1 But the problem with the above command is that it also eliminates 'tab' which is between the columns.. For example if the contents... (7 Replies)
Discussion started by: abk07
7 Replies

5. Homework & Coursework Questions

Operators

I really don't know the meaning of these operators. Could someone explain the meanings? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

6. Shell Programming and Scripting

Operators

I really don't know the meaning of these operators. Could someone explain the meanings so I can make my test for today? <, <=, ==, !=, >=, >, ||, &&, ! ~ , !~ Thanks! (1 Reply)
Discussion started by: Erjen
1 Replies

7. UNIX for Dummies Questions & Answers

selective removal of blank spaces in string

Hi, I'm a newbie to shell scripting and I have the following problem: I need all spaces between two letters or a letter and a number exchanged for an underscore, but all spaces between a letter and other characters need to remain. Searching forums didn't help... One example for clarity: ... (3 Replies)
Discussion started by: Cpt_Cell
3 Replies

8. UNIX for Dummies Questions & Answers

Operators

I am trying to understand Does the following: {tmp+=$10} Mean take $10 and add them all up and call it tmp thanks! (2 Replies)
Discussion started by: llsmr777
2 Replies

9. UNIX for Dummies Questions & Answers

Arithmetic Operators

Hello, I have a list of 'inputs' and i want to convert those on the second list named 'Desired Outputs', but i don't know how to do it? Inputs Desired Outputs 1 2 94 4 276 8 369 10 464 12 ... (0 Replies)
Discussion started by: filda
0 Replies
Login or Register to Ask a Question