Register
It is currently Fri Aug 01, 2014 3:51 am

findinfile


All times are UTC - 6 hours


Post new topic Reply to topic  [ 1 post ] 
Author Message
 PostPosted: Mon Sep 17, 2012 12:04 pm   
User avatar

Joined: Mon Sep 17, 2012 11:10 am
Posts: 1
Pure BASh.
Code:
#!/bin/bash

[ ${#} -lt 2 ] && { printf "${0} <string> <file(s)>\n";exit 1; }

str="${1}"; shift; files=( "${@}" ); h=0

for ((i=0; i < ${#files[@]}; i++)); do

   file="${files[${i}]}"; j=0; k=1; m=

   [ ${h} -lt ${#file} ] && h=${#file} || \
   [ ${h} -gt ${#file} ] && for ((l=0;l < ${h} - ${#file}; l++)); do m+=" "; done

   path_to_file="${file}${m}\r"; printf "${path_to_file}"

   [ -r "${file}" ] || \
   { printf "\e[1m${file}\e[0m is \e[31;1mnot readable\e[0m\n" && continue; } && \
   [ -d "${file}" ] || \
   while read fstr; do ((j++))

      [[ "${fstr}" =~ "${str}" ]] && \
      { [ ${k} -eq 1 ] && printf "\e[1m${file}\e[0m\n" && ((k=0)) || \
      printf "\t${j}: ${fstr/${str}/\e[32;1m${str}\e[0m}\n"; }

   done < "${file}"

done

printf "\n"


Top
 Profile  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: No registered users and 6 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
cron


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP