-3

Is there a way to search the content of a specific column of a csv file using the terminal. But that it still returns all the information of the rest of the columns.

I want to be able to search for a string in a column of a csv file and if it matches return all of the information in the same Row

Is there away of implementing it into a shell bash file and ask the user what string to search

6
  • You can use awk. Have you tried anything so far?
    – muru
    Dec 16, 2014 at 17:23
  • How would I use the awk command @muru
    – Jhondoe
    Dec 16, 2014 at 17:30
  • Like so: awk -F, '$N == 'some string', replacing N with the column number under consideration.
    – muru
    Dec 16, 2014 at 17:31
  • Where would I enter the file name
    – Jhondoe
    Dec 16, 2014 at 17:34
  • Where would I add the file name @muru
    – Jhondoe
    Dec 16, 2014 at 17:34

2 Answers 2

1

Lets say your string to search is "name" in the 4th column, then use

awk -F ',' '$4 ~ /^name$/' filename.csv
1
awk -F',' '$N == "string to search"' filename.csv

Replace N with column number and filename.csv with filename to search

10
  • How could you implement this into a shell bash file and ask the user the string to search @KasiyA
    – Jhondoe
    Dec 16, 2014 at 18:11
  • printf "please enter pattern to search:\n%s"; read pattern; awk -v patt="$pattern" -F',' '$N == patt' filename.csv Dec 16, 2014 at 18:32
  • Could you be more clear in your answer @KasiyA
    – Jhondoe
    Dec 16, 2014 at 18:34
  • printf "please enter pattern to search:\n%s" jut prints the please enter pattern to search: then prints a new-line(\n). read pattern read from stdin and store the input into pattern variable. then in -v patt="$pattern", stored or entered string from user, copies into awk patt local variable and rest of the command search in column $N if it's equal with user entered input. Dec 16, 2014 at 18:40
  • I have tried what you said and it does not display any results
    – Jhondoe
    Dec 16, 2014 at 18:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.