Is it possible to use scp and change a filename to $date - shell

So I have this script that creates a file called "myfile.txt" this name will never change.
I have 2 servers srv01 and srv02
srv01:/home/user/scriptdata/myfile.txt is the file I want to move to srv02
I want this file to arrive at srv02 in the following format
srv02:/home/user/scriptoutput/$date.txt
Is there an easy way to do this?
So far I have been using scp /home/user/scriptdata/myfile.txt 12.34.56.78:/home/user/scriptoutput/day-month-year.txt manually

scp /home/user/scriptdata/myfile.txt 12.34.56.78:/home/user/scriptoutput/data_$(date +%d-%m-%Y_%H:%M).txt
This worked! thanks jhnc for ur answer

Related

I need to copy only specific files that have in naming the dates between start date and end date

I have the following files in a directory
file1_20221010_095000.log
file2_20221011_105000.log
file3_20221012_115000.log
file4_20221013_125000.log
file5_20221014_135000.log
file6_20221015_145000.log
And if I use my script with the following command with 2 parameters as below:
Myscript.sh <START_DATE> <END_DATE>
Myscript.sh 20221011_105000 20221014_135000
I want to copy only these files to another directory:
file2_20221011_105000.log
file3_20221012_115000.log
file4_20221013_125000.log
file5_20221014_135000.log
Thanks in advance.
I m not sure how it is best way to proceed. Every help will be appreciated.

Replace string path in log file with current folder name

I'm new here and already tried to find solution to the following requirement without success. I'm trying to achieve this:
I have these 5 folders:
ServiceEngine
PaymentEngine
InvoiceEngine
ProcessEngine
OrderProcessEngine
Inside each of these folders, I have a log file with default path location to store the log files e.g. ServiceEngine/logs
The log file contain the following path structure:
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
I expect to find a way that I retrieve the name of the current folder which I'm in and replace the string engine with folder name
Example: I'm in ServiceEngine folder and execute a command that retrieve the current folder name. The expected result is:
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log
Later I change the directory to PaymentEngine and the expected result is:
name="RollingRandomAccessFile" fileName="logs/PaymentEngine.log"
filePattern="logs/PaymentEngine-%i.log
and so on. Maybe there is a smarter way to create a script that update the string in a loop like do; if ... fi; done or to use the for in ... loop.
Do you mean something like this?
~/ServiceEngine$ cat logfile
name="RollingRandomAccessFile" fileName="logs/engine.log"
filePattern="logs/engine-%i.log"
~/ServiceEngine$ awk -v path=$(basename $(pwd)) 'gsub("engine", path)' logfile
name="RollingRandomAccessFile" fileName="logs/ServiceEngine.log"
filePattern="logs/ServiceEngine-%i.log"
See basename, declaring variables in awk and awk gsub.
I guess I don't understand your question, but:
dir=$( basename $( pwd ) )
echo name="RollingRandomAccessFile" \
fileName="logs/$dir.log" \
filePattern="logs/$dir-%i.log"
Is that what you're looking for?
It sounds like you are looking for something like this:
$ sed 's/\bengine\b/'$(basename $(pwd))'/' logs
When run from within one of your folders, it spits out the text you're asking for. It wasn't clear what you wanted to do with that text though.

Merge lines in bash

