user3463055
user3463055

Reputation: 131

UNIX: Finding lines

I need to write a small script, which it find lines according to a regular expression (for example "^folder#) and it will write the number of lines where it matchs.

My idea is, that I will use "find", then delete all slash and then use grep with a regular expression. I don't know why it doesn't work. Could you give some advice how to improve, or how I should find that lines with another function?

In

./example
./folder/.DS_Store
./folder/file.png

Out

2: ./folder/.DS_Store
3: ./folder/file.png

IGN="^folder$"

find . -type f | sed -e "s/\// /g" | grep -n "${IGN}"

Upvotes: 0

Views: 45

Answers (2)

Walter A
Walter A

Reputation: 19982

You tried

IGN="^folder$"
find . -type f | sed -e "s/\// /g" | grep -n "${IGN}"

This script isn't working since IGN looks for start-of-line, not start-of-word.
You can make lines from the parts of your paths with

IGN="^folder$"
find . -type f | tr -s "/" "\n" | grep -n "${IGN}"

Upvotes: 0

janos
janos

Reputation: 124648

You say you want to use ^folder$ pattern but you want to get output like:

2: ./folder/.DS_Store
3: ./folder/file.png

These two requests contradict each other. A line like ./folder/.DS_Store cannot match pattern ^folder$ because the line doesn't start with "folder" and doesn't end with "folder".

To get the output you describe you need to change the pattern used with grep to ^\./folder/

Upvotes: 1

Related Questions