I currently have a directory of files with date based names in the format mmddyy:
nxd060815a.html
nxd060915a.html
nxd061015a.html
and would like to change file name format to yyyy-mm-dd:
nxd2015-06-08a.html
nxd2015-06-09a.html
nxd2015-06-10a.html
How would I do this from the command line?
Any help is appreciated.
ls | awk '{ system("mv " $0 " " substr($0,1,3) "20" substr($0,8,2) "-" substr($0,4,2) "-" substr($0,6,2) substr($0,10,6))}'
run the above from the dir the files are in. You should be able to figure out whats going on, so no other comment.
Related
I try to add a string to the first line in text files.
The first part of every string is identical. Here "#Test:". The last part of the string should incorporate the digit which is derived from the folder and file names.
The folder names consist exclusively of digits from 1-52. There are between 1-20 files in each folder with the following structure:
1 (folder)
1_tree1 (file)
1_tree2 (file)
1_tree3 (file)
...
2 (folder)
2_tree1 (file)
2_tree2 (file)
2_tree3 (file)
...
...
The operating system is Ubuntu 20.04.
I am able to change each file separately. For example, the following command in the terminal adds #Tree:1 for one file the first folder.
sed -i '1s/^/#Test:1 \n/' '/path/to/the/file'
However, if I try to do this for all files in the folder, I can not proceed. Could you show me how to do it automatically? I am not necessarily restricted to sed.
Thank you.
This might be what you want, using GNU awk for "inplace" editing, BEGINFILE (so it'll work even on empty input files), and gensub():
find . -type f -exec awk -i inplace '
BEGINFILE { print "#Test:" gensub(".*/([0-9]+)_[^/]+$","\\1",1,FILENAME) }
1' {} +
I would like to retain a backup file, only if sed altered the original file.
for example:
I have the following file:
# cat test
This is example file
abcd
efgh
process with sed so there is nothing to change:
# sed -i.BAK "s/AAAA/BBBB/" test
The "test" file is not changed because nothing matched. In this case, I would like to avoid the backup file that was created:
# md5sum test*
d3ca57583595576338ad6f9a01276cd5 test
d3ca57583595576338ad6f9a01276cd5 test.BAK
I learned that what I was asking is not possible by "sed" as I suspected by RTFM.
I solved by adding "if [ grep ... ] " on the expression needed to replace.
The "sed" is performed if and only if the expression exists.
Thanks for the people that commented.
I create a variable and store the day, date & time in it:
NOW=$(date "+%a %d/%m/%Y% %H:%M")
Then I would like to pass $NOW to the mv command to rename a file.
e.g. Create file named a.txt with a title and the current date:
printf "File Report (" > ~/Desktop/a.txt
echo $NOW"):\n" >> ~/Desktop/a.txt
Then I try to rename the file with the variable ($NOW) included in the name:
mv ~/Desktop/a.txt ~/Desktop/'File Report $NOW'.txt
What should that last line be? I also tried these two options.
mv ~/Desktop/a.txt ~/Desktop/'File Report' $NOW.txt
&
mv ~/Desktop/a.txt ~/Desktop/'File Report'${NOW}.txt
Assuming a reasonably Bourne-like shell (such as bash), variable substitution does not happen inside single quotes. You need to use double quotes:
mv ~/Desktop/a.txt "${HOME}/Desktop/File Report ${NOW}.txt"
(I'm not sure whether the curly braces are required, but they can't hurt)
You will also need to change the date command to avoid the use of slashes. For example:
NOW="$(date '+%a %d-%m-%Y% %H:%M')"
I am trying to run a bash command like this one in scala:
cat "example file.txt" | grep abc
Scala has a special syntax for process piping, so this was my first approach:
val filename = "example file.txt"
(Process(Seq("cat", filename)) #| Process(Seq("grep", "abc"))).run()
As far as I understand, this executes the first process, reads the output back to scala and feeds it to the second process. The problem is that, for performance reasons, I would like to execute both processes without leaving the terminal. The file is huge and I don't need the whole output, that's why I am using grep in the first place. So, my second approach was this one:
val filename = "example file.txt"
(Process(Seq("bash", "-c", "cat " + filename + " | grep abc"))).run()
The problem here is that it breaks if the filename has spaces. I could try to escape spaces, but I would rather have scala doing it for me (there are many other characters that I would also need to escape).
Is there another way to run this command?
It is easy enough to escape the filename:
val escapedFilename = "'" + filename.replace("'", "'\\''") + "'"
But the right way to do this is to pass the filename directly to grep:
Process(Seq("grep", "abc", filename)).run()
Which is equivalent to this in the shell:
grep abc "example file.txt"
I have a bunch of.txt files that need to be made into one big file that can be read by programs such as Microsoft Excel.
The problem is that the files currently do not have a break at the end of them, so they end up in one long line.
Here's an example of what I have (the numbers represent the line number):
1. | first line of txt file
2. | second line
Here's what I want to turn that into:
1. | first line of txt file
2. | second line
3. |
I have around 3000 of these files in a folder, all in the same format. Is there any way to take these files and add a blank line to the end of them all? I'd like to do this without the need for complicated code, i.e. PHP, etc.. I know there are similar things you can do using the terminal (I'm on CentOS), but if something does specifically what I require I'm missing it.
The simplest way to achieve this is with a bash for-loop:
for file in *.txt; do
echo >> "$file"
done
This iterates over all .txt files in the current directory and appends a newline to each file. It can be written in one line, you only need to add a ; before the done.
Note that $file is quoted to handle files with spaces and other funny characters in their names.
If the files are spread across many directories and not all in the same one, you can replace *.txt with **/*.txt to iterate over all .txt files in all subdirectories of the current folder.
An alternative way is to use sed:
sed -i "$ s:$:\n:" *.txt
The -i flag tells sed to edit the files in-place. $ matches the last line, and then the s command substitutes the end of the line (again $) with a new line (\n), thus appending a line to the end of the file.
Try this snippet:
for f in *; do ((cat $f && echo "") > $f.tmp) done && rename -f 's/\.tmp$//' *.tmp
This basically takes any file in the folder (for f in *; do).
Outputs the file on STDOUT (cat $f) followed by a newline (echo "")
and redirects the output into filename.tmp (> $f.tmp)
and then moves the *.tmp files to the original files (rename -f 's/\.tmp$//' *.tmp).
Edit:
Or even simpler:
for f in *; do (echo "" >> $f) done
This basically takes any file in the folder (for f in *; do).
Outputs a newline (echo "")
and appends it to the file (>> $f)