格式化文件以删除“字符

格式化文件以删除“字符

我有一个包含以下数据的文件

"MG1507XXXXXX|" "|020000XXXXXX" "20261031|"     "|3,827.92"     "|3,581.41"     "|542,729.62"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20261130|"     "|3,680.15"     "|3,729.18"     "|539,000.44"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20261231|"     "|3,776.70"     "|3,632.63"     "|535,367.81"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270131|"     "|3,751.24"     "|3,658.09"     "|531,709.72"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270228|"     "|3,365.07"     "|4,044.26"     "|527,665.46"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270331|"     "|3,697.28"     "|3,712.05"     "|523,953.41"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270430|"     "|3,552.84"     "|3,856.49"     "|520,096.92"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270531|"     "|3,644.24"     "|3,765.09"     "|516,331.83"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270630|"     "|3,501.16"     "|3,908.17"     "|512,423.66"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270731|"     "|3,590.47"     "|3,818.86"     "|508,604.80"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270831|"     "|3,563.72"     "|3,845.61"     "|504,759.19"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20270930|"     "|3,422.68"     "|3,986.65"     "|500,772.54"   "MBA"
"MG1507XXXXXX|" "|020000XXXXXX" "20271031|"     "|3,508.84"     "|3,900.49"     "|496,872.05"   "MBA"

我多么想改变它,让它看起来像

MG1507XXXXXX|020000XXXXXX|20261031|3,827.92|3,581.41|542,729.62|MBA|
MG1507XXXXXX|020000XXXXXX|20261130|3,680.15|3,729.18|539,000.44|MBA|
MG1507XXXXXX|020000XXXXXX|20261231|3,776.70|3,632.63|535,367.81|MBA|
MG1507XXXXXX|020000XXXXXX|20270131|3,751.24|3,658.09|531,709.72|MBA|
MG1507XXXXXX|020000XXXXXX|20270228|3,365.07|4,044.26|527,665.46|MBA|
MG1507XXXXXX|020000XXXXXX|20270331|3,697.28|3,712.05|523,953.41|MBA|
MG1507XXXXXX|020000XXXXXX|20270430|3,552.84|3,856.49|520,096.92|MBA|
MG1507XXXXXX|020000XXXXXX|20270531|3,644.24|3,765.09|516,331.83|MBA|
MG1507XXXXXX|020000XXXXXX|20270630|3,501.16|3,908.17|512,423.66|MBA|
MG1507XXXXXX|020000XXXXXX|20270731|3,590.47|3,818.86|508,604.80|MBA|
MG1507XXXXXX|020000XXXXXX|20270831|3,563.72|3,845.61|504,759.19|MBA|
MG1507XXXXXX|020000XXXXXX|20270930|3,422.68|3,986.65|500,772.54|MBA|
MG1507XXXXXX|020000XXXXXX|20271031|3,508.84|3,900.49|496,872.05|MBA|

我不知道用什么来实现这一目标。有任何想法吗 ?

答案1

您可以tr将所有空格和双引号解释为|(并s挤压),然后cut从第二个字符到行尾:

tr -s '[[:blank:]"]' \| <infile | cut -c2-

答案2

假设您的数据位于名为“data”的文件中:

sed -e s'/^"//' -e 's/|" "|/|/g' -e 's/" "|/|/g' -e 's/" "/|/g' -e s'/"$/|/' data

答案3

sed -i 's/\"//g' filename

您可以"通过放置一个 来转义该字符\。如果您还想删除所有空格,请执行以下操作:

sed -i 's/[" ]//g' filename

答案4

使用awk

awk ' BEGIN { FS="[|\" ]+" ; OFS="|" } { print $2,$3,$4,$5,$6,$7,$8"|" } ' file

解释:

BEGIN { FS="[|\" ]+" ; OFS="|" }首先设置以下内容:

FS="[|\" ]+": 字段由 set ( ) 管道、双引号(需要转义)和空格+的任意组合 ( ) (零个或多个)分隔。[]|\"

OFS="|"用管道分隔输出字段。

print $2,$3,$4,$5,$6,$7,$8"|"打印第 1 列到第 8 列,并在末尾显示一个管道(请注意,由于行以双引号开头,因此它会移动一位,从而使第一个字段成为空字符串,从而移动所有其他字段的位置)。

相关内容