AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / user-259093

Kintaro's questions

Martin Hope
Kintaro
Asked: 2022-02-16 08:28:06 +0800 CST

按两列比较多个文件行,匹配第一个比较第二个

  • 0

我有两个像这样的输入文件:

文件 1

ABC001;text;text;5.00;text;text;;20/06/2020;http://www.domain.com/img/foobar4325.jpg
ABC002;text;text;15.20;text;text;;4/12/2021;http://www.domain.com/img/foobar545.jpg
ABC003;text;text;10.00;text;text;;24/07/2021;http://www.domain.com/img/foobar6y6.jpg
ABC004;text;text;4.90;text;text;;31/12/2021;http://www.domain.com/img/foobar5464.jpg
ABC007;text;text;10.30;text;text;;3/12/2021;http://www.domain.com/img/foobar45tgv.jpg
ABC010;text;text;9.00;text;text;;20/12/2021;http://www.domain.com/img/foobar2345f.jpg

file2(“四舍五入”价格不含 .00)

ABC001;text;text;6
ABC002;text;text;15.20
ABC003;text;text;10
ABC004;text;text;5.50
ABC005;text;text;25
ABC007;text;text;10.50
ABC010;text;text;9

所需的输出:

ABC001;text;text;5.00;text;text;;20/06/2020;http://www.domain.com/img/foobar4325.jpg
ABC004;text;text;4.90;text;text;;31/12/2021;http://www.domain.com/img/foobar5464.jpg
ABC007;text;text;10.30;text;text;;3/12/2021;http://www.domain.com/img/foobar45tgv.jpg

这些行需要匹配第一列,然后比较匹配的行“价格”列(第五),如果价格在数字上不同,我只想从 file1 中提取行。

我使用这个(GNU Awk 4.0.2):

awk -F';' -v RS='[\r\n]+' 'FNR==NR{righe[$1]; next} $1 in righe' file1.csv file2.csv > output.csv

比较两个 csv 文件,但我无法添加有条件的价格

awk file-comparison
  • 1 个回答
  • 127 Views
Martin Hope
Kintaro
Asked: 2018-11-30 04:01:42 +0800 CST

grep 使用数组值并使其更快

  • 1

array[1] 是从 30k 行 CSV 中提取的字符串:示例:

samsung black 2014

我需要将这些行与数组(arrayItems)中包含的值之一匹配。

arrayItems 包含 221 个值,例如:

apple
sony
samsung

实际脚本:

while IFS=$';' read -r -a array
do
    mapfile -t arrayItems < $itemsFile
    ## now loop through the above array
    for itemToFind in "${arrayItems[@]}"
    do
       itemFound=""
       itemFound="$(echo ${array[1]} | grep -o '^$itemToFind')"
       if [ -n "$itemFound" ] 
       then 
          echo $itemFound 
          # so end to search in case the item is found
          break
       fi
    done
   # here I do something with ${array[2]}, ${array[4]} line by line and so on, 
   # so I can't match the whole file $file_in at once but online line by line.
done < $file_in

问题是 grep 不匹配。

但如果我尝试像这样对 $itemToFind 进行硬编码:

itemFound="$(echo ${array[1]} | grep -o '^samsung')"

另一件事是......如何更快地做到这一点,因为 $file_in 是 30k 行 CSV?

bash shell-script
  • 2 个回答
  • 9298 Views
Martin Hope
Kintaro
Asked: 2018-08-11 02:04:55 +0800 CST

找不到命令在bash中为数组赋值

  • 0

如果某个索引处的数组为空,我正在尝试分配一个值,但出现command not found错误。

while IFS=$';' read -r -a array
do

# if empty set Not Available
if [[ -z ${array[6]} ]] ; then
        $array[6]="Not Available"
        echo barcode is ${array[6]}
fi

  echo '  <Product>' >> $file_out
  echo '    <Reference>'${array[0]}'</Reference>' >> $file_out
  echo '    <Name>'${array[1]}'</Name>' >> $file_out
  echo '    <Category>'${array[2]}'</Category>' >> $file_out
  echo '    <Price>'${array[3]}'</Price>' >> $file_out
  echo '    <Scale>'${array[4]}'</Scale>' >> $file_out
  echo '    <Manufacture>'${array[5]}'</Manufacture>' >> $file_out
  echo '    <Barcode>'${array[6]}'</Barcode>' >> $file_out
  echo '    <DatePub>'${array[7]}'</DatePub>' >> $file_out
  echo '    <Image>'${array[8]}'</Image>' >> $file_out
  echo '    <Availability>'$availability'</Availability>' >> $file_out
  echo '    <Supplier>'$Supplier'</Supplier>' >> $file_out

echo '  </Product>' >> $file_out
done < $file_in

此代码正在工作,除了“值分配部分”:

# if empty set Not Available
if [[ -z ${array[6]} ]] ; then
        $array[6]="Not Available"
        echo barcode is ${array[6]}
fi

错误:

./convert-csv-to-xml: row 20: LM113A[6]=Not Available: command not found
barcode is

第 20 行是:

$array[6]="Not Available"
shell-script array
  • 1 个回答
  • 754 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve