Replacing Multiple spaces with a single space but excluding few regular expressions


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing Multiple spaces with a single space but excluding few regular expressions
# 1  
Old 10-07-2015
Replacing Multiple spaces with a single space but excluding few regular expressions

Hi All.

Attached are two files.

I ran a query and have the output as in the file with name "FILEWITHFOURRECORDS.txt "
I didn't want all the spaces between the columns so I squeezed the spaces with the "tr" command and also added a carriage return at the end of every line.

But in two columns I.e., the second and third column, I have a description where it has spaces in between and it should NOT be removed. But as I apply a 'tr' on the whole file, even those spaces are removed.

Could you please help me, how to retain the spaces in that string and replace everything else.

Below is how it looks after removing spaces:

Code:
 
657715,"SvcLoc,20458,IP,Normal,N/A","SvcSeg,88FT,0.5IN,PL,DNA"
655873,"SvcLoc,24558,IP,Normal,N/A,C","SvcSeg,46FT,1IN,PL,DNA"

It should look like this:

Code:
657715,"Svc Loc , 20458, IP, Normal, N/A","Svc Seg, 88FT, 0.5IN, PL, DNA"
655873,"Svc Loc ,24558 ,IP ,Normal ,N/A ,C","SvcSeg,46FT,1IN,PL,DNA"

Any suggestion will be very helpful. Please let me know if my question is not clear!

Thank You!!
# 2  
Old 10-07-2015
There are no double quotes in FILEWITHFOURRECORDS.txt, so how did your code generate them? Without any separators/tokens/indicators, how can we tell where $2 stops and $3 starts?

---------- Post updated at 23:29 ---------- Previous update was at 23:21 ----------

or do field2 and 3 always have five comma separated elements?
# 3  
Old 10-07-2015
My bad, That result was from a query which I executed before, but the 2nd and 3rd fields does have the quotes around it.
And to your questions $2 and $3 have four or sometimes five comma separated elements
# 4  
Old 10-07-2015
Quote:
Originally Posted by sparks
My bad, That result was from a query which I executed before, but the 2nd and 3rd fields does have the quotes around it.
And to your questions $2 and $3 have four or sometimes five comma separated elements
So, your real data contains double quotes surrounding fields 2 and 3, but the sample data you've shown us contains no double quotes? And, without the quotes fields 2 and 3 sometimes contain 3 commas and sometimes contain 4 commas. So how are we supposed to guess where to add the double quotes?

I am confused.

Can you please show us the output from your query that contains the double quotes and show us the output you hope to produce from that output? (Please post samples in-line in a post (in CODE tags) if at all possible instead of uploading files that everyone who wants to help you has to download.)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

BASH - Regular Expressions :Looking for one word on multiple lines.

Im looking for a bash solution that will use Regular Expressions (not perl, sed or awk) to check the example data below and then give me a status. which would be just simply Match or Mismatch. SYS PS1 is present. Fan status: Normal Input Voltage status: Normal ... (5 Replies)
Discussion started by: popeye
5 Replies

2. UNIX for Dummies Questions & Answers

Replacing double spaces with single space

I am looking for a regular expression that uses sed to replace multiple spaces with single spaces on every line where it is not at the start of the line and not immediately before double slashes ('//') or between quotes ("). In its simplest form, it would look like this: sed -e 's# # #g'... (4 Replies)
Discussion started by: figaro
4 Replies

3. Shell Programming and Scripting

Replacing trailing space with single quote

Platform : RHEL 5.8 I want to end each line of this file with a single quote. $ cat hello.txt blueskies minnie mickey gravity snoopyAt VI editor's command mode, I have used the following command to replace the last character with a single quote. ~ ~ ~ :%s/$/'/gNow, the lines in the... (10 Replies)
Discussion started by: John K
10 Replies

4. UNIX for Dummies Questions & Answers

How to translate multiple spaces into a single space using tr command?

I am trying to read a txt file and trying to translate multiples spaces into single spaces so the file is more organized, but whenever I try the command: tr ' ' ' ' w.txt The output is: tr: extra operand `w.txt' Try `tr --help' for more information. Can someone please help? :wall: ... (2 Replies)
Discussion started by: Nonito84
2 Replies

5. UNIX for Advanced & Expert Users

What is the difference between single line mode and multiline mode in Regular expressions?

Hi All, Can please let me know what is the difference between the single line mode and multi line mode in regular expresions? Thanks, Chidhambaram B (3 Replies)
Discussion started by: chidhu.anu
3 Replies

6. Shell Programming and Scripting

How to avoid the truncating of multiple spaces into a single space while reading a line from a file?

consider the small piece of code while read line do echo $line done < example content of example file sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the output is like sadasdasdasdsa erwerewrwr ergdgdfgf rgerg erwererwr the... (4 Replies)
Discussion started by: Kesavan
4 Replies

7. UNIX for Dummies Questions & Answers

Regular Expressions -- Find spaces outside

Hello, I need help with using grep and regular expressions.... I have a long list of about 1000 lines of Chinese flashcards. Here's a small excerpt: 意文 yìwén (given name) 貴姓 guìxìng (honorable surname) 貴 guì (honorable) 姓 xìng (one's surname is; to be surnamed; surname) 呢 ne (interrogative... (2 Replies)
Discussion started by: arduino411
2 Replies

8. Shell Programming and Scripting

replace space or spaces in a line of a file with a single :

I am searching while I await a response to this so if it has been asked already I apologize. I have a file with lines in it that look like: bob johnson email@email.org I need it to look like: bob:johnson:email@email.org I am trying to use sed like this: sed -e 's/ /:/g' file >... (5 Replies)
Discussion started by: NewSolarisAdmin
5 Replies

9. Shell Programming and Scripting

Consecutive spaces within input being converted to single space

I'm reading from a file that is semi-colon delimited. One of the fields contains 2 spaces separating the first and last name (4th field in - "JOHN<space><space> DOE"): e.g. TORONTO;ONTARIO;1 YONGE STREET;JOHN DOE;CANADA When I read this record and either echo/print to screen or write to... (4 Replies)
Discussion started by: NinersFan
4 Replies

10. Shell Programming and Scripting

replacing single space in argument

I want to write a script which will check the arguments and if there is a single space(if 2 more more space in a row , then do not touch), replace it with _ and then gather the argument so, program will be ran ./programname hi hello hi usa now hello hello so, inside of program,... (7 Replies)
Discussion started by: convenientstore
7 Replies
Login or Register to Ask a Question