如何从.csv文件中grep列及其相邻的文本行,然后以zenity的特定模式打印?[已解决]

isr3a4wc  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(110)

我问过这个问题一次,但它被关闭,因为它不够清楚。我试图编辑它,但这个网站不允许它。如果下面的信息仍然不够清楚,请不要关闭,并给予我机会来修复它。我有一个.csv文件,其中包含了很多文本。我想要一个bash脚本,将允许我输入一个关键字,然后在zenity文本框中打印与关键字相关的所有列/行。
我的代码,例如:

#!/bin/bash
path="filename.csv"
ans=$(zenity --entry --title="Keyword?" --timeout=10);

function () {
while IFS= read -r i; do echo "${i%?}"; done < "$path"|grep "$ans*"
}

function | zenity --list --title="Results" --text=function --multiple --separator=" " \
--column="tile-of-col1" --column="title-of-col2"  --column="title-of-col3" --width 800 --height 200; exit;

我的期望:

title-of-col1               title-of-col2                title-of-col3
keyword info                info on same row             info on same row

我得到的

title-of-col1                     title-of-col2                     title-of-col3
keyword,next column,next column   keyword,next column,next column

只有第一列包含关键字。我有多个示例,其中关键字出现在整个.csv,但只在第一列
问题是,我怎样才能让文本以列/行格式打印?上面的代码是一个例子。实际的代码没有使用像函数这样的通用术语,但是包含了我不想共享的敏感信息。这不是一个zenity问题。即使我
第一个月
结果仍然是
column-with-keyword,next-column-in-row-containing-keyword,next-column-in-row-containing-keyword
基本上,如果我的关键字是help*,那么包含help的任何变体的所有列都将被打印出来,并通过它们的行进行连接

help-me,go to help.com,some more info
help me please,absolutely not,go back to using pen and paper

我想让它看起来像是在一个

help-me            go to help.web     some more info
help me please     absolutely not     just use pen and paper

(以上的话都是开玩笑的,无意当真)

huus2vyu

huus2vyu1#

我不太明白你的问题。
如果您有此CSV

a,b,c
help me please,absolutely not,go back to using pen and paper
lorem ipsum, sit lorem, arundary
help-me,go to help.com,some more info

可以使用Miller来grep每个字段中包含help的所有记录,并以这种方式创建漂亮的打印输出:

mlr --c2p grep 'help' input.csv

输出将为

a              b              c
help me please absolutely not go back to using pen and paper
help-me        go to help.com some more info

如果您不希望输出中包含标头,则可以按以下方式更改命令

mlr --c2p --headerless-csv-output grep 'help' input.csv

相关问题