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
# 15  
Old 06-07-2011
Quote:
Originally Posted by kumaran_5555
Did you try this ?
Yes I tried it.. But it becomes highly difficult if I try to include each and every operator.. Isn't it??
I mean as the number of operators increases the code becomes huge..
# 16  
Old 06-07-2011
Making bit more flexible..
HOpe this should work for different scenarios...
Code:
nawk '{if($0~/\+|\=|print|\*\(\-\%/) {gsub(/[  \t]/,"");print} else {print}}' /tmp/test.t

# 17  
Old 06-07-2011
Guys guys consider this example..
Code:
void main() 
{ 
float radius, area; 
clrscr(); 
printf ("Enter the radius of a circle\n"); 
scanf ("%f", &radius); 
area = PI * pow (radius,2); 
printf ("Area of a circle = %5.2f\n", area); 
}

I wanted something like this

Code:
void main() 
{ 
float radius, area; 
clrscr(); 
printf("Enter the radius of a circle\n"); 
scanf("%f", &radius); 
area=PI*pow(radius,2); 
printf("Area of a circle = %5.2f\n", area); 
}

Observe line numbers 5, 6, 7 and 8..
Hope you understood my problem.. Smilie
# 18  
Old 06-07-2011
Try this

Code:
 
perl -lne '$_=~s/\s+//g if /(\w+\s+)\=\s+(.+)/ && !/print/;print ' in

# 19  
Old 06-07-2011
Code:
sed '/()/!s/ (/(/;s/ +/+/;s/ = /=/;s/ \* /*/g' filename

# 20  
Old 06-07-2011
Never used it myself, but perhaps this will do the job: Indent - GNU Project - Free Software Foundation

Any script short of the language interpreter/compiler is going to be imperfect, so I'd be wary of running any such formatting tool on a critical codebase. The solutions suggested in this thread are particularly susceptible to error (no offense, guys Smilie), though depending on your needs some sed/perl voodoo may be sufficient.

Regards,
Alister
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