separate a string - sed

I have a file as shown below:
Sep 6 18:59 Cash_A2_updates_03_08_2011.sql
Sep 6 18:59 Cash_A2_updates_04_08_2011.sql
Sep 6 18:59 Cash_A2_updates_05_08_2011.sql
The first word in the file name nothing but the schema name. I want to keep it in a separate column and the rest of the file name in the last column as shown below.
Sep 6 18:59 Cash A2_updates_03_08_2011.sql
Sep 6 18:59 Cash A2_updates_04_08_2011.sql
Sep 6 18:59 Cash A2_updates_05_08_2011.sql

sed 's/_/ /'
Assuming that's always the first _ in there.

awk version: (if the file content is like what you shown us)
awk 'sub(/_/," ")' yourFile
test:
kent$ echo "Sep 6 18:59 Cash_A2_updates_03_08_2011.sql
Sep 6 18:59 Cash_A2_updates_04_08_2011.sql
Sep 6 18:59 Cash_A2_updates_05_08_2011.sql"|awk 'sub(/_/," ")'
output
Sep 6 18:59 Cash A2_updates_03_08_2011.sql
Sep 6 18:59 Cash A2_updates_04_08_2011.sql
Sep 6 18:59 Cash A2_updates_05_08_2011.sql

Related

Tar command throwing errors when attempting to create an archive using the date command

I use the tar command a lot, very familiar with it. However, I tried creating an archive using the date command to name the archive file, and it throws an error to the effect that it can't open the file, which is strange because I'm not trying to open the file but create it.
Here's a copy of the directory, the code, and the error:
#MDG /media/VideoCam/Test $ ll
total 468
drwxr-xr-x 3 neo neo 4096 Sep 5 09:55 ./
drwxr-xr-x 4 neo neo 466944 Sep 4 21:54 ../
-rw-r--r-- 1 neo neo 45 Sep 5 08:52 1.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 1.mp4
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 2.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 2.mp4
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 3.jpg
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 4.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 4.mp4
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 5.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 5.mp4
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 6.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 6.mp4
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 7.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 7.mp4
-rw-r--r-- 1 neo neo 0 Sep 4 19:32 8.jpg
-rw-rw-r-- 1 neo neo 0 Sep 5 08:41 8.mp4
drwxr-xr-x 2 neo neo 4096 Sep 4 19:30 Archive/
neo#MDG /media/VideoCam/Test $ sudo tar -zcvf "archive.$(date '+%D').tar.gz" *.jpg
tar (child): archive.09/05/17.tar.gz: Cannot open: No such file or directory
tar (child): Error is not recoverable: exiting now
1.jpg
2.jpg
3.jpg
4.jpg
5.jpg
6.jpg
7.jpg
8.jpg
neo#MDG /media/VideoCam/Test $
I've tried many different concatenations, with and without quotes, switching up the order of the command options (zcvf), etc. If I leave out the date command and just give it a name, e.g. archive.tar.gz, it executes perfectly, but introducing the date command causes the error. I've also tried setting the date command as a variable with a similar result.
If you look at the error message, you see
tar (child): archive.09/05/17.tar.gz: Cannot open: No such file or directory
This indicates that it is trying to create the file archive.09/05/17.tar.gz. That is, 17.tar.gz in the directory 05, in the directory archive.09.
If this is what you actually want, which I doubt, you need to create the directory outside tar with something like
mkdir -p archive.09/05
If you don’t want subdirectories, you cannot use the / character in the filename. Try a different format for the date command; one common option is +%F because it generates filenames which would still be in the right order if sorted as text.
I strongly recommend you read the date man page and check the formatting options.
As it's been suggested by others the issue is with the format of date output
$ date '+%D'
09/05/17
You can't create a file with slashes in the name because of Unix typical behavior. You should instead try something like
$ date '+%m-%d-%Y'
09-05-2017
That in case you want to preserve the same formatting as %D.
The date is pritning out slashes and it's treating it as folder path. Use dashes.

I broke the FIND command of Ubuntu, mtime not working as it should

