0

How to remove the spaces in 22th and 23th position of the specific string in a file?

Example below and expected outcome.

XXXSA3FFESS3052599004   L +

Expected result will be:

XXXS3DFFESS3052599004L +

My code seemd inaccurate as below. Please advise.

sed 's/[A-Z0-9]  //g' $file

Truly appreciate your help. I‘m not sure exactly the code. But my basic understanding is that sed could be able to do this.

Cyrus
  • 69,405
  • 13
  • 65
  • 117
Neo
  • 5
  • 3

3 Answers3

3
echo 'XXXSA3FFESS3052599004   L +' | sed 's/  *//'

Output:

XXXSA3FFESS3052599004L +

See: The Stack Overflow Regular Expressions FAQ

Cyrus
  • 69,405
  • 13
  • 65
  • 117
  • thanks cyrus, it works for this, but sorry I have not mentioned. that there are other strings inside the file. and it affect other strings with spaces that deleted them as well. It should match the pattern for this particular string. – Neo Jun 11 '17 at 08:59
  • 1
    Try this: `sed -r 's/^(.{21}) +(.*)/\1\2/'` – Cyrus Jun 11 '17 at 09:08
  • 1
    @Cyrus no need for `(.*)` or `\2`. – 123 Jun 11 '17 at 11:02
  • @123: Good catch. – Cyrus Jun 11 '17 at 11:55
  • @Neo if `sed 's/*//'` (use a literal blank instead of - the comment editor is deciding to change 2 consecutive blanks to 1 blank) isn't all you need then edit your question to show a more truly representative sample where doing that would not produce the desired output. Without truly representative input/output we're just guessing and so there's a good chance you're going to get a more complicate or more fragile solution than is possible. – Ed Morton Jun 11 '17 at 15:10
3

or like this:

echo 'XXXSA3FFESS3052599004  L +' | sed 's/.//22;s/.//22'
tso
  • 4,174
  • 2
  • 17
  • 30
1

If your spaces are in a fixed position, maybe it would make more sense to use cut:

echo 'XXXSA3FFESS3052599004  L +' | cut -c1-21,24-

or

echo 'XXXSA3FFESS3052599004  L +' | cut --complement -c22,23
liborm
  • 2,326
  • 16
  • 28