I am getting a file from database using shell script. Data looks like below after running shell script.This is a pipe separated data file.


After generating the file using spool, below is the command that I am running to format the data.

sed -i -E 's/[[:blank:]]+(||$)/\1/g' Test.txt.  

but the problem is date data

2017-09-2900:00:00, I need this like 2017-09-29 00:00:00

I need space between date and time. How this can be done?

  • can you post the initial few lines before sed processing? – RomanPerekhrest Oct 2 '17 at 17:55
  • if [ -f $HOME/data/feed/$1.txt ]; then var=$header_info sed -i "1s/.*/$var/" $1.txt sed -i '$d' $1.txt sed -i 's/ *,/,/g' $1.txt sed -i -E 's/[[:blank:]]+(||$)/\1/g' $1.txt sed -i 's/^[[:blank:]]*$//' $1.txt – unknown Oct 2 '17 at 18:42
  • That code belongs in your question where we can see it in context. Not posted as a comment where it can be overlooked. Please edit your question to include it. – roaima Oct 2 '17 at 22:21

Just do.

sed 's/....-..-../& /' infile

Assuming there is always one pattern can match with xxxx-xx-xx format.

Or at first in your SQL you need to tell substr in select command to do this job to you.

select ... '|' || substr(mydate,1,10) || subtsr(mydate,11,8) || '|'  ...
  • 3
    That's a novel solution, using Morse code :-) – Kusalananda Oct 2 '17 at 18:10
$ sed -E 's/([0-9]{2})([0-9]{2}):/\1 \2:/' file
4676004||2017-09-29 00:00:00|13222|0|XYZ|TTTTT||1855467|S|14671

This is assuming that the only place in the input line where four digits occur before a : is in the date string. The sed substitution separates the two groups of two digits with a space.

  • I got the required solution..Thanks for your help.. – unknown Oct 2 '17 at 19:16
  • 1
    @unknown In that case, if you're happy with one or several of the answers, upvote them. If one is solving your issue, accepting it would be the best way of saying "Thank You!" :-) – Kusalananda Oct 2 '17 at 19:24
  • 1
    And to elaborate a bit on @Kusalananda's comment, accepting an answer is not only for saying "Thank you": it indicates to future visitors that the answer solved the problem. That's what makes SE useful (well, at least one of the things that makes SE useful). – NickD Oct 2 '17 at 19:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.