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?
# 8  
Old 10-01-2014
none of them are working.

okay lets make my question very simple.
I have an excel sheet in which i just want to sed one space which is coming in the second column data.

Code:
column1        column2      column3      column4
date is here    _alia

The underscore in red color indicates the space. So now i just want to delete this one space from the entire second column.
# 9  
Old 10-01-2014
How would you propose to distinguish that space from the one(s) before? In awk default, ALL spaces are the field separators, so they would be gone anyhow. In sed, there's no fields, so you would need to know the space count in order to remove that one...
# 10  
Old 10-01-2014
Quote:
Originally Posted by RudiC
Leaving out the code tags you hid all those multiple spaces from us thus leading to inappropriate solutions. With that new info, try
Code:
sed -r 's/ +/ /g; s/DATA //p; 1s/^[^ ]* //p; 2!d' file1
Date Name Age State
------------------------------------------------------------------------------------
12 Sep 2014 14:00 EST Alia 22 Chennai
12 Sep 2014 14:00 EST Ashika 22 Chennai
12 Sep 2014 14:00 EST Bosky 22 Chennai
12 Sep 2014 14:00 EST Swati 22 Chennai

It gives an error to me saying sed -r is not recognized

---------- Post updated at 08:03 AM ---------- Previous update was at 08:02 AM ----------

Quote:
Originally Posted by RudiC
How would you propose to distinguish that space from the one(s) before? In awk default, ALL spaces are the field separators, so they would be gone anyhow. In sed, there's no fields, so you would need to know the space count in order to remove that one...
The space count is one for that column which i want to remove
# 11  
Old 10-01-2014
Try
Code:
sed 's/  */ /g; s/DATA //p; 1s/^[^ ]* //p; 2!d' file

# 12  
Old 10-01-2014
Ok so its very simple i have a text file okay which has four columns 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

and from this file i want to remove that one space which is coming in name and move it to another file which is an excel sheet so that it comes 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

---------- Post updated at 10:23 AM ---------- Previous update was at 08:11 AM ----------

can we do this?? please reply i need help with this Smilie Smilie
# 13  
Old 10-01-2014
First, let me be very clear: If you have a text file, feeding it through awk, grep, sed, and/or tr is not going to magically convert that text file into the proprietary format of a Microsoft Excel Spreadsheet!

If you have the text file you have shown us in post #12 in this thread, you can create the output you say you want in that post as a text file using the command:
Code:
sed '3,$s/  / /' file.txt

# 14  
Old 10-06-2014
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

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