Finding and replacing spaces with Sed Mac Terminal

I have a .CSV file with over 500,000 lines that I need:

  • find all the "spatial double quotes" and replace them with nothing
  • find all the "spatial double quotes" and replace nothing
  • find all double quotes and replace nothing

Example .CSV line:

"DISH Hartford & New Haven  (Hartford)", "206", "FBNHD", " 06028", " East Windsor Hill", "CT", "Hartford County"

** Required Conclusion **

DISH Hartford & New Haven  (Hartford),206,FBNHD,06028,East Windsor Hill,CT,Hartford County

I need to remove all double quotes ( ") and spaces before commas and commas ( ,).

I tried

$ cd /Users/Leonna/Downloads/
$ cat bs-B2Bformat.csv | sed s/ " //g

This gives me the "incomplete" command more than a hint, so I then tried:

$ cat bs-B2Bformat.csv | sed s/ " //g
sed: 1: "s/": unterminated substitute pattern
$ cat bs-B2Bformat.csv |sed s/ \" //g
sed: 1: "s/": unterminated substitute pattern
$

Too many lines for editing in Excel (Excel does not load all lines) or even a text editor. How can i fix this?

+4
5

:

POSIX [[: space:]] \s, GNU sed.

, , , , GNU BSD-.

sed -E 's/[[:space:]]?"[[:space:]]?//g' <path/to/file>

-E BSD. GNU sed , , , BSD.

BSD sed:

-E ()        , (BRE).

,

"DISH Hartford and New Haven ()", "206", "FBNHD", "06028", "East Windsor Hill", "CT", "Hartford County"

DISH - (), 206, FBNHD, 06028, -, ,

+9

:

sed -i 's/\(\s\|\)"\(\|\s\)//g' bs-B2Bformat.csv
+1

. , ?

 sed -e 's|", "|,|g' -e 's|^"||g' -e 's|"$||g' file.csv

 echo '"DISH Hartford & New Haven (Hartford)", "206", "FBNHD", " 06028", " East Windsor Hill", "CT", "Hartford County"' | sed -e 's|", "|,|g' -e 's|^"||g' -e 's|"$||g'

 DISH Hartford & New Haven (Hartford),206,FBNHD, 06028, East Windsor Hill,CT,Hartford County
+1

- csv:

import csv 
import sys 

## Open file provided as argument.
with open(sys.argv[1], 'r') as f:

    ## Create the csv reader and writer. Avoid to quote fields in output.
    reader = csv.reader(f, skipinitialspace=True)
    writer = csv.writer(sys.stdout, quoting=csv.QUOTE_NONE, escapechar='\\')

    ## Read file line by line, remove leading and trailing white spaces and
    ## print.
    for row in reader:
        row = [field.strip() for field in row]
        writer.writerow(row)

:

python3 script.py csvfile

:

DISH Hartford & New Haven  (Hartford),206,FBNHD,06028,East Windsor Hill,CT,Hartford County
0

, , , :

$ cat bs-B2Bformat.csv | sed s/ " //g
sed: 1: "s/": unterminated substitute pattern
$ cat bs-B2Bformat.csv |sed s/ \" //g
sed: 1: "s/": unterminated substitute pattern
$

. :

$ cat bs-B2Bformat.csv | sed 's/ " //g'
                             ^        ^

Without single quotes, bash breaks into spaces and sends three separate arguments (well, at least for the case \"). sed saw his first argument as simple s/.

Edit: FYI, single quotes are not required, they just simplify this case. If you want to use double quotes, just open the one you want to keep for matching:

$ cat bs-B2Bformat.csv | sed "s/ \" //g"
0
source

Source: https://habr.com/ru/post/1502528/


All Articles