How to remove spaces from a file selectively?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to remove spaces from a file selectively?
# 15  
Old 10-06-2014
Quote:
Originally Posted by Sharma331
Hi Don,

sorry for the confusion and the mess created by me. Smilie
I have only the xls file which has this data
Code:
column1          column2     column3      column4

16 jan 1994       _alia1        22        chennai 
17 jan 1994       _alia2        22        chennai 
18 jan 1994       _alia3        22        chennai 
19 jan 1994       _alia4        22        chennai

Underscores means spaces. so there is only one leading space before the data in column 2 and the position of the space is shown above.
and i want to remove space from the second column to make it look like below:
Code:
column1          column2     column3      column4

16 jan 1994       alia1        22        chennai 
17 jan 1994       alia2        22        chennai 
18 jan 1994       alia3        22        chennai 
19 jan 1994       alia4        22        chennai

NO! What you have shown us is a text file not a binary Microsoft Excel spreadsheet file. An underscore and a space are very different characters. If you are saying that you have converted a space to an underscore to indicate which one of the nine spaces between 1994 and alia is to be deleted; I say it doesn't matter whether you delete the 2nd of those nine spaces (which the sed script I provided does) or the 9th of those nine spaces. Either way, you get the output you requested (as long as the input you showed us above is an accurate representation of the text file you have as an input file). Did you try the sed command I suggested, or have you determined by looking at it that it won't do what you want?

Please try it before deciding it won't work. The sed script I suggested produces exactly the output you said you want when I run it!
# 16  
Old 10-06-2014
Quote:
Originally Posted by Don Cragun
NO! What you have shown us is a text file not a binary Microsoft Excel spreadsheet file. An underscore and a space are very different characters. If you are saying that you have converted a space to an underscore to indicate which one of the nine spaces between 1994 and alia is to be deleted; I say it doesn't matter whether you delete the 2nd of those nine spaces (which the sed script I provided does) or the 9th of those nine spaces. Either way, you get the output you requested (as long as the input you showed us above is an accurate representation of the text file you have as an input file). Did you try the sed command I suggested, or have you determined by looking at it that it won't do what you want?

Please try it before deciding it won't work. The sed script I suggested produces exactly the output you said you want when I run it!
I tried your code it doesn't work for me.. Smilie
and i have a excel file with .xls extension. I am doing all the data processing in text file and finally transferring it to the xls file.

the xls file has that space.

