How to remove spaces between the columns in UNIX script?.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove spaces between the columns in UNIX script?.
# 1  
Old 03-01-2013
How to remove spaces between the columns in UNIX script?.

Hi guru's,

I am trying to write a script to generate a csv file by connecting to database run a query and put the values into csv file.

But the problem i face is i am getting lot of space after one value.how can i remove those values?.

Please help.

#!/bin/bash
export comtdir=/opt/DataMig/data/name
export logdir=/opt/DataMig/data/name
export datadir=/opt/DataMig/data/name
export migpassword=tiger 
 
sqlplus -s scott$HOST_CONNECT_STR/$migpassword <<EOF > ${logdir}/taxdetails.txt
 
set linesize 500
set feedback off
set heading off


select trim(A),trim(B),trim(C),trim(D),trim(E) from xyz
where rownum<4
order by B;
 
EOF
 
tr -s ' '<taxdetails.txt | tr ' ' ',' >taxdetails.csv




here first i am trying to create a txt file and convert it into csv file.

what i need to get:

disc,334567,11756,raj,W2

What i am getting:

disc ,334567 ,11756 ,raj W2

Last edited by karingulanagara; 03-01-2013 at 12:14 AM.. Reason: to show data clearly
# 2  
Old 03-01-2013
Code:
sed 's/ *,/,/g' file.csv

# 3  
Old 03-01-2013
There is no need for these additional steps to convert to CSV. Simply set the column separator to comma , before running your SQL:
Code:
set colsep ,

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. 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

2. UNIX for Beginners Questions & Answers

How do I remove leading spaces in UNIX when count of space character is not fixed? Example below-

Script showStreamsGLIS$reg.$env.ksh gives me output as below- Job Stime Etime Status ExitCode GLIS-AS-S-EFL-LOCK-B ----- ----- OI 103313880/0 GLIS-ALL-Q-EOD-FX-UPDT-1730-B ----- ----- TE 0/0 GLIS-TK-S-BWSOD-B ... (8 Replies)
Discussion started by: Tanu
8 Replies

3. Shell Programming and Scripting

Remove spaces / tabs from variable in script

I want to remove extra spaces from variable in aix script. We retrieve the data from oracle database and then print the values. We have a value on 90th position. When we execute the query on sqlplus it shows the length of 90th position as 3, but when we use the same query in aix script it shows... (5 Replies)
Discussion started by: lodhi1978
5 Replies

4. Shell Programming and Scripting

Unix remove white spaces/tabs before & after pattern

Hi All, I wanted to know is there any way we can remove white spaces/tabs before & after some pattern { eg. before & after "," }. Please find below sample data below, Sat Jul 23 16:10:03 EDT 2011 , 12345678 , PROD , xyz_2345677 , testuuyt , ... (3 Replies)
Discussion started by: gr8_usk
3 Replies

5. Shell Programming and Scripting

Merge 2 columns/remove specific spaces

Hi, I have a requirement to remove certain spaces from a table of information, but I'm unsure where to start. A typical table will be like this: ABCDE 1 Elton John 25 12 15 9 3 ABCDE 2 Oasis 29 13 4 6 9 ABCDE 3 The Rolling Stones 55 19 3 8 6The goal is to remove only the spaces between... (11 Replies)
Discussion started by: danhodges99
11 Replies

6. Shell Programming and Scripting

How to remove white spaces from the beginning an end of a string in unix?

Suppose, I have a variable var=" name is ". I want to remove the blank spaces from the begining and endonly, not from the entire string. So, that the variable/string looks like following var="name is". Please look after the issue. (3 Replies)
Discussion started by: mady135
3 Replies

7. Shell Programming and Scripting

Single command for add 2 columns and remove 2 columns in unix/performance tuning

Hi all, I have created a script which adding two columns and removing two columns for all files. Filename: Cust_information_1200_201010.txt Source Data: "1","Cust information","123","106001","street","1-203 high street" "1","Cust information","124","105001","street","1-203 high street" ... (0 Replies)
Discussion started by: onesuri
0 Replies

8. Shell Programming and Scripting

Remove spaces from columns

I have a file with only one field something like this : 213.45 220.65 240.47 345.67 320.45 I want to remove all spaces in between. Is there any unix command for that ?Thanks in advance.. (2 Replies)
Discussion started by: jacks
2 Replies

9. UNIX for Dummies Questions & Answers

To remove Continous blank spaces from a file in UNIX

All... I want to remove blank spaces in file . I just leraned that we can use " cat <Input filename> | tr -s ‘ ‘ > <Target file name> " i also know with SED we can replace a blank space by other character by sed s/ /*/g filename. Please let me know how can i do that by... (1 Reply)
Discussion started by: arunkumar_mca
1 Replies

10. UNIX for Dummies Questions & Answers

To remove the extra spaces in unix

Hi... I am quite new to Unix and would like an issue to be resolved. I have a file in the format below; 4,Reclaim,ECXTEST02,abc123,Harry Potter,5432 6730 0327 5469,0603,,MC,,1200,EUR,sho-001,,1,,,abc123,1223 I would like my output to be as follows; 4,Reclaim,ECXTEST02,abc123,Harry... (4 Replies)
Discussion started by: Sho
4 Replies
Login or Register to Ask a Question