Shell Script to remove spaces while writing to the file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Shell Script to remove spaces while writing to the file
# 1  
Old 11-20-2009
Question Shell Script to remove spaces while writing to the file

Hello Folks,
I want to get the results from a SQL query which needs to be exported to a .txt file.
My Script is something like

Code:
#!/bin/ksh
db2 connect to DATABASE user user_name using pwd;
touch test.txt
isResult=0;
isResult= `db2 -x select 'ABC',COL_B from TABLE_A WHERE COL_B=CONDITION`
print "$isResult" >> test.txt
echo Done

So in the test.txt I am getting the output as
ABC_COLBDATA
ABC_COLBDATA
ABC_COLBDATA
ABC_COLBDATA

WhiteSpace is marked as _
My question is that How can I remove the space between each column. While writing on to the output file,Space is automically getting populated between each column. Please help me on this. Thanks in advance!!
# 2  
Old 11-20-2009
Code:
| tr -d ' '

Code:
| tr ' ' ''

# 3  
Old 11-20-2009
Thanks Chakra!! but i am a new bee.. could you please tell me where to use this part of code in my above script
# 4  
Old 11-20-2009
try this: notice there is a space in between ' single quote

Code:
print "$isResult" | tr -d ' ' >> test.txt

# 5  
Old 11-20-2009
awk should work as well:

Code:
print "$isResult" | awk '{ print $1 $2 }' >> test.txt

# 6  
Old 11-20-2009
The following too will work -

Code:
tr -d ' ' < test.txt > newtest.txt

# 7  
Old 11-20-2009
Thanks a lot guys!!!!

---------- Post updated at 04:24 AM ---------- Previous update was at 04:08 AM ----------

The issue of single space between each column is resolved.
But there is one more catch here. There is column value which contains eight blank spaces which needs to be printed.

Something like this..

ABC_COLBDATA---------COLCDATA
ABC_COLBDATA---------COLCDATA
ABC_COLBDATA---------COLCDATA

_ is blankspace between the columns

- is the null values from the table data.
Hope you understand my explanation

---------- Post updated at 04:31 AM ---------- Previous update was at 04:24 AM ----------

got it guys!!!
Code:
awk '{print $1 $2 "        " $3 $4}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to remove leading and trailing spaces for variable in shell script?

Hi I have variable named tablename. The value to tablename variable has leading and trailing white spaces. How to remove the leading and training white spaces and write the value of the tablename without space to a file using shell script. ( for e.g. tablename= yyy ) INPUT ... (10 Replies)
Discussion started by: pottic
10 Replies

2. Shell Programming and Scripting

Script to remove spaces and mv a file

I've tried various solutions to move a file name with spaces and nothing seems to work. I need to take a date as input, prepend it to a filename with spaces then remove the spaces and mv the file to the new name. #!/bin/ksh # if (( $# != 1 )) then echo "Usage: `basename $0` <DATE> " ... (5 Replies)
Discussion started by: w_s_s
5 Replies

3. Programming

Writing a UNIX shell script to call a C function and redirecting data to a .txt file

Hi, I am complete new to C programming and shell scripting. I just wrote a simple C code to calculate integral using trapezoid rule. I am prompting user to pass me No. of equally spaced points , N , upper and lower limit. My code looks as follows so far: #include<stdio.h> #include<string.h>... (2 Replies)
Discussion started by: bjhjh
2 Replies

4. Shell Programming and Scripting

Sqlplus inside shell script writing to tmp file

Hi, facing an issue while calling sqlplus inside shell script. It for some reason goes to tmp file to write something and i get error as permission denied as i dont have access there. ANy idea why sqlplus writes in /tmp and how to change or stop this ? (2 Replies)
Discussion started by: rushikeshs
2 Replies

5. Shell Programming and Scripting

Sybase isql writing spaces to file

I'm running isql in sybase on a Linux box to read a table, and write the contents to a file. It needs to be a nice comma delimited file for input into another system. It reads the DB fine and writes the file. However, I have two issues. 1) I don't want the line width to be limited in any... (2 Replies)
Discussion started by: Cynthia
2 Replies

6. Shell Programming and Scripting

Problem with writing into a file through shell script

Hi all, I have a shell script which I use to login to the server from the client and then from the server I run a bunch of other scripts to complete my task. I am having problems with the script below- #!/bin/bash while read line do connections=`echo $line | cut -d " " -f 1` period=`echo... (3 Replies)
Discussion started by: joydeep4u
3 Replies

7. Shell Programming and Scripting

Help with writing shell script file

I am trying to prompt the user using tput command to read the information ( 5 last names, first names and grades) from the keyboard. Save the data in a file called student.txt. Sort the file by last name and display it on the screen My pseudocode is as follow: Pseudocode: Initialize... (1 Reply)
Discussion started by: jestaton
1 Replies

8. Shell Programming and Scripting

Shell script for a writing the directory structure to a file

Hi All, I am new user of shell scripting has come up with a problem. that I have a directory structure like : Home | |--------A | |----trunk | |-------A_0_1/ | | | |-------A_0_2/ | |--------B | ... (6 Replies)
Discussion started by: bhaskar_m
6 Replies

9. Shell Programming and Scripting

Dealing with spaces in file names in a shell script

Hi, What's the best way to find all files under a directory - including ones with space - in order to apply a command to each of them. For instance I want get a list of files under a directory and generate a checksum for each file. Here's the csh script: #!/bin/csh set files = `find $1... (5 Replies)
Discussion started by: same1290
5 Replies

10. UNIX for Dummies Questions & Answers

HELP! writing shell script with c++ file

how would i write a shell script to count number of one-line comments in a c++ file. please help with coding thank you. (1 Reply)
Discussion started by: deadleg
1 Replies
Login or Register to Ask a Question