You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用sed将摩尔斯电码转文本时遇错误的技术咨询

Fixing Your Morse-to-Text Bash Script Error

Hey there! Let's sort out that sed error you're facing first, then tweak a few other parts to get your Morse code conversion working perfectly.

The Root Cause of the sed Error

The error sed: can't read s/.-/A/g: No such file or directory happens because you didn't prefix your first substitution command with -e. Sed is treating s/.-/A/g as a filename it needs to read instead of an edit command. For multi-command sed calls, every substitution needs to be preceded by -e, or you can bundle all commands into a single string separated by semicolons.

Fixed Script with Corrected Mappings

Here's the revised script with proper syntax and accurate Morse code mappings:

#!/bin/bash
sed -e 's/\.-/A/g' \
    -e 's/-.../B/g' \
    -e 's/-.-./C/g' \
    -e 's/-../D/g' \
    -e 's/\./E/g' \
    -e 's/..-./F/g' \
    -e 's/--./G/g' \
    -e 's/..../H/g' \
    -e 's/../I/g' \
    -e 's/.---/J/g' \
    -e 's/-.-/K/g' \
    -e 's/.-../L/g' \
    -e 's/--/M/g' \
    -e 's/-./N/g' \
    -e 's/---/O/g' \
    -e 's/.--./P/g' \
    -e 's/--.-/Q/g' \
    -e 's/.-./R/g' \
    -e 's/.../S/g' \
    -e 's/-/T/g' \
    -e 's/..-/U/g' \
    -e 's/...-/V/g' \
    -e 's/.--/W/g' \
    -e 's/-..-/X/g' \
    -e 's/-.--/Y/g' \
    -e 's/--../Z/g' \
    -e 's/.----/1/g' \
    -e 's/..---/2/g' \
    -e 's/...--/3/g' \
    -e 's/....-/4/g' \
    -e 's/...../5/g' \
    -e 's/-..../6/g' \
    -e 's/--.../7/g' \
    -e 's/---../8/g' \
    -e 's/----./9/g' \
    -e 's/-----/0/g' \
    Morse_Text.txt

Key Fixes Explained

  • Proper sed command structure: Every substitution now uses -e, so sed recognizes them as commands. I added backslashes (\) to split the script across lines—this is optional but makes the code way easier to read and edit.
  • Escaped the dot for E: The original s/./E/g would replace any character with E because . is a regex wildcard. Adding a backslash (\./) tells sed to match a literal dot.
  • Fixed G's mapping: You had an extra space in G's pattern (- -.); the correct Morse code for G is --., so I updated that line.
  • Corrected number mappings: Your original script had duplicate/reversed patterns for numbers 1-5. I fixed them to match standard Morse code conventions.

Testing with Your Sample Input

If your Morse_Text.txt contains -- --- .-. ... . -.-. --- -.-. . (I assumed the … was a typo for ...), running this script will output MORSE CODE—exactly what you want!

内容的提问来源于stack exchange,提问作者Makaveli Shakur

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 14:22:30