Linux rename time stamped file [closed] - linux

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
i have some files with timestamp (XXX_20160125-17.dmp) and i want to rename to XXX_20160124.dmp (-1 day, and only YYYYMMDD).
I try a few things but doesn't work. Thanks.

ls *.dmp |
perl -lne '
m/(.*_)(\d\d\d\d)(\d\d)(\d\d).*(\.dmp)/;
chomp($d = qx(date -d"$2-$3-$4 - 1 day" +%Y%m%d));
#rename $_, "$1$d$5" or die "rename $_ -> $1$d$5: $!\n";
print "mv $_ $1$d$5";
'
This generates commands like:
mv XXX_20160101-19.dmp XXX_20151231.dmp
mv XXX_20160125-17.dmp XXX_20160124.dmp
Assuming the filenames have no spaces, you can pipe the result into bash.
Or uncomment the 'rename' stmt in the perl script.

Related

Extracting month from day using linux terminal [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
I am having a text file containing a list of date and time just like the sample below -
posted_at"
2012-06-09 11:48:31"
2012-08-09 12:40:02"
2012-04-09 13:10:00"
2012-03-09 13:40:00"
2012-10-09 14:30:01"
2012-12-09 15:30:00"
2012-11-09 16:20:00"
I want to extract the month from each line.
P.S - grep should not be used at any point of the code
Thanks in advance!
First select date pattern :
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file
Second, extract the month :
awk -F '-' '{print $2}'
Third redirect to desired file :
>> desired_file
So mix of all this with | to final solution :
egrep '[0-9]{4}-[0-9]{2}-[0-9]{2} ' content_file| awk -F '-' '{print $2}'>> desired_file
VoilĂ 

A bash loop to echo all possible ASCII characters [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 4 years ago.
Improve this question
I know how to print all letters
{a..z} and {A..Z} and {0..9}
But is there a way to print all possible ASCII Characters via a bash loop?
You don't need a loop
echo -e \\x{0..7}{{0..9},{A..F}}
It prints all chars from 0 to 127.
If it is okay to use awk:
awk 'BEGIN{for (i=32;i<127;i++) printf("%c", i)}'
Or using printf:
for((i=32;i<127;i++)) do printf "\x$(printf %x $i)"; done
use this:
for ((i=32;i<127;i++)) do printf "\\$(printf %03o "$i")"; done;printf "\n"

How to loop over a list containing two different pattern files in linux (bash)? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I got a list containing filenames that match the following two patterns:
one is like XXX_01.fastq
another is XXX_01_001.fastq
I am going to write a for loop (in bash) to loop over all the filenames with different patterns and I need to determine which ones match the patterns above. Any help about it?
Contents of list.txt:
$ cat list.txt
AAA_01.fastq
AA_01_001.fastq
BBB_01_002.fastq
BBB_02.fastq
Example using bash pattern matching:
for file in `cat list.txt`; do
if [[ $file =~ [A-Z]{3}_[0-9]{2}\.fastq || $file =~ [A-Z]{3}_[0-9]{2}_[0-9]{3}\.fastq ]]; then
echo "MATCH $file";
fi;
done
Output:
MATCH: AAA_01.fastq
MATCH: BBB_01_002.fastq
MATCH: BBB_02.fastq

inserting text in filename using linux shell script [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I have a couple of files that needs to be renamed by using a linux shell script. I need to insert the text "ID" next to the second character of the filenames:
HP0001.txt
HP0002.txt
HP0003.txt
the script should be able to rename it to
HPID0001.txt
HPID0002.txt
HPID0003.txt
If it's just a "couple of files", it's probably easiest to just rename them manually.
Otherwise, here's a trivial script you can adapt:
for f in HP*.txt; do
f2=`echo $f|sed -e 's/HP/HPID/'`
echo mv $f $f2
done
SAMPLE OUTPUT:
ls HP*
HP001.txt HP002.txt HP003.txt
bash ./tmp.sh
mv HP001.txt HPID001.txt
mv HP002.txt HPID002.txt
mv HP003.txt HPID003.txt
Files in directory before script execution :
HP0001.txt
HP0002.txt
HP0003.txt
main
main script content
cat main.sh
for file in *
do
new_name=$(echo $file|sed 's/HP/&ID/')
mv $file $new_name 2>/dev/null
done
Directory contents after script execution:
ls -1
HPID0001.txt
HPID0002.txt
HPID0003.txt
main

rename multiple files [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I have multiple file like :
abc_e1000g1.pcap.1
abc_e1000g1.pcap.2
I have to rename this to
1abc_e1000g1.pcap
2abc_e1000g1.pcap
Any unix / dos command for this.
I'm sure there are shorter ways do to this, but here's what comes naturally, using unix shell, assuming there's nothing but the files of interest in your present working directory:
for i in *; do mv "$i" "$(echo $i | sed 's/\(.*\)\.\(.*\)/\2\1/')"; done
Maybe this PowerShell Command would work for you.
dir | rename-item -newName { $_.Name -replace '^(.+)\.([0-9]+)$', '$2$1' }

Resources