I'm building a script for backup and I'm heavily using the FIND with -mtime.
Yesterday I used find -mtime +1 a lot, to search the file modified more than a day ago.
At the end of the day, the command I used for the whole day stopped working.
user#ubuntu-4:~$ mkdir test
user#ubuntu-4:~$ cd test/
user#ubuntu-4:~/test$ touch -t 201601180830 yesterdayMorning
user#ubuntu-4:~/test$ touch -t 201601181725 yesterdayAfternoon
user#ubuntu-4:~/test$ ll
total 32
drwxrwxr-x 2 user user 4096 Jan 19 09:37 ./
drwx------ 9 user user 12288 Jan 19 09:36 ../
-rw-rw-r-- 1 user user 0 Jan 18 17:25 yesterdayAfternoon
-rw-rw-r-- 1 user user 0 Jan 18 08:30 yesterdayMorning
The result of FIND -mtime n
user#ubuntu-4:~/test$ find -mtime +1
user#ubuntu-4:~/test$ find -mtime -1
.
./yesterdayAfternoon
user#ubuntu-4:~/test$ find -mtime 0
.
./yesterdayAfternoon
user#ubuntu-4:~/test$
I should be able to find the file named yesterdayMorning because at the time I'm writing (09:48 am of 19 january) that file is older than 1 day.
find -mtime -1 (or 0 too) show the correct result because the file's last modification is less than 24 hours.
And yesterday before 05.00 pm I swear it was working!
It's actually not 24 hours ago but more than n days ago. I.e. for -mtime +1 it would have to be modified two days ago.
Use find -mtime +0 to match also yesterday's files.
As stated in the accepted answer -mtime +0 will work for you in this case.
Note:
find using -mtime and -daystart
-mtime n
File's data was last modified n*24 hours ago.
-daystart
Measure times (for -amin, -atime, -cmin, -ctime, -mmin, and
-mtime) from the beginning of today rather than from 24 hours
ago.
This option only affects tests which appear later on the
command line.
date
Tue Jan 19 10:24:43 CET 2016
~/test $ ls -n
total 0
-rw-r--r-- 1 1000 1000 0 Jan 18 10:15 yesterdayMorning10:15.txt
-rw-r--r-- 1 1000 1000 0 Jan 18 10:45 yesterdayMorning10:45.txt
~/test $ find -mtime +0
./yesterdayMorning10:15.txt
~/test $ find -mtime 0
./yesterdayMorning10:45.txt
~/test $ find -daystart -mtime +0
./yesterdayMorning10:15.txt
./yesterdayMorning10:45.txt

Need to remove files with spaces in Debian

I need to find and remove files with spaces in them in a certain folder.
$ ls -l
total 16
-rw-r--r-- 1 smw staff 10 Feb 6 16:10 Foo Bar
-rw-r--r-- 1 smw staff 11 Feb 6 16:10 foobar
$ ls -l *\ *
-rw-r--r-- 1 smw staff 10 Feb 6 16:10 Foo Bar
$ rm -i *\ *
remove Foo Bar? y
$ ls -l
total 16
-rw-r--r-- 1 smw staff 11 Feb 6 16:10 foobar
You'll have to deal with bash's niceties when dealing with spaces...
First, you need to iterate over the files, in a way that gives you the files properly regardless of spaces. Check out this question. I'd favor this:
find ... | while read line ; do command "$line" ; done
And then it's a matter of using something like sed to change $line into whatever you need (such as the same thing without spaces) right where command "$line" is.
This is how I removed a file with a space
pi#raspberrypi ~/Music $ ls -l
-rw-r--r-- 1 pi pi 0 Feb 25 16:05 Sleep Away.mp3
pi#raspberrypi ~/Music $ rm Sleep\ Away.mp3
use the "\" forward slash to escape any spaces

Problem running Makefile that calls Perl script

I'm trying to compile a C++ software a month ago.
There is a lot of pressure to make this work as soon as possible.
I've been looking for similar problems but I'm getting more and more confused.
I'm using the following:
bcmsa#braw176 ~/nba >uname -a
SunOS braw176 5.8 Generic_108528-13 sun4u sparc SUNW,Ultra-5_10
bcmsa#braw176 ~/nba >make -v
make -v
GNU Make version 3.79.1, by Richard Stallman and Roland McGrath.
Built for sparc-sun-solaris2.8
Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 2000
Free Software Foundation, Inc.
I executed the command:
bcmsa#braw176 ~/nba/DLPRTER_CAA/bin/RPG3/default >make -dwp all
But I'm receiving the following:
Reaping winning child 0x0006da38 PID 27586
/bin/sh: /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts/sigunion.pl: not found
Live child 0x0006da38 (/export/home/bcmsa/nba/DLPRTER_CAA/bin/RPG3/default/sigunion.h) PID 27588
Got a SIGCHLD; 1 unreaped children.
Reaping losing child 0x0006da38 PID 27588
make: *** [/export/home/bcmsa/nba/DLPRTER_CAA/bin/RPG3/default/sigunion.h] Error 1
Removing child 0x0006da38 PID 27588 from chain.
The crazy thing is that sigunion.pl is stored at the indicated directory:
bcmsa#braw176 ~/nba >cd /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts/
bcmsa#braw176 /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts >ls -la
total 120
drwxrwsr-x 2 80422 3626 4096 Jun 12 2002 .
drwxrwsr-x 3 80422 3626 4096 Jun 12 2002 ..
-r-xr-xr-x 1 80422 3626 2195 Jun 28 1999 bdt_c.sh
-r-xr-xr-x 1 80422 3626 2449 Mar 8 1999 bdt_h.sh
-r-xr-xr-x 1 80422 3626 681 Oct 20 1999 change_base
-r-xr-xr-x 1 80422 3626 990 Oct 20 1999 convert_to_iog11
-r-xr-xr-x 1 80422 3626 692 Oct 20 1999 create_directory
-r-xr-xr-x 1 80422 3626 604 Oct 20 1999 create_elements
-r--r--r-- 1 80422 3626 582 Mar 2 1999 create_elements.base
-r-xr-xr-x 1 80422 3626 2059 Dec 13 2001 generate_signal_files.csh
-r-xr-xr-x 1 80422 3626 486 Oct 20 1999 generate_version_info
-r-xr-xr-x 1 80422 3626 610 Oct 20 1999 get_program.csh
-r-xr-xr-x 1 80422 3626 760 Oct 20 1999 get_suid.csh
-r-xr-xr-x 1 80422 3626 774 Oct 20 1999 set_autostart.pl
-r-xr-xr-x 1 80422 3626 1555 Oct 20 1999 sigunion.pl
Please, help me find how to fix this fault.
I really don't know what else to do.
I tried to execute the following just to see what happens:
bcmsa#braw176 /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts >perl sigunion.pl
Can't exec /usr/atria/bin/Perl at sigunion.pl line 1.
Info:
bcmsa#braw176 /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts >which perl
/usr/bin/perl
bcmsa#braw176 /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts >perl -v
This is perl, version 5.005_03 built for sun4-solaris
bcmsa#braw176 /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts >head sigunion.pl
#! /usr/atria/bin/Perl
I strongly suspect that the shebang in sigunion.pl (aka the #! line) is pointing to the wrong place for your perl executable.
head sigunion.pl will show you where sigunion.pl thinks perl is, and which perl will show you where perl is actually installed.
Example:
$ head sigunion.pl
#!/usr/bin/perl
.....stuff omited
$ which perl
/usr/opt/perl-5.8.8/bin/perl
/bin/sh: /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts/sigunion.pl: not found
...indicates that /bin/sh could not figure out how to run the script sigunion.pl. It is probably missing a shebang line:
#!/usr/bin/perl
...which tells the shell what external program (in this case, perl) to parse and run the file.
Alternatively, you can just let perl be found in the $PATH, by changing the appropriate line in the Makefile to:
perl /aps/APS40/RPG3_R4A/lib/cmtool/plugin/rpg3.R3B/tools/tools/scripts/sigunion.pl <arguments>

How can I test when a feature was added to Perl?

Are there any services similar to codepad that will allow you to test Perl constructs on old versions of perl?
Ideally, a system where you could enter an expression and it will tell you the oldest version of perl that it will work with.
Of course it's possible to use CPANTS for this, but that seems like an abuse of the service (if only for making the BackPan bigger). And it could take several days/weeks to get decent test coverage on old versions.
You might find Perl::MinimumVersion helpful. Use the provided perlver utility to scan your code.
I think that if I was concerned, I'd build salient versions of Perl on my machine and test them. Actually, that's more or less what I do anyway. My Solaris 10 machine has:
lrwxrwxrwx 1 jleffler rd 26 Mar 6 2008 v5.10.0 -> v5.10.0-32bit-multiplicity
drwxr-xr-x 3 jleffler rd 512 Jan 10 2008 v5.10.0-32bit
drwxr-xr-x 6 jleffler rd 512 Mar 7 2008 v5.10.0-32bit-multiplicity
drwxr-xr-x 3 jleffler rd 512 Jan 10 2008 v5.10.0-64bit
lrwxrwxrwx 1 jleffler rd 13 Jan 29 21:07 v5.10.1 -> v5.10.1-64bit
drwxr-xr-x 6 jleffler rd 512 Jan 29 21:43 v5.10.1-64bit
drwxr-xr-x 5 jleffler rd 512 May 6 2003 v5.5.3
lrwxrwxrwx 1 jleffler RAND 11 Mar 21 2007 v5.6.1 -> v5.6.1-full
drwxr-xr-x 5 jleffler rd 512 May 6 2003 v5.6.1-full
drwxr-xr-x 5 jleffler rd 512 Feb 5 2008 v5.8.7-multi
lrwxrwxrwx 1 jleffler rd 19 Mar 24 2007 v5.8.8 -> v5.8.8-32bit-sun-cc
drwxr-xr-x 6 jleffler rd 512 Mar 25 2007 v5.8.8-32bit-sun-cc
drwxr-xr-x 5 jleffler rd 512 Feb 13 2006 v5.8.8-64bit-thread-multi
drwxr-xr-x 3 jleffler rd 512 Mar 20 2008 v5.8.8-gcc-3.4.6
So, that's 5.5.3, 5.6.1, 5.8.7, 5.8.8, 5.10.0 and 5.10.1 installed; I have the source for other versions too:
-rw-r--r-- 1 jleffler rd 2171936 Apr 12 2001 perl-5.004_04.tar.bz2
-rw-r--r-- 1 jleffler rd 3023320 Aug 31 1999 perl-5.005_03.tar.bz2
-rw-r--r-- 1 jleffler rd 12426022 Dec 18 2007 perl-5.10.0.tar.bz2
-rw-r--r-- 1 jleffler rd 11608061 Jan 29 12:32 perl-5.10.1.tar.bz2
-rw-r--r-- 1 jleffler rd 4430438 Mar 29 2000 perl-5.6.0.tar.bz2
-rw-r--r-- 1 jleffler rd 4864306 Apr 8 2001 perl-5.6.1.tar.bz2
-rw-r--r-- 1 jleffler rd 5142605 Aug 16 2005 perl-5.6.2.tar.bz2
-rw-r--r-- 1 jleffler rd 8618487 Jul 18 2002 perl-5.8.0.tar.bz2
-rw------- 1 jleffler rd 9410641 Sep 29 2003 perl-5.8.1.tar.bz2
-rw-r--r-- 1 jleffler rd 9424944 Nov 5 2003 perl-5.8.2.tar.bz2
-rw-r--r-- 1 jleffler rd 9509716 Jan 14 2004 perl-5.8.3.tar.bz2
-rw-r--r-- 1 jleffler rd 9598489 Apr 21 2004 perl-5.8.4.tar.bz2
-rw-r--r-- 1 jleffler rd 9464689 Jul 19 2004 perl-5.8.5.tar.bz2
-rw-r--r-- 1 jleffler rd 9693085 Nov 27 2004 perl-5.8.6.tar.bz2
-rw-r--r-- 1 jleffler rd 9839086 Jun 15 2005 perl-5.8.7.tar.bz2
-rw-r--r-- 1 jleffler rd 10123359 Feb 13 2006 perl-5.8.8.tar.bz2
-rw-r--r-- 1 jleffler rd 11121414 Dec 14 2008 perl-5.8.9.tar.bz2
#Ether asked:
Do you have a script which will run a particular module, script or unit test against all versions in sequence and collate the results? Such a utility might be really handy as a chrooted/sandboxed CGI.
No, but I haven't needed it. It's basically trivially, though:
for perl in /usr/perl/v5.*.?
do
echo $(basename $perl)
$perl/bin/perl "$#"
done
Basically, for each of the Perl directories in /usr/perl/, run the perl from the bin directory on the given set of arguments. Watch the output...
The difficulty is in deciding what constitutes pass/fail. Obviously, the core of the loop could be:
if $perl/bin/perl "$#" >/dev/null 2>&1
then echo ok $perl
else echo not ok $perl
fi
That's faintly similar to the TAP output. To make it formally equivalent (using bash or Korn shell):
test=0
max=$(ls -d /usr/perl/v5.*.? | wc -l | sed 's/ //g')
echo 1..$max
for perl in /usr/perl/v5.*.?
do
((test = test + 1))
if $perl/bin/perl "$#" >/dev/null 2>&1
then echo ok $test - $perl
else echo not ok $test - $perl
fi
done
Here's an example of running it:
$ ksh test.perl -e 'exit 0'
1..6
ok 1 - /usr/perl/v5.10.0
ok 2 - /usr/perl/v5.10.1
ok 3 - /usr/perl/v5.5.3
ok 4 - /usr/perl/v5.6.1
ok 5 - /usr/perl/v5.8.8
not ok 6 - /usr/perl/v5.8.8-gcc-3.4.6
$
That shows a limitation in shell scripts and their globbing facilities (I'd like to limit the 'star' to a series of digits). The Perl that fails does so because the bin directory doesn't contain a copy of Perl; I needed to save space at some point! It would not be hard to convert the shell script into a Perl script, of course.
If you're looking for when a module was introduced to core Perl, you can use Module::CoreList.
If you'd like to help develop a system like codepad for Perl, search for "PITA testing".
Specifically, if you want a utility to download and install a number of Perl binaries to test your code, you can look at the programs in PITA-Setup-Perl/bin.
I know perlcritic is capable of finding a few cases, but in general you will have to look it up in google or in perldelta files. In particular, you may want to look at the deltas for 5.6 5.8 and 5.10 and maybe even the upcoming 5.12