Using a shell script, How Do I Copy and Edit Data in a File using sed or awk
by DeployTheYak from LinuxQuestions.org on (#5PNTE)
I am writing a simple archive script. It take files older than X days an moves them to a new directory. The issue is that even after archive the files need to still be accessible. The software that creates the files puts an entry in the database for where to find the files. So I also need to write an update script. What my script currently does is runs a find -mtime command and sends the output to a file. The script then uses a sed command to move the files to their new location. What I need to do next is make the output file from the find command into an update query. The thing I do not know how to do is copy the line of data onto the same line and do some edits to add the SQL syntax.
so if this is my original Find.out (Note many different file types. 2 different paths)
/some/path1/file1.xlsx
/different/path2/file2.xlsx
/different/path2/file3.docx
/some/path1/file4.pdf
What the file needs to end up looking like:
<SQL UPDATE TABLE COMMAND> '/some/NEWpath1/file1.xlsx' <SQL WHERE CLAUSE> '/some/path1/file1.xlsx';
<SQL UPDATE TABLE COMMAND> '/different/NEWpath2/file2.xlsx' <SQL WHERE CLAUSE> '/some/path1/file2.xlsx';
<SQL UPDATE TABLE COMMAND> '/different/NEWpath2/file3.docx' <SQL WHERE CLAUSE> '/some/path1/file3.xlsx';
<SQL UPDATE TABLE COMMAND> '/some/NEWpath1/file4.pdf' <SQL WHERE CLAUSE> '/some/path1/file4.xlsx';
So to recap, my question is from a shell script, how do I take a line of data in a file and paste it to that same line while making edits to the same line? It is the copy/paste part I am really stuck on.
I do not care if it is a sed or an aux but I know there must be a way to do this so it can be automated not a manual edit..
Thanks for the help.
so if this is my original Find.out (Note many different file types. 2 different paths)
/some/path1/file1.xlsx
/different/path2/file2.xlsx
/different/path2/file3.docx
/some/path1/file4.pdf
What the file needs to end up looking like:
<SQL UPDATE TABLE COMMAND> '/some/NEWpath1/file1.xlsx' <SQL WHERE CLAUSE> '/some/path1/file1.xlsx';
<SQL UPDATE TABLE COMMAND> '/different/NEWpath2/file2.xlsx' <SQL WHERE CLAUSE> '/some/path1/file2.xlsx';
<SQL UPDATE TABLE COMMAND> '/different/NEWpath2/file3.docx' <SQL WHERE CLAUSE> '/some/path1/file3.xlsx';
<SQL UPDATE TABLE COMMAND> '/some/NEWpath1/file4.pdf' <SQL WHERE CLAUSE> '/some/path1/file4.xlsx';
So to recap, my question is from a shell script, how do I take a line of data in a file and paste it to that same line while making edits to the same line? It is the copy/paste part I am really stuck on.
I do not care if it is a sed or an aux but I know there must be a way to do this so it can be automated not a manual edit..
Thanks for the help.