# How can I find "-" ni the string variable?

**URL:** <https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327>\
**Category:** Shell Programming and Scripting\
**Created:** [March 1, 2017, 10:38am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327 "2017-03-01T10:38:01Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![digioleg54](https://community.unix.com/letter_avatar/digioleg54/32/6_449c533f586939b6e5f246632a46421f.png) [@digioleg54](https://community.unix.com/u/digioleg54)\
**Post date:** [March 1, 2017, 10:38am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/1 "2017-03-01T10:38:01Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![rbatte1](https://community.unix.com/letter_avatar/rbatte1/32/6_449c533f586939b6e5f246632a46421f.png) [@rbatte1](https://community.unix.com/u/rbatte1)\
**Post date:** [March 1, 2017, 10:46am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/2 "2017-03-01T10:46:36Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2017, 11:07am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/3 "2017-03-01T11:07:50Z")

</div>

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),
- `sed` would show an error message anywhere like something "is not found" in a `s` ubstitute command,  
provided that the data are supplied in a file or a pipe.  
If the data are shell variables, `sed` were inappropriate; use parameter expansion instead.

---

<div class="post-metadata">

**Author:** ![digioleg54](https://community.unix.com/letter_avatar/digioleg54/32/6_449c533f586939b6e5f246632a46421f.png) [@digioleg54](https://community.unix.com/u/digioleg54)\
**Post date:** [March 1, 2017, 11:14am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/4 "2017-03-01T11:14:47Z")

</div>

input

```nohighlight
 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

```nohighlight
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

```nohighlight
if ["${dash}" = "-"]
then
        dt1=`echo ${dt} | sed 's/-//'` #don't need to put {} around dt1
        echo "DATE NOW IS ${dt1}"
fi

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2017, 11:18am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/5 "2017-03-01T11:18:51Z")

</div>

Why don't you use your shell's "parameter expansion/pattern replacement"?

```nohighlight
echo "$dt" "${dt//-}" 
20170301-171628 20170301171628

```

---

<div class="post-metadata">

**Author:** ![digioleg54](https://community.unix.com/letter_avatar/digioleg54/32/6_449c533f586939b6e5f246632a46421f.png) [@digioleg54](https://community.unix.com/u/digioleg54)\
**Post date:** [March 1, 2017, 11:28am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/6 "2017-03-01T11:28:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2017, 11:36am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/7 "2017-03-01T11:36:19Z")

</div>

Do you mean "Bourne shell" (= `/bin/sh` )?

How about reading the man page and applying some creativity, like

```nohighlight
echo ${dt%-*}${dt#*-}
20170301173320

```

---

<div class="post-metadata">

**Author:** ![digioleg54](https://community.unix.com/letter_avatar/digioleg54/32/6_449c533f586939b6e5f246632a46421f.png) [@digioleg54](https://community.unix.com/u/digioleg54)\
**Post date:** [March 1, 2017, 11:51am UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/8 "2017-03-01T11:51:52Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![vgersh99](https://community.unix.com/user_avatar/community.unix.com/vgersh99/32/14851_2.png) [@vgersh99](https://community.unix.com/u/vgersh99)\
**Post date:** [March 1, 2017, 12:04pm UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/9 "2017-03-01T12:04:01Z")

</div>

will this help?

```nohighlight
sed 's/[-]//'

```

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2017, 12:04pm UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/10 "2017-03-01T12:04:32Z")

</div>

`man sh` . That's the man page part. What about the other part?

Nevertheless, try

```nohighlight
echo ${dt%-*}${dt#${dt%-*}-}
20170301173320

```

---

<div class="post-metadata">

**Author:** ![digioleg54](https://community.unix.com/letter_avatar/digioleg54/32/6_449c533f586939b6e5f246632a46421f.png) [@digioleg54](https://community.unix.com/u/digioleg54)\
**Post date:** [March 1, 2017, 12:06pm UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/11 "2017-03-01T12:06:50Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![RudiC](https://community.unix.com/letter_avatar/rudic/32/6_449c533f586939b6e5f246632a46421f.png) [@RudiC](https://community.unix.com/u/RudiC)\
**Post date:** [March 1, 2017, 1:22pm UTC](https://community.unix.com/t/how-can-i-find-ni-the-string-variable/365327/12 "2017-03-01T13:22:57Z")

</div>

My fault; I tested with the wrong variable. Until a better alternative is available, we need to test for the dash. Try

```nohighlight
[${dt%-*} = ${dt}] && echo ${dt%-*}${dt#${dt%-*}} || echo ${dt%-*}${dt#${dt%-*}-}

```
