Trim spaces


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Trim spaces
# 1  
Old 09-14-2012
Trim spaces

All,

i am comparing the output of one command to a numberic

Code:
if [[ `cat $ORATAB | grep ^+ASM | wc -l` = 1 ]]

but my problem is the output of follwoing is but but has some leading columns. I don't have any problme in LINUX and HP-UX. But only in AIX i am getting the leading spaces. I have developed my script on LINUX but when i try to run it fails because of this.

Code:
cat $ORATAB | grep ^+ASM | wc -l

o/p:
     1   -->  some leading spaces.


I am comparing like above one in many places so what will be the best option without assign the command output to a variable and compare it with another variable.

i.e Is there any way i can directly compare the o/p of command to a certain values in AIX ?

-Thanks,
# 2  
Old 09-14-2012
what is output of this..?

Code:
cat $ORATAB | grep ^+ASM

# 3  
Old 09-14-2012
Code:
cat $ORATAB | grep ^+ASM

o/p:
+ASM1:/u01/app/product/11202/grid:N           # line added by Agent

I need to compare the count
# 4  
Old 09-14-2012
Code:
cat $ORATAB | grep ^+ASM | wc -l

please replace that by:
Code:
grep -c '^+ASM' $ORATAB

These 2 Users Gave Thanks to delugeag For This Post:
# 5  
Old 09-15-2012
Hi rcc50886,
You said (correctly) that you want to compare numeric values, but the test operator you're using is for string comparisons. If you had changed your original code from:
Code:
if [[ `cat $ORATAB | grep ^+ASM | wc -l` = 1 ]]

to:
Code:
if [[ `cat $ORATAB | grep ^+ASM | wc -l` -eq 1 ]]

it would have done what you wanted.

As delugeag suggested:
Code:
if [[ `grep -c '^+ASM' $ORATAB` -eq 1 ]]

would be much more efficient.

It doesn't make any difference in this example, but quoting rules can get tricky inside `...`; using $( ... ) instead will frequently make your job much easier.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Trim trailing spaces from file

I have a file like this. hari,corporationbank,2234356,syndicate ravi,indian bank,4567900000000,indianbank,accese raju,statebank of hyderabad,565866666666666,pause Here each record has different record length and there are blank spaces... (8 Replies)
Discussion started by: kshari8888
8 Replies

2. Shell Programming and Scripting

trim spaces in unix for variable

HI Guys I have written a script using awk to split a file based on some identifier and renaming the file based on two values from specific length. ts a fixed width file. When I am trying to fetch the values a = substr($0,11,10) b = substr($0,21,5); i am getting spaces in a and b values .... (6 Replies)
Discussion started by: manish8484
6 Replies

3. Shell Programming and Scripting

trim spaces and replacing value

Hi, I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor ~? ~ ~783.9 .35~ 765.76~ anh reid~ kelly woodburg ~nancy ~ ~? Now each row in the file has value for 7 columns with "~" as delimiter. The requirement was i)I need to erase the blank spaces between... (2 Replies)
Discussion started by: badrimohanty
2 Replies

4. Shell Programming and Scripting

trim spaces in a file

Hi, I'm new to shell programming. Need some help in the following requirement: I have a file origFile.txt with values: origFile.txt .00~ 145416.02~ xyz~ ram kishor .35~ 765.76~ anh reid~ kishna kerry Now each row in the file has value for 4 columns with "~" as... (7 Replies)
Discussion started by: badrimohanty
7 Replies

5. UNIX for Dummies Questions & Answers

how to append spaces(say 10 spaces) at the end of each line based on the length of th

Hi, I have a problem where I need to append few spaces(say 10 spaces) for each line in a file whose length is say(100 chars) and others leave as it is. I tried to find the length of each line and then if the length is say 100 chars then tried to write those lines into another file and use a sed... (17 Replies)
Discussion started by: prathima
17 Replies

6. UNIX for Advanced & Expert Users

TRIM spaces in shell

am get a value like ' 15' in a variable what is the easiest method i can follow to strip 15 out (3 Replies)
Discussion started by: anumkoshy
3 Replies

7. Shell Programming and Scripting

To Trim spaces at the end of line

Hi Friends, Can any one help with this issue: How to trim spaces for each line at the end, Like I have a file in this format. EMP1 SMITH 46373 5 STREET HOWARD 74636 EMP2 JONES 5454 { these are spaces ........} EMP3 SMITH 46373 5 STREET HOWARD 74636 EMP4 JON 2554 { these are... (1 Reply)
Discussion started by: sbasetty
1 Replies

8. Shell Programming and Scripting

Trim trailing spaces from each line in a file

Hello folks, Is there a simple way to trim trailing spaces from each line a file. Please let me know. Regards, Tipsy. (5 Replies)
Discussion started by: tipsy
5 Replies

9. Shell Programming and Scripting

Trim white spaces using awk

Hi, I have a CSV file with footer information as below. The third value is the number of records in the file. Sometimes it contains both leading and trailing white spaces which i want to trim using awk. C,FOOTER , 00000642 C,FOOTER , 00000707 C, FOOTER,... (2 Replies)
Discussion started by: mona
2 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question