Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The sed command can be used to edit text using pattern substitution. While it is very powerful, the regex syntax for some of its more advanced features is quite different from "standard" grep or perl regexes. As a result, I tend to use it only for very simple substitutions, usually as a component of a multi-pipe expression.

Code Block
languagebash
# lookLook for runs in the SA18xxx and report their job and run numbers without JA/SA but with other text
cat joblist.txt | grep -P 'SA18\d\d\d$' | sed 's/JA/job /' | sed 's/SA/run /'

# List all student home directories, both the full paths and the student account sub-directory
#   inIn the 1st sed expression below, note #the   use of backslash escaping of the
# forward slash character we want to strip.
#   theThe g modifier says to replace all instances of the forward slash
for dir in `ls -d /stor/home/student0?/`; do
  dir2subdir=$( echo $dir | sed 's/\///g' | sed 's/stor//' | sed 's/home//' )
  echo "full path: $dir - directory name $dir2$subdir"
done

perl pattern substitution

...