diff --git a/module6.ipynb b/module6.ipynb index dfe18e2..c5224c1 100644 --- a/module6.ipynb +++ b/module6.ipynb @@ -446,6 +446,121 @@ "source": [ "for i in $(cat story.txt); do B=`echo -n \"${i:0:1}\" | tr \"[:lower:]\" \"[:upper:]\"`; echo -n \"${B}${i:1} \"; done; echo -e \"\\n\"" ] + }, + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "## Edit files using substrings" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "cat ~/data/list.txt\n", + "\n", + "# 001 jane /data/jane_profile_07272018.doc\n", + "# 002 kwood /data/kwood_profile_04022017.doc\n", + "# 003 pchow /data/pchow_profile_05152019.doc\n", + "# 004 janez /data/janez_profile_11042019.doc\n", + "# 005 jane /data/jane_pic_07282018.jpg\n", + "# 006 kwood /data/kwood_pic_04032017.jpg\n", + "# 007 pchow /data/pchow_pic_05162019.jpg\n", + "# 008 jane /data/jane_contact_07292018.csv\n", + "# 009 kwood /data/kwood_contact_04042017.csv\n", + "# 010 pchow /data/pchow_contact_05172019.csv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "grep 'jane' ~/data/list.txt\n", + "\n", + "# 001 jane /data/jane_profile_07272018.doc\n", + "# 004 janez /data/janez_profile_11042019.doc\n", + "# 005 jane /data/jane_pic_07282018.jpg\n", + "# 008 jane /data/jane_contact_07292018.csv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "grep ' jane ' ~/data/list.txt\n", + "\n", + "# 001 jane /data/jane_profile_07272018.doc\n", + "# 005 jane /data/jane_pic_07282018.jpg\n", + "# 008 jane /data/jane_contact_07292018.csv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "grep \" jane \" ~/data/list.txt | cut -d ' ' -f 3\n", + "\n", + "# /data/jane_profile_07272018.doc\n", + "# /data/jane_pic_07282018.jpg\n", + "# /data/jane_contact_07292018.csv" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# To return a range of fields together\n", + "grep \" jane \" ~/data/list.txt | cut -d ' ' -f 1-3\n", + "# To return a set of fields together\n", + "grep \" jane \" ~/data/list.txt | cut -d ' ' -f 1,3" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Find files using bash script\n", + "\n", + "#!/bin/bash\n", + "> oldFiles.txt\n", + "files=$(grep \" jane \" ../data/list.txt | cut -d ' ' -f 3);\n", + "for file in $files; do\n", + " if test -e \"..${file}\"; then echo \"..${file}\" >> oldFiles.txt; fi\n", + "done\n" + ] + }, + { + "cell_type": "code", + "execution_count": null, + "metadata": {}, + "outputs": [], + "source": [ + "# Rename files using Python script\n", + "\n", + "#!/usr/bin/env python3\n", + "import sys\n", + "import subprocess\n", + "with open(sys.argv[1]) as file:\n", + " lines = file.readlines()\n", + " for line in lines:\n", + " oldvalue = line.strip()\n", + " newvalue = oldvalue.replace(\"jane\", \"jdoe\")\n", + " subprocess.run([\"mv\", oldvalue, newvalue])\n", + "file.close()\n" + ] } ], "metadata": { diff --git a/module7.ipynb b/module7.ipynb index 709d82c..5621f7e 100644 --- a/module7.ipynb +++ b/module7.ipynb @@ -1,5 +1,12 @@ { "cells": [ + { + "cell_type": "markdown", + "metadata": {}, + "source": [ + "# Final Project" + ] + }, { "cell_type": "code", "execution_count": null,