×
UNIX.COM Login
Username:
Password:  
Show Password






👤


Shell Programming and Scripting

BSD, Linux, and UNIX shell scripting — Post awk, bash, csh, ksh, perl, php, python, sed, sh, shell scripts, and other shell scripting languages questions here.

Linux Commands needed for replacing variable number of spaces with a single , in a txt file

👤 Login to reply
 
Thread Tools Search this Thread Display Modes
    #1  
Old 04-23-2018
Hari Prasanth Hari Prasanth is offline
Registered User
 
Join Date: Apr 2018
Last Activity: 11 June 2018, 11:09 AM EDT
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Data Linux Commands needed for replacing variable number of spaces with a single , in a txt file

Hi I want to read a text file and replace various number of spaces between each string in to a single "," or any other character .Please let me know the command to do so. My input file is a txt file which is the output of a SQL table extract so it contains so many spaces between each column of the table.

tried this command
Code:
tr ' ' ',' < input_file.txt > output_file.txt

but it is converting eachspace into a single charecter so the output is like

Code:
name,,,,,age,,,,job,,,,,,

where each space is replaced by comma.

i just want all the spaces in between each column into a single comma like the below one

Code:
name,age,job

Please help me with a command or a set of commands to convert the output in the format i asked.


Moderator's Comments:
Linux Commands needed for replacing variable number of spaces with a single , in a txt file Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-23-2018 at 05:47 AM.. Reason: Added CODE tags.
Sponsored Links
    #2  
Old 04-23-2018
RudiC RudiC is offline Forum Staff  
Moderator
 
Join Date: Jul 2012
Last Activity: 17 July 2018, 4:35 AM EDT
Location: Aachen, Germany
Posts: 13,065
Thanks: 448
Thanked 4,012 Times in 3,689 Posts
Welcome to the forum.

The first place to look at is the data creator, i.e. your SQL query. Can it be modified to supply exactly what you need without correction / adaption downstream? My SQL has become a bit rusty, but there exist settings to influence the output like set colsep , or functions to remove spaces e.g. trim(). Does this help?

Re. your request: does your tr version offer the -s (squeeze) option?
Code:
tr -s ' ' ',' < file
name,age,job,

If not, we need to dive deeper and resort to a small script like for awk or similar.
Sponsored Links
    #3  
Old 04-23-2018
rdrtx1 rdrtx1 is offline Forum Advisor  
Registered Pusher
 
Join Date: Sep 2012
Last Activity: 31 May 2018, 9:41 AM EDT
Location: Houston, Texas, USA
Posts: 1,142
Thanks: 0
Thanked 421 Times in 398 Posts
No trailing ","s ?
Code:
awk '$1=$1' OFS=, input_file.txt

Code:
sed 's/  */,/g; s/,*$//' input_file.txt

Sponsored Links
👤 Login to reply

« Previous Thread | Next Thread »
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing Multiple spaces with a single space but excluding few regular expressions sparks Shell Programming and Scripting 3 10-07-2015 09:11 PM
Replacing double spaces with single space figaro UNIX for Dummies Questions & Answers 4 09-18-2013 03:31 PM
Replacing line number w/spaces in DSDT compiler log digital_dreamer Shell Programming and Scripting 0 08-11-2010 08:02 PM
need help in replacing spaces in a file sais Shell Programming and Scripting 7 10-06-2008 09:24 AM
replacing a number with random variable inside shell script user_prady Shell Programming and Scripting 3 09-11-2007 02:44 AM



All times are GMT -4. The time now is 05:40 AM.

Unix & Linux Forums Content Copyright©1993-2018. All Rights Reserved.