删除特定单词之间的空格

问题描述 投票:0回答:1

我有以下输出

ssh-rsa aaaaaaaaaaa bbbbbbbbbbbbb cccccccccccccc ddddddddddddddddd eeeeeeeeeeeeeeee mail: [email protected]

我想删除 ssh-rsa 和邮件之间的所有空格:

所以我会得到这个结果

ssh-rsa aaaaaaaaaaabbbbbbbbbbbbbccccccccccccccdddddddddddddddddeeeeeeeeeeeeeeee mail: [email protected]
shell
1个回答
0
投票

请参阅代码中提供的说明以了解它是如何完成的:


#!/bin/bash
# Input string
input="ssh-rsa aaaaaaaaaaa bbbbbbbbbbbbb cccccccccccccc ddddddddddddddddd eeeeeeeeeeeeeeee mail: [email protected]"
# Split the input into an array
IFS=' ' read -r -a array <<< "$input"
# Initialize the output with the first element
output="${array[0]} "
# Join all elements except the last two
for ((i = 1; i < ${#array[@]} - 2; i++)); do
    output+="${array[i]}"
done
# Append the last two elements with a space in between
output+=" ${array[-2]} ${array[-1]}"
# Print the result
echo "$output"
© www.soinside.com 2019 - 2024. All rights reserved.