Underscore is used to show the space its not the actual underscore.. I have used it to show the space,the leading space.
# 17  
Old 10-06-2014
Again (see post#9): How would you propose to distinguish that space from the one(s) before? Are those spaces or <TAB>s?

If it is a REAL .xls file (which is hard to believe reading all the posts), we cannot work on it with text tools. You need to run EXCEL and remove that column's leading spaces (or maybe the perl excel module?).
# 18  
Old 10-06-2014
Quote:
Originally Posted by RudiC
Again (see post#9): How would you propose to distinguish that space from the one(s) before? Are those spaces or <TAB>s?

If it is a REAL .xls file (which is hard to believe reading all the posts), we cannot work on it with text tools. You need to run EXCEL and remove that column's leading spaces (or maybe the perl excel module?).
You are correct i was talking about the one leading space inside the xls file.

---------- Post updated at 07:33 AM ---------- Previous update was at 07:18 AM ----------

my actual code is below:

Code:
#!/bin/ksh


tempsql=$EXPRS_ROOT/logs/temp1.txt
tempsql1=$EXPRS_ROOT/logs/temp2.txt
outputFile=$EXPRS_ROOT/logs/temp3.xls
outputFileName="RT Claim Dashboard Usage Report Cumulative.xls"

grep DATA $tempsql >> $tempsql1

sed 's/DATA//' $tempsql1 >> $outputFile

uuencode $outputFile "$outputFileName"  | mail -s "Report Cumulative" "alia@gmail.com"

exit 0

# 19  
Old 10-06-2014
Just calling a file "something.xls" does not make it a (binary) EXCEL file. What you provided above is producing text files, and in those you can't distinguish one space from the other, and removing the first of nine is as good as removing the last (as Don Cragun said). If his proposal doesn't fit your needs, I'm afraid you're out of luck.
# 20  
Old 10-06-2014
thanks.. Smilie
But it do send me a mail with a proper xls file with four columns.
# 21  
Old 10-07-2014
Quote:
Originally Posted by Sharma331
thanks.. Smilie
But it do send me a mail with a proper xls file with four columns.
I suggest you to save the file in .csv format first. And all the rest I think you can figure it out Smilie
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. Shell Programming and Scripting

Remove spaces from the file

Hi All, The output file contains data as below. "20141023","CUSTOMER" ,"COMPANY" ,"IN0515461" ,"" ,"JOSHUA" There are spaces in between the ending " and ,. The number of spaces is random. How can I remove that from the file so that the final output is:... (4 Replies)
Discussion started by: aarsh.dave
4 Replies

3. Shell Programming and Scripting

Remove spaces in a file

Hi friends, I have a file1.txt 1 | a | 4757634 | jund jdkj | erhyj 2 | a | 4757634 | jnd jdkj | rhje hjrhwj i have used tr -d '\040' to remove the spcaes output file cat file1.txt | tr -d '\040' 1|a|4757634|jundjdkj|erhyj... (5 Replies)
Discussion started by: i150371485
5 Replies

4. Shell Programming and Scripting

How to remove all blank spaces in a file

I have a file which contains data such as that shown below. How do i remove all the blcnak spaces, before, during and at the end of each line in one command? 300015, 58.0823212, 230.424728 300016, 58.2276459, 229.141602 300017, 58.7590027, 226.960846 ... (9 Replies)
Discussion started by: carlr
9 Replies

5. Linux

How to remove spaces in the file?

hiii i have a file that contains spaces in the begining of a file till the middle the from there the txt would appear. hw can i remove those spaces and bring the text to the begining portion file1 text starts from here (12 Replies)
Discussion started by: anurupa777
12 Replies

6. Shell Programming and Scripting

Remove EOL selectively

Hi, I have a text as below test1 test2 test3\ test4 test5 test6 test7 newtest1 newtest2\ newtest3 newtest4 newtest5 And need this to be replaces to test1 test2 test3 test4 test5 test6 test7 newtest1 newtest2 newtest3 newtest4 newtest5 So my requirement is to remove the EOL... (5 Replies)
Discussion started by: praveenbvarrier
5 Replies

7. UNIX for Dummies Questions & Answers

remove spaces in between file

hey, I have this file: ATOM 2510 HG12 VAL 160 8.462 15.861 1.637 ATOM 2511 HG13 VAL 160 9.152 14.510 0.725 ATOM 2512 CG2 VAL 160 6.506 16.579 -0.088 ATOM 2513 HG21 VAL 160 5.499 16.421 -0.478 ATOM 2514 HG22 VAL 160 6.417 16.984 ... (4 Replies)
Discussion started by: kanikasharma
4 Replies

8. Shell Programming and Scripting

how to remove blank spaces in file

hi i have a file which store some data.the contents of my file is data1:data2 data3:data4 i have a script which read this file correct="$(cat /root/sh | cut -d: -f1)" i used this syntax..please help me which syntax is used to remove blank spaces..then how to read this file.. (1 Reply)
Discussion started by: shubhig15
1 Replies

9. Shell Programming and Scripting

Remove trailing spaces from file

I'm currently writing my sql results to a file and they have trailing spaces after each field. I want to get rid of these spaces and I'm using this code: TVXTEMP=$(echo $TVXTEMP|sed -e 's/\ //g') It doesn't work though. I'm not familiar with sedscript, and the other codes I've found online... (6 Replies)
Discussion started by: avillanueva
6 Replies

10. Shell Programming and Scripting

How can i remove spaces in between the fields in a file

Hey , I have a file and it's having spaces for some of the fields in it. Like the one below. I want to remove the spaces in them through out the file. The spaces occur randomly and i can't say which field is having space. So please help. Here is sample file with spaces after 5th field. (3 Replies)
Discussion started by: dsravan
3 Replies
Login or Register to Ask a Question