The unix tools AWK and SED are very powerful tools to manipulate files and perform text processing tasks. It’s true that the syntax is not always very intuitive but it can help you in performing task in a single line.
The following example illustrate the usage of SED and AWK. This is surely not the best one but gives some overview about the gensub awk command and replacement commands.
Let us suppose first that you need to find recursively a list of files that match a pattern.
find . -name "__init__.py" |
These files contains the full path names (with the directory). From these files, you want to create a symbolic link (ln -s command under Linux) and in addition you want to keep to keep track of the directory (replacing the / sign with a _ sign).
find . -name "__init__.py" | awk '{print "ln -s " $1 " " gensub("/", "_", "g", $1)}' |
The “gensub” command is used by awk to replace a character by an other. The “g” means replace all occurences.
Yet, because a directory starts with “./”, all files start with a ”._”, which can be removed with “sed”. Note that the ”.” is a special character, so you need to use ”\.” instead of simply ”.” .
The final command is therefore:
find . -name "__init__.py" | awk '{print "ln -s " $1 " " gensub("/", "_", "g", $1)}' | sed -e 's/\._//g' |
One Response to Combine AWK and SED to create symbolic links