gpt4 book ai didi

linux - 反向函数通过 shell 读取应用 csv 中的特定列

转载 作者:太空宇宙 更新时间:2023-11-04 10:07:15 26 4
gpt4 key购买 nike

如何反转 csv 中第 10 列数据的特定条件。输入文件:

RFIN0010|TD|379410|3261664|44753406|CAD|MC|SALE|S|4|8275.08|USD|0.76644909
RFIN0010|TD|379410|3261666|44753408|CAD||IA|S||-335.81|USD|
RFIN0010|TD|379410|3261666|44753408|CAD||PFEE|S||-15.31|USD|
HFIN0010T|362051|11/01/2017|11/01/2017|11/02/2017|08:55:43
RFIN0011|PFEE|DEP|TD|379410|3261666|44753408|USD|27366020|MC||"Fee"|S|4|0.0000|6342.43|0.000500|-3.18|CAD
HACT0010|362051|11/01/2017|11/01/2017|11/02/2017|08:48:41
RACT0010|11/01/2017|871889|"online3"|"1238"|12152|TD|349239|USD|"abc87905"||xxx01|06/20|12.00|VI|DP|10/31/2017|88450G|100||||7311||||||V383|N|.1|.0225|-.37|-.02||USD|""|

我要 RACT0010 第 10 列数据反转

输出:

 RFIN0010|TD|379410|3261664|44753406|CAD|MC|SALE|S|4|8275.08|USD|0.76644909
RFIN0010|TD|379410|3261666|44753408|CAD||IA|S||-335.81|USD|
RFIN0010|TD|379410|3261666|44753408|CAD||PFEE|S||-15.31|USD|
HFIN0010T|362051|11/01/2017|11/01/2017|11/02/2017|08:55:43

RFIN0011|PFEE|DEP|TD|379410|3261666|44753408|USD|27366020|MC||"Fee"|S|4|0.0000|6342.43|0.000500|-3.18|CAD
HACT0010|362051|11/01/2017|11/01/2017|11/02/2017|08:48:41
RACT0010|11/01/2017|871889|"online3"|"1238"|12152|TD|349239|USD|"50978cba"||xxx01|06/20|12.00|VI|DP|10/31/2017|88450G|100||||7311||||||V383|N|.1|.0225|-.37|-.02||USD|""|

我的 shell :

#!/bin/bash
for j in *.dfr;
do
FILE=$j;
echo $j++ |awk '{if ($1=="RACT0010"){$10=reverse$10} else {$1=$1} print}' FS='|' OFS='|' $j> rev/$j
done;

最佳答案

您的问题的 awk 部分可以这样解决(为清楚起见添加了换行符和注释):

awk '$1 == "RACT0010" {              # use a condition, not an "if"
n = split($10, a, //) # split into array of characters
s = "" # initialise s to empty
for (i = n; i > 0; i--) s = s a[i] # build new reversed string
$10 = s # overwrite original field
}
1' FS='|' OFS='|' file # 1 is shortest true condition

将其放入循环中:

for j in *.dfr; do
awk '$1 == "RACT0010" {
n = split($10, a, //); s = ""; for (i = n; i > 0; i--) s = s a[i]; $10 = s
} 1' FS='|' OFS='|' "$j" > rev/"$j"
done

正如评论中所指出的,规范不保证将 split// 一起使用以将字符串拆分为单个字符。在 GNU awk 中它有效,但在其他情况下它可能无效。另一种反转字符串的方法是重复调用 substr:

s = ""
len = length($10)
for (i = 0; i < len; ++i) s = s substr($10, len - i, 1)

关于linux - 反向函数通过 shell 读取应用 csv 中的特定列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514782/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com