Visit Our UNIX and Linux User Community


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
# 1  
Old 06-07-2011
Bug Help with removal of spaces between operators and operands

Hi I'm trying to remove blank spaces in expressions and function calls..
Consider the following example


Code:
printf ("Hello");
a = a + b;

I'm trying to eliminate space in between the function name and the opening brace. And also eliminate space between operators and operands..
That is, I'm trying to get the following output

Code:
printf("Hello");
a=a+b;

How can I do that??
Thanks in advance..
# 2  
Old 06-07-2011
Try this

Code:
sed s/' '//g temp.txt

# 3  
Old 06-07-2011
Quote:
Originally Posted by palanisvr
Code:
sed s/' '//g temp.txt

Hey no.. The command which you have given replaces all the blank spaces in the file!!
I want only in those two cases which I've mentioned..
# 4  
Old 06-07-2011
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 *,-
# 5  
Old 06-07-2011
Code:
sed 's/ [\(\=\+]//g' -e 's/[\(\=\+] //g' file

# 6  
Old 06-07-2011
Quote:
Originally Posted by itkamaraj
Code:
sed 's/ [\(\=\+]//g' -e 's/[\(\=\+] //g' file


It will also remove the chars such as ( ,= and +, but i guess this isn't the requirement.
# 7  
Old 06-07-2011
Question

Quote:
Originally Posted by itkamaraj
Code:
sed 's/ [\(\=\+]//g' -e 's/[\(\=\+] //g' file

hey I got the following message

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


Previous Thread | Next Thread
Test Your Knowledge in Computers #88
Difficulty: Easy
PowerShell makes use of pipes.
True or False?

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

Featured Tech Videos