简体   繁体   中英

Sed to replace tab with Control-A in shell script

Working with data and trying to convert a tab-delimited to control-a in a shell script. Using command-line, I would represent control-a by doing the following sequence, 'control-v, control-a'.

Here is my code in a .sh:

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

However, this doesn't work. I've also tried the following:

 '\x001'
 x1
 '\x1'

You can use tr here:

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

To demonstrate that it has been replaced:

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

Output:

tab^Adelimited^Aquery^Ahere$

sed alternative:

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

awk alternative:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM