English 中文(简体)
替换文件中的分隔符
原标题:Replace delimiter in the file
Let s take "apple" , "tomato,potato", 100, 250, "banana" is my data I want replace the outer delimiter i.e. , which is same as inner delimiter. Ultimately, want my data like : "apple" ; "tomato,potato"; 100; 250; "banana" and the position of the string with delimeter can be anywhere in data. I have tried many chatGPT sed commands all are not working
问题回答
This might work for you (GNU sed): sed -E :a;s/^(("[^,"]*"[^"]*)*"[^,"]*),/1 /;ta;y/ ,/,;/ file First convert any commas inside double quotes to newlines. Then transliterate newlines to commas and commas to semi-colons. An alternate solution (kudos to Walter A): sed -E s/(("[^"]*"|)[^",]*),/1;/g file
This might work for you (GNU sed): sed -E :a;s/^(("[^,"]*"[^"]*)*"[^,"]*),/1 /;ta;y/ ,/,;/ file First convert any commas inside double quotes to newlines. Then transliterate newlines to commas and commas to semi-colons. An alternate solution (kudos to Walter A): sed -E s/(("[^"]*"|)[^",]*),/1;/g file
Some implementations of CSV parsers may have trouble with that "sloppy" CSV. While the spaces around commas are OK, the fact that the quotes are not immediately next to the comma isn t OK. For example, the ruby CSV module throws a MalformedCSVError "Any value after quoted field isn t allowed" $ echo "apple" , "tomato,potato", 100, 250, "banana" | ruby -rcsv -e p CSV.parse(STDIN.readline) /home/linuxbrew/.linuxbrew/Cellar/ruby/3.3.2/lib/ruby/3.3.0/csv/parser.rb:1066:in `parse_quotable_robust : Any value after quoted field isn t allowed in line 1. (CSV::MalformedCSVError) python: $ python3 Python 3.12.3 (main, Apr 9 2024, 08:09:14) [GCC 11.4.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import csv >>> line = "apple" , "tomato,potato", 100, 250, "banana" >>> for fields in csv.reader([line]): ... print(fields) ... [ apple , "tomato , potato" , 100 , 250 , "banana" ] the bash5.2 dsv module does the same thing as python: the "tomato,potato" value is not considered one field because there is a space before the opening quote, so the quotes don t have special meaning.
I wouldn t normally answer a question that shows no attempt by the OP but since there are multiple answers posted already.... Your input isn t valid CSV and so YMMV trying to use a tool that understands CSV on it. Using any awk in any shell on every Unix box you could treat the input as "-separated fields instead of ,-separated fields and replace the ,s in every odd-numbered "field": $ awk BEGIN{FS=OFS="""} {for (i=1; i<=NF; i+=2) gsub(/,/,";",$i)} 1 file "apple" ; "tomato,potato"; 100; 250; "banana" If that s not all you need then see whats-the-most-robust-way-to-efficiently-parse-csv-using-awk for ideas if you want to use awk for this.
When the input has an even number of double quotes, you can match a quoted field with ("[^"]*") and the following substring until the next , with ([^",]*). When you also want to replace delimiters between fields without quotes, add an * after the regex for a quoted field. echo "apple" , "tomato,potato", 100, 250, "banana" | sed -E s/("[^"]*")*([^",]*),/12;/g # or with a file sed -E s/("[^"]*")*([^",]*),/12;/g file Result "apple" ; "tomato,potato"; 100; 250; "banana"
You can simply replace , with ; using this sed command sed s/, /; /g . for example: echo "apple" , "tomato,potato", 100, 250, "banana" | sed s/, /; /g output: "apple" ; "tomato,potato"; 100; 250; "banana"
This is a very bad CSV, you shouldn t have the white spaces outside the double quotes. Using Miller you can do it in this way mlr --csvlite --ifs "," --ofs ";" -N cat input.csv >output.csv The output "apple" ; "tomato;potato"; 100; 250; "banana" Some notes --csvlite, to manage this wrong CSV format -N, to set that CSV has no header --ifs ",", to manage input field separator --ofs ";", to manage output field separator
The problem here is probably that you are missing the "inplace" parameter -i. sed -i -E s/((, )(([a-zA-Z0-9 ]+)|("[a-zA-Z ,0-9]+")))/; 3/g nameofthefile.csv this will substitute all occurencies of "," with ";" in the file without change them if they are inside quotes. Please change directory to the path of the file. If you want to do this replacement for multiple files csv in the same path use this command: sed -i -E s/((, )(([a-zA-Z0-9 ]+)|("[a-zA-Z ,0-9]+")))/; 3/g *.csv




相关问题
Signed executables under Linux

For security reasons, it is desirable to check the integrity of code before execution, avoiding tampered software by an attacker. So, my question is How to sign executable code and run only trusted ...

encoding of file shell script

How can I check the file encoding in a shell script? I need to know if a file is encoded in utf-8 or iso-8859-1. Thanks

How to write a Remote DataModule to run on a linux server?

i would like to know if there are any solution to do this. Does anyone? The big picture: I want to access data over the web, using my delphi thin clients. But i´would like to keep my server/service ...

How can I use exit codes to run shell scripts sequentially?

Since cruise control is full of bugs that have wasted my entire week, I have decided the existing shell scripts I have are simpler and thus better. Here is what I have so far svn update /var/www/...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

热门标签