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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How can I find "-" ni the string variable?
# 1  
Old 03-01-2017
How can I find "-" ni the string variable?

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.

Last edited by rbatte1; 03-01-2017 at 11:45 AM.. Reason: Added ICODE tags
# 2  
Old 03-01-2017
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
# 3  
Old 03-01-2017
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 substitute 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.
# 4  
Old 03-01-2017
input
Code:
 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
Code:
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
Code:
if [ "${dash}" = "-" ]
then
        dt1=`echo ${dt} | sed 's/-//'` #don't need to put {} around dt1
        echo "DATE NOW IS ${dt1}"
fi


Moderator's Comments:
Mod Comment Seriously: PLEASE use CODE tags as required by forum rules!

Last edited by RudiC; 03-01-2017 at 12:20 PM.. Reason: Added CODE tags.
# 5  
Old 03-01-2017
Why don't you use your shell's "parameter expansion/pattern replacement"?
Code:
echo "$dt" "${dt//-}" 
20170301-171628 20170301171628

# 6  
Old 03-01-2017
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


Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 03-01-2017 at 12:37 PM.. Reason: Added CODE tags.
# 7  
Old 03-01-2017
Do you mean "Bourne shell" (= /bin/sh)?

How about reading the man page and applying some creativity, like
Code:
echo ${dt%-*}${dt#*-}
20170301173320

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

find . -path "*_nobackup*" -prune -iname "*.PDF" \( ! -name "*_nobackup.*" \)

These three finds worked as expected: $ find . -iname "*.PDF" $ find . -iname "*.PDF" \( ! -name "*_nobackup.*" \) $ find . -path "*_nobackup*" -prune -iname "*.PDF" They all returned the match: ./folder/file.pdf :b: This find returned no matches: $ find . -path "*_nobackup*" -prune... (3 Replies)
Discussion started by: wolfv
3 Replies

2. Shell Programming and Scripting

Delete all log files older than 10 day and whose first string of the first line is "MSH" or "<?xml"

Dear Ladies & Gents, I have a requirement to delete all the log files in /var/log/test directory that are older than 10 days and their first line begin with "MSH" or "<?xml" or "FHS". I've put together the following BASH script, but it's erroring out: for filename in $(find /var/log/test... (2 Replies)
Discussion started by: Hiroshi
2 Replies

3. UNIX for Dummies Questions & Answers

Find a string across line break (because of "segmentation fault core dumped")

Hi, thanks to a precedent post, and thanks to the reply of derekludwig of the forum, I have convert my first awk command as : test.txt is : AAAAAGHIJKLAjKMEFJKLjklABCDJkLEFGHIJKL awk -f findstring.awk test.txt > textreturn.txtfindstring.awk is : BEGIN{ SLENGTH = 3 } { ... (3 Replies)
Discussion started by: thewizarde6
3 Replies

4. Shell Programming and Scripting

Find lines with "A" then change "E" to "X" same line

I have a bunch of random character lines like ABCEDFG. I want to find all lines with "A" and then change any "E" to "X" in the same line. ALL lines with "A" will have an "X" somewhere in it. I have tried sed awk and vi editor. I get close, not quite there. I know someone has already solved this... (10 Replies)
Discussion started by: nightwatchrenba
10 Replies

5. Shell Programming and Scripting

grep with "[" and "]" and "dot" within the search string

Hello. Following recommendations for one of my threads, this is working perfectly : #!/bin/bash CNT=$( grep -c -e "some text 1" -e "some text 2" -e "some text 3" "/tmp/log_file.txt" ) Now I need a grep success for some thing like : #!/bin/bash CNT=$( grep -c -e "some text_1... (4 Replies)
Discussion started by: jcdole
4 Replies

6. Shell Programming and Scripting

how to use "cut" or "awk" or "sed" to remove a string

logs: "/home/abc/public_html/index.php" "/home/abc/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" "/home/xyz/public_html/index.php" how to use "cut" or "awk" or "sed" to get the following result: abc abc xyz xyz xyz (8 Replies)
Discussion started by: timmywong
8 Replies

7. Shell Programming and Scripting

Using sed to find text between a "string " and character ","

Hello everyone Sorry I have to add another sed question. I am searching a log file and need only the first 2 occurances of text which comes after (note the space) "string " and before a ",". I have tried sed -n 's/.*string \(*\),.*/\1/p' filewith some, but limited success. This gives out all... (10 Replies)
Discussion started by: haggismn
10 Replies

8. Shell Programming and Scripting

sed - extract string before "/" from variable- linux 2.6.9-89

Hi, I have a FTP script which gets called from a wrapper script that exports a variable having value as: "/export/home/dips/logs/dipsSFTP_file1.log.YYYYMMDDHHMISS". I want to extract the file name "dipsSFTP_file1.log.YYYYMMDDHHMISS" and the dir path "/export/home/dips/logs/" from this... (4 Replies)
Discussion started by: dips_ag
4 Replies

9. Shell Programming and Scripting

"find command" to find the files in the current directories but not in the "subdir"

Dear friends, please tell me how to find the files which are existing in the current directory, but it sholud not search in the sub directories.. it is like this, current directory contains file1, file2, file3, dir1, dir2 and dir1 conatins file4, file5 and dir2 contains file6,... (9 Replies)
Discussion started by: swamymns
9 Replies

10. Shell Programming and Scripting

How to get Find command work with a variable passing "*" value?

Hi guy, I have a problem to pass a variable containing '*' value to FIND command. below is the script. It doesn't work by submit below command: rmf.sh name '*.txt' or rmf.sh name *.txt I've tried either optn="-name '$2'" or optn="-name $2"., and there is no luck. ### (script... (5 Replies)
Discussion started by: unxuser
5 Replies
Login or Register to Ask a Question