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.

  • 1
    is the number of _also fixed for first column string? – Sundeep Oct 16 '17 at 5:39
  • is your input a string or a file? – RomanPerekhrest Oct 16 '17 at 7:11
  • _ is just one. input is a line in a file – Roopak Vasa Oct 17 '17 at 6:48
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.

  • Thanks. That is exactly what I was looking for. simple solution. – Roopak Vasa Oct 17 '17 at 6:50
5

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
  • Thanks. That is exactly what I was looking for but sed seems to bit more easy with syntax ! – Roopak Vasa Oct 17 '17 at 6:51

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.