To start with, I know this way is quite not the right one, as often mentioned why shouldn't I parse the output of ls, but it is a part of my university project (so I could use sed and awk along).I have to find files which names start with a given string. I stared with listing the current directory using ls:ls -LR1which gives me the example output:.:binetcgamesincludelibmansbinsharesrc./bin:aptgnome-helphighlightmint-sha256sumpastebinsearchszukajyelp./etc:./games:./include:./lib:python2.7python3.5./lib/python2.7:dist-packagessite-packages./lib/python2.7/dist-packages:./lib/python2.7/site-packages:./lib/python3.5:dist-packages./lib/python3.5/dist-packages:./man:./sbin:./share:ca-certificatesemacsfontsmansgmlxml./share/ca-certificates:./share/emacs:site-lisp./share/emacs/site-lisp:./share/fonts:./share/man:./share/sgml:declarationdtdentitiesmiscstylesheet./share/sgml/declaration:./share/sgml/dtd:./share/sgml/entities:./share/sgml/misc:./share/sgml/stylesheet:./share/xml:declarationentitiesmiscschema./share/xml/declaration:./share/xml/entities:./share/xml/misc:./share/xml/schema:./src:Now I want to use sed or possibly awk to get to get files with names starting with given string, lets say 'Do'. I would like to achieve something like this:sed (or awk?) parses each line and searches for line that starts with 'Do'If there is a match, moves to previous line and searches for . (which is an indicator for relative path), if there's no match jumps to the line before and so onPrints the line as relative path:.path/filenameIs it even possible to achive?Thank you for your help!
Parse ls output using sed to get file location of files with specific names
sed;awk;ls
As an example, let's consider these files:$ ls -LR1.:a./a:b./a/b:badDo_goodNow, let's find the file that you want:$ ls -LR1 | awk '/^\.\//{sub(/:$/, /, $0); dir=$0} /^Do/{print dir $0}'./a/b/Do_goodHow it works:/^\.\//{sub(/:$/, /, $0); dir=$0}Every time that we find a line that begins with ./, we replace the find : with a / and update the variable dir/^Do/{print dir $0}Every time that we find a line that begins Do, we print the variable dir followed by this line.Limitation: As you are aware, the output of ls is intended to be human-readable and parsing it is unreliable.  This code is only for purposes of demonstrating the use of awk.  It should not be used for anything serious.Preferred methodAs wildcard pointed out, find is the right tool for this task:$ find . -name 'Do*'./a/b/Do_good