1

I have a file which has many entries as below :

US6DWMD01#DW01DDATAPURGE(060009/28)
US6DWMD01#DW01DDATAPURGE(060009/29)
US6DWMD01#DW01DDATAPURGE(060009/30)
US6DWMD01#DW01DDATAPURGE(060011/01)
US6DWMD01#DW11WPURESUN(060011/01)
US6TPA01#PPAORD__LDBASE(000009/26)
US6TPA01#PPAORD__LGBOX(000009/26)
US6TPA01#PPATDD__DEDMGT(060009/25)
US6TPA01#PPATDD__FLNET(060009/25)
US6TPA01#PPATDD__LORTBLS(060009/25)
US6TPA01#PPATDD__PPATTBLS(060009/25)
US6TPA01#PPATDD__P8020RP(060011/01)

I want use cut/sed/awk commands to insert space after 5 characters when counting reverse as below :

US6TPA01#PPATDD__DEDMGT(0600 09/25)
US6TPA01#PPATDD__FLNET(0600 09/25)
US6TPA01#PPATDD__LORTBLS(0600 09/25)
US6TPA01#PPATDD__PPATTBLS(0600 09/25)
US6TPA01#PPATDD__P8020RP(0600 11/01)
3

There are 6 character, in fact: 2 digits, 1 slash, 2 digits, 1 parenthesis:

sed 's/.\{6\}$/ &/' < input > output

The $ matches the end of line.

2

In regular expressions, $ matches the end of the line. You can therefore use it to match the last 6 (your question mentions 5 but you show 6, unless there is a trailing space you aren't showing us) characters and add a space before them. For example:

  • Sed:

    sed 's/......$/ &/' file
    

    or

    sed 's/.\{6\}$/ &/' file
    
  • GNU or FreeBSD sed:

    sed -E 's/.{6}$/ &/' file
    
  • Perl

    perl -pe 's/.{6}$/ $&/' file
    
  • awk

    awk '{sub(/.{6}$/," &");print}' file
    
  • perl -pe 'substr $_, -7, 0, " "' – choroba Nov 2 '15 at 16:20
  • The command perl -pe 'substr $_, -7, 0, " "' worked well , but not others. – sriram2207 Nov 2 '15 at 16:51
  • @sriram2207 how did they fail? I tested all of them and they work as expected on your example file. – terdon Nov 2 '15 at 18:23

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.