Home > OS >  How to grep columns and their ajoining rows of text from a .csv file then print in a specific patter
How to grep columns and their ajoining rows of text from a .csv file then print in a specific patter

Time:12-30

I asked this question once before but it was closed because it wasn't clear enough. I tried to edit it, but this website would not allow it. If the information below is still not clear enough, please do not close and give me the chance to fix it. I have a .csv file that contains a lot of text. I want a bash script that will allow me to input a keyword, then print all columns/rows related to keyword in a zenity text box.

My code e.g.:

#!/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;

What I expect:

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

What I get

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

Only the first column contains the keyword. I do have multiple instances where the keyword appears throughout the .csv but only in the first column

The question is, how can I get the text to print in a column/row format? The code above is an example. The actual code does not use generic terms like function, but does contain sensitive information that I do not wish to share. This is not a zenity issue. Even if I

cat filename.csv|grep keyword

the results are still

column-with-keyword,next-column-in-row-containing-keyword,next-column-in-row-containing-keyword

Basically, if my keyword is help*, all columns containing any variation of help would then be printed and joined by their row

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

I would like for it to look like in a zenity window

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

(those above statements are being said in a joking manner and not inteded to be taken serious)

CodePudding user response:

I didn't quite understand your question.

If you have this 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

you could use Miller to grep all records that contains help in every field, and create a pretty printed output in this way:

mlr --c2p grep 'help' input.csv

The output will be

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

If you do not want the header in the output, you can change the command in this way

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