Over a million developers have joined DZone.

Shell Scripting Notes – Searching Matching Filenames and Matching Content

Here are a few shell scripting notes that might be useful to you.

· DevOps Zone

The DevOps Zone is brought to you in partnership with Sonatype Nexus. The Nexus Suite helps scale your DevOps delivery with continuous component intelligence integrated into development tools, including Eclipse, IntelliJ, Jenkins, Bamboo, SonarQube and more. Schedule a demo today

Some random shell scripting notes for future reference:

  • On OS X: find -E . -regex ‘pattern’

    • E specifies extended regex support
  • On Linux flavors: find . -regextype posix-regex -regex ‘pattern’

  • Posix vs basic vs extended regex character class differences.

  • Pipe result to newfile > : eg grep ‘pattern’ file > output.txt

  • Pipe result appending to file > : eg grep ‘pattern’ file >> output.txt

  • Capture output as String? : $(some expression)

Iterate files:

for f in some-file-pattern or something producing a list of files
do ... done

Use find . -name ‘pattern’ to recurse matching files down subdirs

Find with a regex for multiple patterns:

find -E . -regex ".*ext1|.*ext2|.*ext3"

first line of file:

head -n 1 filename

grep -o : only display match

Match patterns in file and output matches or matched groups:

#!/bin/bash
for f in `find -E . -regex ".*ext1|.*ext2|.*ext3"`
do
line=$(head -n 1 $f)
if [[ $line =~ TextAtStartOfLine:[[:space:]]*([[:digit:]]+) ]]; then
echo "${BASH_REMATCH[0]}" # print whole match
echo "${BASH_REMATCH[1]}" # print 1st group match
fi
done

View rawgistfile1.txt hosted by GitHub

Match files, patterns in files, and pipe matches to file:

#!/bin/bash
for f in $(find -E . -regex ".*ext1|.*ext2|.*ext3")
do
echo $f $'\n' $(egrep -o 'TextAtStartOfLine:[[:space:]]*[[:digit:]]+' $f) $'\n' >> report"$(date)".txt
done

View rawgistfile1.txt hosted by GitHub

The DevOps Zone is brought to you in partnership with Sonatype Nexus. Use the Nexus Suite to automate your software supply chain and ensure you're using the highest quality open source components at every step of the development lifecycle. Get Nexus today

Topics:
linux ,script ,shell scripting

Published at DZone with permission of Kevin Hooke, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}