Hi all,

I am having a nightmare trying to filter some text in a file for a script I am working on and was wondering if anyone could help - I am by no way an expert in BASH Scripting, but I am learning :slight_smile:

I have attached the file I am working with and I am trying to filter only the .jpg name so “b359f1f7127503c810465661a2da8a5b.jpg”

Does anyone know how I can do this?

Thanks

Test.txt (75 Bytes)

4 Spice ups

I’m not clear on what you want the output to wind up being.

grep file jpg

**grep "jpg" Test.txt |awk '{print $3}'**
1 Spice up

Assuming this is the only file you are working with:

grep ".jpg" Test.txt | awk '{ print $3 }'
1 Spice up

BrentMHK and The Matt Thanks that works. What if I had multiple .jpg files I wanted to filter. For example…attached new file.

Test.txt (118 Bytes)

The same command should work.

[~]# grep "jpg" Test.txt |awk '{print $3}'
b359f1f7127503c810465661a2da8a5b.jpg
black-white-background-35.jpg

so it does :slight_smile:

thank you very much, now I just need to work out what it means :confused:

If you run the command I listed again, you should have a two-line output with just file names.

Here is the play-by-play on the command:

grep ".jpg" Test.txt

grep searches all lines for string “.jpg” within file Text.txt and outputs the entire line that contains the match (this is case sensitive and will not pick up “.JPG” or any other permutation of capitalization).

|

Pipe, the most beloved character in all of *nix-land, uses the output from grep and feeds it as input to the command that follows it.

awk '{ print $3 }'

Awk is a very complicated program (basically its own programming language). In this very simplistic usage, awk is instructed to print the third field of characters, using a whitespace as a default delimiter (" " ← the space between the quotes is a whitespace). So, the output will be the third field of characters of lines that contain “.jpg”.

Please note that this command will only work on the exact formatting you have in your input files, and only if the jpg filenames do not include a space (awk will interpret them as delimiters).

1 Spice up

Thanks The Matt. I use Grep and | all the time when scripting, it was AWK I couldn’t get to grips with but after you explained it as ‘very complicated’ it makes me feel a bit better :slight_smile:

I’m going to try and learn it all the same.

Thanks for your help :slight_smile:

OK back again…

If I want to use the output of that above command in a bash script that runs a command for each result how would I do that?

For example if I then wanted to delete each file listed one by one with the ‘rm’ command (I need to avoid using the * wildcard) how would I do it?

For filenames with spaces use:

grep "jpg" Test.txt |awk '{print substr($0, index($0,$3))}'

That will print everything from the 3rd column to the end of the line without quotes. If you need quotes then use:

grep "jpg" Test.txt |awk '{print "\"" substr($0, index($0,$3)) "\""}'
1 Spice up

Enclose the above commands in backticks like so:

rm `grep "jpg" Test.txt|awk '{print "\"" substr($line, index($line,$3)) "\""}'`
2 Spice ups