sed delete leading spaces in a .csv if not in a string


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed delete leading spaces in a .csv if not in a string
# 1  
Old 07-06-2012
sed delete leading spaces in a .csv if not in a string

Solaris, ksh
I have a .csv file I am trying to clean up before loading into the database. The file contains comma separated columns that have leading spaces which I need to remove. The trouble is, some columns that should not be touched are strings which happen to have the same pattern in them. How do I search/replace a comma-space pattern if NOT in a string delimited by double-quotes?

Input row (null first column):
Code:
,12345,"first last, MD",Yes,<space>not in use,<space>Other

Desired output:
Code:
,12345,"first last, MD",Yes,not in use,Other

The comma-space in the third field (name) should be left alone, but the other columns should have the leading space removed.

This command of course affects the name field which I want to leave alone.
Code:
sed 's/, /,/g' x.dat

Note: the third column will be the only column that could be a string. If sed could only operate on columns > 3, that approach would work too.

Thanks for any advice!
# 2  
Old 07-06-2012
Your columns are not comma-separated.

You can kludge awk to separate on " and then replace inside...
# 3  
Old 07-06-2012
Quote:
Originally Posted by gary_w
Note: the third column will be the only column that could be a string. If sed could only operate on columns > 3, that approach would work too.
Using these assumptions, the following should work:

Code:
sed 'h;s/^\(.*"[^"]*"\).*/\1/;x;s/^\(.*"[^"]*"\)\(.*\)/\2/;s/, /,/g;x;G;s/\n//' inputfile

EDIT: I've assumed that the 3rd field has " and ".

Last edited by elixir_sinari; 07-06-2012 at 01:11 PM..
# 4  
Old 07-06-2012
Like Corona also suggests, I would use ". You could then convert to semicolon for example
Code:
awk 'NR%2{gsub(/,/,";")}1' RS=\" ORS= file

and take it from there, for example:
Code:
$ awk 'NR%2{gsub(/,/,";")} {gsub(/; */,";")}1' RS=\" ORS= file
;12345;first last, MD;Yes;not in use;Other
$

# 5  
Old 07-06-2012
Thank you all for your suggestions. I didn't make it clear enough that the third field does not necessarily have quotes.
At least this is a one-time task. I have taken steps to ensure the source trims before giving me the data the next time!
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sed, Inline replacement of string with spaces

Hello, Just surfed on the web for probable answers but could not get them working. I wish to replace the string containing spaces by another phrase but below answers did not work. My string is: PAIN & GAIN I wish to convert it to: P&G I just need it working with sed with function -i ... (6 Replies)
Discussion started by: baris35
6 Replies

2. Shell Programming and Scripting

sed searches a character string for a specified delimiter character, and returns a leading or traili

Hi, Anyone can help using SED searches a character string for a specified delimiter character, and returns a leading or trailing space/blank. Text file : "1"|"ExternalClassDEA519CF5"|"Art1" "2"|"ExternalClass563EA516C"|"Art3" "3"|"ExternalClass305ED16B8"|"Art9" ... ... ... (2 Replies)
Discussion started by: fspalero
2 Replies

3. Shell Programming and Scripting

How to delete the commas in a .CSV file that are enclosed in a string with double quotes?

Okay, I would like to delete all the commas in a .CSV file (TEST.CSV) or at least substitute them with empty space, that are enclosed in double quote. Please see the sample file as below: column 1,column 2,column 3,column 4,column 5,column 6,column 7,column 8,column 9,column 10... (8 Replies)
Discussion started by: dhruuv369
8 Replies

4. Shell Programming and Scripting

How to delete ending/trailing spaces using awk,sed,perl?

How to delete ending/trailing spaces using awk,sed,perl? Input:(each line has extra spaces at the end) 3456 565 3 7 35 878 Expected output: 3456 565 3 7 35 878 (5 Replies)
Discussion started by: cola
5 Replies

5. Shell Programming and Scripting

Using sed to replace a string in file with a string in a variable that contains spaces

Hi, i call my shell like: my_shell "my project name" my script: #!/bin/bash -vx projectname=$1 sed s/'PROJECT_NAME ='/'PROJECT_NAME = '$projectname/ <test_config_doxy >temp cp temp test_config_doxy the following error occurres: sed s/'PROJECT_NAME ... (2 Replies)
Discussion started by: vivelafete
2 Replies

6. Shell Programming and Scripting

sed: replace string with another string (with spaces)

Hi I have an XML file with strings XABCD, XEFGHX and XIJKLX. I would like to replace XABCDX with "This is the first string", XEFGHX with "This is the second string" and XIJKLX with "This is the third string". What is the best way to implement this? Should I have a file with the data that is... (4 Replies)
Discussion started by: zmfcat1
4 Replies

7. Shell Programming and Scripting

sed over writes my original file (using sed to remove leading spaces)

Hello and thx for reading this I'm using sed to remove only the leading spaces in a file bash-280R# cat foofile some text some text some text some text some text bash-280R# bash-280R# sed 's/^ *//' foofile > foofile.use bash-280R# cat foofile.use some text some text some text... (6 Replies)
Discussion started by: laser
6 Replies

8. UNIX for Dummies Questions & Answers

Read a string with leading spaces and find the length of the string

HI In my script, i am reading the input from the user and want to find the length of the string. The input may contain leading spaces. Right now, when leading spaces are there, they are not counted. Kindly help me My script is like below. I am using the ksh. #!/usr/bin/ksh echo... (2 Replies)
Discussion started by: dayamatrix
2 Replies

9. Shell Programming and Scripting

how to remove spaces in a string using sed.

Hello, I have the following to remove spaces from beginning and end of a string. infile=`echo "$infilename" | sed 's/^ *//;s/ *$//` How do I modify the above code to remove spaces from beginning, end and in the middle of the string also. ex: ... (4 Replies)
Discussion started by: radhika
4 Replies

10. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies
Login or Register to Ask a Question