LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices


Reply
  Search this Thread
Old 05-07-2016, 02:21 AM   #16
plasma33
LQ Newbie
 
Registered: May 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled

David, I tried copying and pasting your given code against .jpg files. It works without any issues but for some weird reason it doesn't want to work on the .tif file format. When I try to use the codes given below it works without any issues and gives me what I want but without the String search count:
Code:
xxd -p /your/file | tr -d '\n' | grep -c '22081b00081f091d2733170d123f3114' WORKS FINE and returns 1 for a single .tif file. I tried on 400 .tif files too and it works without any issues and returns me 1 each time.

and,

awk '{/\x00\x02\x13\x12\x30\x44/ {print FILENAME ; exit}' $f WORKS FINE too and returns the full .tif file locations for all the available .tif files in that specific directory.
What might be possibly wrong? I am 100% certain that the string I am using is a genuine one and is found inside all the 400 .tif files.

Thanks.

Plasma33
 
Old 05-07-2016, 02:25 AM   #17
plasma33
LQ Newbie
 
Registered: May 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by syg00 View Post
I would simply pipe it to "wc -l".
Hi syg00,

Is this the right way to do it?
Code:
wc -l | awk '{/\x00\x02\x13\x12\x30\x44/ {print FILENAME ; exit}'
Thanks.

Plasma33
 
Old 05-07-2016, 02:34 AM   #18
DavidPhillips
LQ Guru
 
Registered: Jun 2001
Location: South Alabama
Distribution: Fedora / RedHat / SuSE
Posts: 7,163

Rep: Reputation: 58
I'm not sure as I was using your example with xxd. You could substitute the whole command string from cat with your awk command.

file findhex
Code:
chmod 755 findhex
Code:
#!/bin/sh 
FTYPE=$1
STR=$2
echo
echo 'Total Files '`ls *"$FTYPE" | wc -l` 
echo 'Search string count '`cat *"$FTYPE" | xxd -p $f | tr -d '/n'|grep -c "$STR" | awk '{ SUM += $1}; END { print SUM }'`


david@david-Dell-System-XPS-L702X ~/pics $ ./findhex png 67984

Total Files 37
Search string count 33


[/code]

Last edited by DavidPhillips; 05-07-2016 at 02:42 AM.
 
1 members found this post helpful.
Old 05-07-2016, 03:01 AM   #19
syg00
LQ Veteran
 
Registered: Aug 2003
Location: Australia
Distribution: Lots ...
Posts: 20,181

Rep: Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755Reputation: 3755
Nope, other end - pipe the output of awk to wc
 
1 members found this post helpful.
Old 05-07-2016, 04:52 AM   #20
plasma33
LQ Newbie
 
Registered: May 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thank you for that awesome innovative code. It worked without any issues but for some unknown reason its giving me a count of 1 instead of 400 again. Is there any chance of piping the output of xxd to wc as @syg00 is suggesting. Thank you @syg00 for that comment. Could you please demostrate by giving us a command for it, please.

Thanks.

Plasma33
 
Old 05-07-2016, 04:00 PM   #21
plasma33
LQ Newbie
 
Registered: May 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Hi guys,

Thank you for all your support and help here. I managed to find this code through stack overflow website. Please find the working code below:
Code:
#!/bin/bash

files=/FILEPATH/*
for f in $files
do
  # Watch byte order. To grep for 22081b00:
  grep -m 1 -P '\x22\x08\x1b\x00' < "$f"
done | wc -l
Thanks, again for all your efforts here.

Plasma33
 
Old 05-07-2016, 04:26 PM   #22
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
Why not use -l (L lowercase) switch for grep, you save a loop

Code:
grep -l -P '\x22\x08\x1b\x00' /FILEPATH/* | wc -l
 
3 members found this post helpful.
Old 05-08-2016, 02:31 AM   #23
plasma33
LQ Newbie
 
Registered: May 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by keefaz View Post
Why not use -l (L lowercase) switch for grep, you save a loop

Code:
grep -l -P '\x22\x08\x1b\x00' /FILEPATH/* | wc -l
Thanks, keefaz for your code. It works without any issues. Any ideas for adding echo "Something Here" command inside a loop especially inside the code given below, please. I can't do it for some reason. Also, is it possible to add echo command after done and before the pipe?

Code:
#!/bin/bash

files=/FILEPATH/*
for f in $files
do
  # Watch byte order. To grep for 22081b00:
  grep -m 1 -P '\x22\x08\x1b\x00' < "$f"
done | wc -l
Thanks.

Plasma33

Last edited by plasma33; 05-08-2016 at 02:37 AM.
 
Old 05-08-2016, 06:31 AM   #24
keefaz
LQ Guru
 
Registered: Mar 2004
Distribution: Slackware
Posts: 6,325

Rep: Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760Reputation: 760
If you add any echo line before the wc -l pipe, you'll end with wrong count at output, wc will count also your echo lines
Or maybe redirect echo output to standard error
As for putting a command between the done and the wc pipe, it won't work, you'll break link with output from the loop and wc pipe input.

Code:
for f in $files
do
  # Watch byte order. To grep for 22081b00:
  grep -m 1 -P '\x22\x08\x1b\x00' < "$f"
  echo "Something Here" >&2
done | wc -l
 
1 members found this post helpful.
Old 05-08-2016, 07:57 PM   #25
plasma33
LQ Newbie
 
Registered: May 2016
Posts: 13

Original Poster
Rep: Reputation: Disabled
Thanks, keefaz. It works perfectly. Thanks for the explanation.

Plasma33
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ bin array to hex string and back teapottwo Programming 5 12-03-2015 07:43 PM
Migrating Java application from HP/UX to Linux - String to byte array issues JayMac2013 Linux - Newbie 1 04-07-2013 02:30 PM
[SOLVED] searching for hex string in files and folders cosgeazy Linux - Software 16 09-28-2012 07:34 AM
Searching a specific directory for files containing a text string lothwen Linux - Newbie 1 11-20-2010 11:58 AM
Hex output of a hex/ascii input string mlewis Programming 35 04-10-2008 01:05 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Software

All times are GMT -5. The time now is 10:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration