I have 100 strings, which have YYYYDDMMHHMMSS in it and only one is YYYYMMDD-HHMMSS .
I want to find that dash and replace it. If I check each string, using sed 's/-//g' , it shows me warning that - is not found.
So I need if [ - found ];then sed 's/-//g' , but I cannot find correct regular expression to find - in if statement.
Can somebody help me?
I am working on AIX and using Korn, c, or borh shell.
Can you show us some sample input and the output from your attempts (wrapped in CODE tags) so we can see what you are getting?
Thanks,
Robin
It is very difficult to believe that
-
sed 's/-//g'should NOT replace any dashes found in a file (unless a similar but different locale character were used), -
sedwould show an error message anywhere like something "is not found" in asubstitute command,
provided that the data are supplied in a file or a pipe.
If the data are shell variables,sedwere inappropriate; use parameter expansion instead.
input
dt=`date '+%Y%m%d-%H%M%S'`
echo "${dt}" =#20170301-110352
dash=`echo ${dt} | cut -c 9`
echo "dash ${dash}" # "-"
if [ "${dash}" = "-" ]
then
${dt1}=`echo ${dt} | sed 's/-//g'`
echo "DATE NOW IS ${dt1}"
fi
It gives the error
script[18]: =20170301110352: not found
DATE NOW IS
What is wrong?
---------- Post updated at 11:10 AM ---------- Previous update was at 11:07 AM ----------
RudiC,
I don't have file. I have variable, where is
20170301-110352 and I wont to find it and replace with //, i.e. I need to have timestamp like 20170301110352
---------- Post updated at 11:14 AM ---------- Previous update was at 11:10 AM ----------
Sorry, I found my error
below is working fine
if [ "${dash}" = "-" ]
then
dt1=`echo ${dt} | sed 's/-//'` #don't need to put {} around dt1
echo "DATE NOW IS ${dt1}"
fi
Why don't you use your shell's "parameter expansion/pattern replacement"?
echo "$dt" "${dt//-}"
20170301-171628 20170301171628
I work on a very old version of UNIX - AIX. And there is a mess. Seems like we use c or korn shell, but in reality it is born shell.
Your statement gives me "${dt//-}": bad substitution always
Do you mean "Bourne shell" (= /bin/sh )?
How about reading the man page and applying some creativity, like
echo ${dt%-*}${dt#*-}
20170301173320
This works. What man page did you look into?
---------- Post updated at 11:51 AM ---------- Previous update was at 11:49 AM ----------
It is not working when I have YYYYMMDDHHMMSS in this case I will get 2017030111495720170301114957
I am telling you, I am working on 100 files, and only one has timestamp YYYYMMDD-HHMMSS. I am looping through all of them and must have if statement. Otherwise it will not work correctly
will this help?
sed 's/[-]//'
man sh . That's the man page part. What about the other part?
Nevertheless, try
echo ${dt%-*}${dt#${dt%-*}-}
20170301173320
The last one doesn't work if I have YYYYMMDDHHMMSS, in this case I will receive double timestamp YYYYMMDDHHMMSSYYYYMMDDHHMMSS
What page do you advice to open, to find above mentioned?
My fault; I tested with the wrong variable. Until a better alternative is available, we need to test for the dash. Try
[ ${dt%-*} = ${dt} ] && echo ${dt%-*}${dt#${dt%-*}} || echo ${dt%-*}${dt#${dt%-*}-}