Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unix bash cutting and grep

Tags:

grep

bash

unix

cut

I have a text file called db.txt. Some sample lines from the file goes as such:

Harry Potter and the Sorcerer's Stone:J.K. Rowling:21.95:100:200

Harry Potter and the Chamber of Secrets:J.K. Rowling:21.95:150:300

Lord of the Rings, The Fellowship of the Ring:J.R.R. Tolkien:32.00:500:500

A Game of Thrones:George R.R. Martin:44.50:300:250

Then in my script, I have the following lines:

echo "Enter title:"
read TITLE

cut -d ":" -f 1 db.txt | grep -iw "$TITLE" | while read LINE
do
    STRING="`echo $LINE | cut -d ":" -f 1`,"
    STRING="$STRING `echo $LINE | cut -d ":" -f 2`, "
    STRING=" \$$STRING`echo $LINE | cut -d ":" -f 3`,"
    STRING=" $STRING`echo $LINE | cut -d ":" -f 4`,"
    STRING=" $STRING`echo $LINE | cut -d ":" -f 5`"
done

Is there a way to grep a specific field from cut and then pass in the full line into the while loop?

For example, if I entered "Harry Potter", it should display:

Harry Potter and the Sorcerer's Stone, J.K. Rowling, $21.95, 100, 200

Harry Potter and the Chamber of Secrets, J.K. Rowling, $21.95, 150, 300

like image 502
Jary Rym Avatar asked Jan 09 '13 14:01

Jary Rym


People also ask

What is the difference between cut and grep?

grep : a tool for printing lines matching a pattern. cut : a tool for cutting out selected portions of each line of a file.

What is cut in grep?

The CUT command is a command-line tool for cutting data from each line of files and writing the result to standard output. It can be used to cut parts of a line by byte position, character, and delimiter. It can also be used to cut data from file formats like CSV.

What does && do in bash?

"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0).

What is the grep command in bash?

The grep command searches the given files for lines containing a match to a given pattern list. In other words, use the grep command to search words or strings in a text files.


1 Answers

You can do this without cut, and without grep if you're ok with bash's regular expression matching (or can use shell pattern matching instead).

The idea would be to read the file line by line, then split the line into an array. Once you've got that, do the comparisons and output you want.

Here's a demo of the technique:

#! /bin/bash
echo "Title:"
read title

# shopt -s nocasematch           # if you want case-insensitive matching

while read line ; do             # this read takes data from input.txt, see
                                 # end of loop
        IFS=: read -a parts <<< "$line"  # this splits the line on ":" into
                                         # an array called parts

        if [[ ${parts[0]} =~ $title ]] ; then  # regex matching
                printf "%s -- %s\n" "${parts[1]}" "${parts[2]}"
        fi
done < input.txt
like image 127
Mat Avatar answered Sep 20 '22 18:09

Mat