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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Linux Commands needed for replacing variable number of spaces with a single , in a txt file
# 1  
Old 04-23-2018
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.SmilieSmilie

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:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 04-23-2018 at 06:47 AM.. Reason: Added CODE tags.
# 2  
Old 04-23-2018
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.
# 3  
Old 04-23-2018
No trailing ","s ?
Code:
awk '$1=$1' OFS=, input_file.txt

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

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

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... (3 Replies)
Discussion started by: sparks
3 Replies

2. Shell Programming and Scripting

Replacing multiple spaces in flat file

Greetings all I have a delimited text file (the delimiter is ';') where certain fields consist of many blanks e.g. ; ; and ; ; Before I separate the data I need to eliminate these blanks altogether. I tried the sed command using the following syntax: sed -i 's/; *;/;;/g' <filename> ... (15 Replies)
Discussion started by: S. BASU
15 Replies

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

4. Shell Programming and Scripting

sed -Replacing file path within .txt file

Hi, I am trying to use sed to replace a file path within all the .lay (.txt) files in a folder. I feel that this should be easy but I can't get it to work no matter what i try. I'm using cygwin. For a .txt file containing the below line I want to replace this file path with a new one. ... (1 Reply)
Discussion started by: carlr
1 Replies

5. Shell Programming and Scripting

Replacing line number w/spaces in DSDT compiler log

Hi, all, I'm from the 8-bit micro days (Z-80, 6809, 6502, etc.) and used to program in assembly and machine code. But, that was 25 years ago and life happened. Now, I'm scripting for the hackintosh community and love every bit of it. I'm parsing a DSDT compiler log for error/warning/remarks... (0 Replies)
Discussion started by: digital_dreamer
0 Replies

6. Shell Programming and Scripting

Deleting lines that contain spaces in a txt file

I need some help deleting lines in a file that contain spaces. Im sure awk or sed will work but i dont know much about those commands. Any help is appreciated :D (7 Replies)
Discussion started by: r04dw4rri0r
7 Replies

7. Shell Programming and Scripting

Join in a single line variable number of lines

Hi all, I have a file with little blocks beginning with a number 761XXXXXX, and 0, 1, 2 or 3 lines below of it beginning with STUS as follow: 761625820 STUS ACTIVE 16778294 STUS NOT ACTIVE 761157389 STUS ACTIVE 16778294 761554921 STUS ACTIVE 16778294 STUS NOT ACTIVE STUS ACTIVE OP... (4 Replies)
Discussion started by: cgkmal
4 Replies

8. Shell Programming and Scripting

replacing spaces with null or 0 in the input file

hi i have records in my input file like this aaa|1234||2bc||rahul|tamilnadu bba|2234||b4c||bajaj|tamilnadu what i am expecting is in between two pipes if there is no character it should be replaced with null or 0 so my file will look like this aaa|1234|null|2bc|0|rahul|tamilnadu... (4 Replies)
Discussion started by: trichyselva
4 Replies

9. Shell Programming and Scripting

need help in replacing spaces in a file

hi all this is the part i am facing a problem eg data: filename : tr1 + T 40 this is a sample record in that file ... the value of T can be anything, but will be a single character. i need to cut from field two, and i am using this command cut -d " " -f2 tr1 >tr3 and the o/p is ... (7 Replies)
Discussion started by: sais
7 Replies

10. Shell Programming and Scripting

replacing a number with random variable inside shell script

Hi All. I need help for the below logic. I ve a file like following input file: NopTX(5) // should be remain same as input ----Nop(@100); //1 Nop(90); //2 --Nop(80); //3 @Nop(70); //4 --@Nop(60); //5 @Nop@(@50); //6 --Nop@( 40); ... (3 Replies)
Discussion started by: user_prady
3 Replies
Login or Register to Ask a Question