0

I have a file

instrument.xml

bash-4.1$ more instrument.xml
<instr instr_id="CPT-2380950-011-001-826" valor_ch="" instr_type="CurrentAccount" instr_name="CPT-2380950-011-001-826" qn_type="1" ccy_code="GBP" cnt_code="CH" date_issue="2002-05-29 00:00:00" int_rate="0.08000000" eusd_scope="0" cmnt="CRD" status="1"/>
<instr instr_id="VAL-0000001-000" isin="0000001-000 " valor_ch="0000001-000" instr_type="Share" instr_name="******ON" qn_type="1" ccy_code="999" cnt_code="XX" int_rate="0.00000000" eusd_scope="0" cmnt="200" status="1"/>
<instr instr_id="CPT-2411550-011-000-826" valor_ch="" instr_type="CurrentAccount" instr_name="CPT-2411550-011-000-826" qn_type="1" ccy_code="GBP" cnt_code="CH" date_issue="2002-08-02 00:00:00" int_rate="0.08000000" eusd_scope="0" cmnt="CRD" status="1"/>

Requirement: I need to change [date time] eg 2002-05-29 00:00:00 to [dateTtime] eg2002-05-29T00:00:00 anywhere in the file. --Some lines wont have date in them so i need to search for date pattern and then do my change --and the columns vary in length sed 13 was a very amateur attempt looking for the 13th space(please neglect)

My futile attempts:

sed "s/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/&T/" instrument.xml
grep "[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]" test.xml | sed 's/ //13' >instrument.xml ( it doesn’t give me all )

sed "s/[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]/&T~+/" instrument.xml |  tr "~" "\n" | sed 's/^+./+/g' ( unable to join only lines which have + in them)
2
  • Please clean up you question. (0) Look at what your question looks like. Then look at Markdown Editing Help and try to make your question readable. (1) Is your file called test.xml, test.txt, or instrument.xml? (2) Do you need to change 2002-05-29 00:00:00 to 2002-05-29T00:00:00 anywhere in the file, or do you need to change space to T in any date/time string of the indicated format? Commented Feb 20, 2015 at 3:26
  • (3) Can you solve it yourself if the lines in in your file are < 80 characters long? Is the length of the lines part of the problem? If not, then there's no reason to blast us with your 250-character-long lines. (4) Where does the 13 in s/ //13 come from? (5) Research regular expressions, specifically sub-expressions. Commented Feb 20, 2015 at 3:26

1 Answer 1

0
sed "s/\($(printf "[0-9][0-9]%s" '' - - '\) \(' : : '')\)/\1T\2/g"

Maybe try building out your pattern programmatically. That can sometimes make it a little easier to nail down the very specific types.

The above expands to...

sed 's/\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\) \([0-9][0-9]:[0-9][0-9]:[0-9][0-9]\)/\1T\2/g'

(but without the single-quotes)

7
  • I hope that the pattern is very long to be misfound so as for me it can be substituted sed 's/\("[0-9-]\{10\}\) \([0-9:]\{8\}"\)/\1T\2/g' Commented Feb 20, 2015 at 9:45
  • @Costas, but yours also gets ---------- :::::::: Commented Feb 20, 2015 at 14:18
  • perl -pe 's/("\d{4}-\d{2}-\d{2}) (\d{2}:\d{2}:\d{2}")/$1T$2/g' Commented Feb 20, 2015 at 14:20
  • @JJao ... what is that for? Commented Feb 20, 2015 at 14:24
  • @mikeserv Yes, but I am sure that such patterns there are not in input file. Commented Feb 20, 2015 at 17:48

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.