0

I have string like

990202-1711-BBVRS390-T01
415715-1611-EDUE818C,1711-EDUE818C
415715-1611-EDUE818C-,1711-EDUE818C-T01

I want to replace all , with newline and first 7 character so that the output is

990202-1711-BBVRS390-T01
415715-1611-EDUE818C
415715-1711-EDUE818C
415715-1611-EDUE818C-
415715-1711-EDUE818C-T01
codeforester
  • 39,467
  • 16
  • 112
  • 140
  • 1
    Welcome to StackOverflow! You may want to take a look at: [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – codeforester Jun 16 '17 at 05:34
  • @Angus Chiang -- Please check https://stackoverflow.com/help/someone-answers – P.... Jun 16 '17 at 11:29

4 Answers4

1

Here is awk apporach:

awk '{firstSeven=substr($0,1,7);gsub(/,/,RS firstSeven)}1' inputfile
990202-1711-BBVRS390-T01
415715-1611-EDUE818C
415715-1711-EDUE818C
415715-1611-EDUE818C-
415715-1711-EDUE818C-T01

Exaplanation:

substr function : This function is used to get the first seven characters.From 1st character to 7th character.
gsub function: This function is used to replace a comma with New line followed by firstSeven Characters. Here RS is used to get new line as the default vale of RS is new line.

To do the changes inside the file use -inplace option of awk ,if it is not available :

awk '{firstSeven=substr($0,1,7);gsub(/,/,RS firstSeven)}1' inputfile >inputfile.tmp && mv inputfile.tmp inputfile
P....
  • 17,421
  • 2
  • 32
  • 52
  • The only (?) problem with that is it'd fail if the replacement string contained backreferences, e.g. `&`. Doesn't LOOK like that can occur for the OPs case though. – Ed Morton Jun 16 '17 at 23:29
0

Use sed:

sed -E 's/^(.{7})([^,]+),(.*)$/\1\2\'$'\n''\1\3/' file

Output:

990202-1711-BBVRS390-T01
415715-1611-EDUE818C
415715-1711-EDUE818C
415715-1611-EDUE818C-
415715-1711-EDUE818C-T01

See also:

codeforester
  • 39,467
  • 16
  • 112
  • 140
0

using sed:

sed 's/\(.\{7\}\)\([^,]*\),\(.*\)/\1\2\n\1\3/g' filename

in regex . means every character;
\(.\{7\}\) - with that i mark first 7 characters.
\([^,]*\) - with that i mark from 8th character part of line that not containing comma.
,\(.*\) - with that there will be comma and with .* i mark all other characters from after comma to end of line.
\1\2 - and backslash and number i can use marked part of line (first marked will be \1, second \2 and so on.).

tso
  • 4,732
  • 2
  • 22
  • 32
  • Thank you for this code snippet, which may provide some immediate help. A proper explanation [would greatly improve](//meta.stackexchange.com/q/114762) its educational value by showing *why* this is a good solution to the problem, and would make it more useful to future readers with similar, but not identical, questions. Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Jun 23 '17 at 11:32
0
$ awk -F',' '{pfx=substr($0,1,7); $1=substr($1,8); for (i=1;i<=NF;i++) print pfx $i}' file
990202-1711-BBVRS390-T01
415715-1611-EDUE818C
415715-1711-EDUE818C
415715-1611-EDUE818C-
415715-1711-EDUE818C-T01
Ed Morton
  • 188,023
  • 17
  • 78
  • 185