gpt4 book ai didi

linux - Sed 在 shell 脚本中用 Control-A 替换制表符

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:11:39 26 4
gpt4 key购买 nike

处理数据并尝试在 shell 脚本中将制表符分隔符转换为 control-a。使用命令行,我将通过执行以下序列“control-v,control-a”来表示 control-a。

这是我在 .sh 中的代码:

echo "tab    delimited     query     here" | sed 's/    /'\001'/g' > $OUTPUT_FILE

但是,这是行不通的。我还尝试了以下方法:

 '\x001'
x1
'\x1'

最佳答案

你可以在这里使用tr:

echo $'tab\tdelimited\tquery\there' | tr '\t' $'\x01'

为了证明它已被替换:

echo $'tab\tdelimited\tquery\there' | tr '\t' $'\x01' | cat -vte

输出:

tab^Adelimited^Aquery^Ahere$

sed 替代方案:

echo $'tab\tdelimited\tquery\there' | sed $'s/\t/\x01/g'

awk 替代方案:

echo $'tab\tdelimited\tquery\there' | awk -F '\t' -v OFS='\x01' '{$1=$1} 1'

关于linux - Sed 在 shell 脚本中用 Control-A 替换制表符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35902111/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com