我有一个用逗号分隔的文件,其中包含字符串和列表以及看起来像这样
ffmpeg version N-91487-g1809f1c Copyright (c) 2000-2018 the FFmpeg developers
built with gcc 5.4.0 (Ubuntu 5.4.0-6ubuntu1~16.04.10) 20160609
configuration: --prefix=/usr --pkg-config-flags=--static --enable-cuda-sdk --enable-cuvid --enable-libnpp --extra-cflags=-I/usr/local/cuda/include/ --extra-ldflags=-L/usr/local/cuda/lib64/ --nvccflags='-gencode arch=compute_61,code=sm_61 -O2' --enable-gpl --enable-libass --enable-libfdk-aac --enable-libx264 --extra-libs=-lpthread --enable-libx265 --enable-nvenc --enable-nonfree
libavutil 56. 18.102 / 56. 18.102
libavcodec 58. 21.105 / 58. 21.105
libavformat 58. 17.101 / 58. 17.101
libavdevice 58. 4.101 / 58. 4.101
libavfilter 7. 26.100 / 7. 26.100
libswscale 5. 2.100 / 5. 2.100
libswresample 3. 2.100 / 3. 2.100
libpostproc 55. 2.100 / 55. 2.100
Hyper fast Audio and Video encoder
usage: ffmpeg [options] [[infile options] -i infile]... {[outfile options] outfile}...
我想要这样的东西
24.33,-30.59,42542,['2.4', '3.6', '4.4', '5.8', '6.6', '7.2','9.4', '9.4', '9.8']
24.33,-30.60,25512,['1.4', '2.6', '2.8', '3.8', '4.6', '5.2','7.4', '8', '9']
什么是最好和最简单的方法
答案 0 :(得分:1)
请您尝试以下。
awk '{gsub(/\[|\]/,"");gsub(/\047/,"");gsub(/, /,",")} 1' Input_file
或更精确(从上面的代码中减少gsub
一个):
awk '{gsub(/\[|\]|\047/,"");gsub(/, /,",")} 1' Input_file
说明: 也在此处添加说明。
awk '
{
gsub(/\[|\]|\047/,"") ##globally substituting [, ] and single dash with NULL in current line.
gsub(/, /,",") ##globally substituting comma with space with only comma in current line.
}
1 ##Mentioning 1 here to print the current line
' Input_file ##Mentioning Input_file name here.
答案 1 :(得分:1)
在单个行上进行简单替换是sed存在要做的工作:
$ sed 's/[^0-9,.-]//g' file
24.33,-30.59,42542,2.4,3.6,4.4,5.8,6.6,7.2,9.4,9.4,9.8
24.33,-30.60,25512,1.4,2.6,2.8,3.8,4.6,5.2,7.4,8,9
但是您也可以使用tr来>
$ tr -cd '0-9,.\n-' < file
24.33,-30.59,42542,2.4,3.6,4.4,5.8,6.6,7.2,9.4,9.4,9.8
24.33,-30.60,25512,1.4,2.6,2.8,3.8,4.6,5.2,7.4,8,9
如果您坚持要求awk,那就是:
$ awk '{gsub(/[^0-9,.-]/,"")}1' file
24.33,-30.59,42542,2.4,3.6,4.4,5.8,6.6,7.2,9.4,9.4,9.8
24.33,-30.60,25512,1.4,2.6,2.8,3.8,4.6,5.2,7.4,8,9