I would like to write a script that restores a file, but preserving the changes that may be done after the backout file is created.
With more details: at some moment I create a backup of a file (file_orig). Do some changes to the original file as well(file_my_changes). After that, the original file can be changed again (file_additional_changes), but after the restore I want to have the backup file, plus the additional changes (file_orig + file_addtional_changes). In general backing out my changes only.
I am talking about grub.cfg file, so the expected possible changes will be adding or removing parts of a line.
Is it possible this to be done with a bash script?
I have 2 ideas:
Add some comments above the lines I am going to change, and then before the resotore if the line differ from the one from the backed out file, to read the comment, which will tell me what exactly to remove from the line;
If there is a way to display only the part of the line that differs from the file_orig and file_additional_changes, then to replace this line with the line from file_orig + the part that differs. But I am not sure if this is possible to be done at all.
Example"
line1: This is line1
line2: This is another line1
Is it possible to display only "another"?
Of course any other ideas are welcome!
Thank you!
Unclear, but perhaps if you're using a bash script you could run a diff on the 2 edited file and the last one and save that output someplace that you want to keep it? That would mean you have a copy of the changes.
Or just use git like everybody else.
One possibility would be to use POSIX commands patch and
diff.
Create the backup:
cp operational-file operational-file.001
Edit the operational file.
Create a patch from the differences:
diff -u operational-file.001 operational-file > operational-file.patch001
Copy the operational file again.
cp operational-file operational-file.002
Edit the operational file again.
Create a new patch
diff -u operational-file.002 operational-file > operational-file.patch002
If you need to recover but skip the changes from patch.001, then:
cp operational-file.001 operational-file
patch -i patch.002
This would apply just the second set of changes to the original file, as log as there's no overlap.
Consider using a version control system to keep records of the file changes. Consider using date/time stamps instead of version numbers on the file names.

sql loader without .dat extension

Oracle's sqlldr defaults to a .dat extension. That I want to override. I don't like to rename the file. When googled get to know few answers to use . like data='fileName.' which is not working. Share your ideas, please.
Error message is fileName.dat is not found.
Sqlloder has default extension for all input files data,log,control...
data= .dat
log= .log
control = .ctl
bad =.bad
PARFILE = .par
But you have to pass filename without apostrophe and dot
sqlloder pass/user#db control=control data=data
sqloader will add extension. control.ctl data.dat
Nevertheless i do not understand why you do not want to specify extension?
You can't, at least in Unix/Linux environments. In Windows you can use the trailing period trick, specifying either INFILE 'filename.' in the control file or DATA=filename. on the command line. WIndows file name handling allows that; you can for instance do DIR filename. at a command prompt and it will list the file with no extension (as will DIR filename). But you can't do that with *nix, from a shell prompt or anywhere else.
You said you don't want to copy or rename the file. Temporarily renaming it might be the simplest solution, but as you may have a reason not to do that even briefly you could instead create a hard or soft link to the file which does have an extension, and use that link as the target instead. You could wrap that in a shell script that takes the file name argument:
# set variable from correct positional parameter; if you pass in the control
# file name or other options, this might not be $1 so adjust as needed
# if the tmeproary file won't be int he same directory, need to be full path
filename=$1
# optionally check file exists, is readable, etc. but overkill for demo
# can also check temporary file does not already exist - stop or remove
# create soft link somewhere it won't impact any other processes
ln -s ${filename} /tmp/${filename##*/}.dat
# run SQL*Loader with soft link as target
sqlldr user/password#db control=file.ctl data=/tmp/${filename##*/}.dat
# clean up
rm -f /tmp/${filename##*/}.dat
You can then call that as:
./scriptfile.sh /path/to/filename
If you can create the link in the same directory then you only need to pass the file, but if it's somewhere else - which may be necessary depending on why renaming isn't an option, and desirable either way - then you need to pass the full path of the data file so the link works. (If the temporary file will be int he same filesystem you could use a hard link, and you wouldn't have to pass the full path then either, but it's still cleaner to do so).
As you haven't shown your current command line options you may have to adjust that to take into account anything else you currently specify there rather than in the control file, particularly which positional argument is actually the data file path.
I have the same issue. I get a monthly download of reference data used in medical application and the 485 downloaded files don't have file extensions (#2gb). Unless I can load without file extensions I have to copy the files with .dat and load from there.

Create new files from existing ones but change their extension

In shell, what is a good way to duplicating files in an existing directory so that the result gives the same file but with a different extension? So taking something like:
path/view/blah.html.erb
And adding:
path/view/blah.mobile.erb
So that in the path/view directory, there would be:
path/view/blah.html.erb
path/view/blah.mobile.erb
I'd ideally like to perform this at a directory level and not create the file if it already has both extensions but that isn't necessary.
You can do:
cd /path/view/
for f in *.html.erb; do
cp "$f" "${f/.html./.mobile.}"
done
PS: This replaces first instance of .html. with .mobile., syntax is bash specific (let me know if you're not using BASH).

Resources