2

Input:

IN22FDX_S1P_WWWWBBBBMMMCCCC_ffg_pre_0.59_0_.72_-40_nominal

Output:

IN22FDX_S1P_WWWWBBBBMMMCCCC ffg_pre_0.59_0_.72_-40_nominal
#                          ^
#                space instead of underscore

How to convert the above line to two columns (remove one underscore between them)? The first word is of fixed length IN22FDX_S1P_WWWWBBBBMMMCCCC.

3
  • 1
    is the number of _also fixed for first column string?
    – Sundeep
    Oct 16, 2017 at 5:39
  • is your input a string or a file? Oct 16, 2017 at 7:11
  • _ is just one. input is a line in a file Oct 17, 2017 at 6:48

2 Answers 2

15

If the first column has a fixed size,

sed 's/./ /28'

replaces the 28th char with a space.

Assuming the data is stored in a file, you can edit it in place with sed.

sed -i.bak 's/./ /28' input_file

Note the .bak I added to the -i option; it instructs sed to make a backup file input_file.bak with the provided suffix. The suffix is optional.

1
  • Thanks. That is exactly what I was looking for. simple solution. Oct 17, 2017 at 6:50
5

GNU cut solution:

s="IN22FDX_S1P_WWWWBBBBMMMCCCC_ffg_pre_0.59_0_.72_-40_nominal"
cut -c 1-27,29- --output-delimiter=' ' <<<"$s"
  • -c - to select characters

The output:

IN22FDX_S1P_WWWWBBBBMMMCCCC ffg_pre_0.59_0_.72_-40_nominal
1
  • Thanks. That is exactly what I was looking for but sed seems to bit more easy with syntax ! Oct 17, 2017 at 6:51

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .