Sed bash是否在每个空格前添加反斜杠?
如果我有像这样的文字Sed bash是否在每个空格前添加反斜杠?,sed,awk,grep,Sed,Awk,Grep,如果我有像这样的文字 /mnt/data/web/web content/page 1/home page.txt 我使用以下命令获取文本,然后通过管道将其传输到另一个命令 cat somefile.txt | awk '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=""; print $0}' 我需要将上述命令导入另一个命令以添加反斜杠 如何在每个空格前添加反斜杠以获得正确的*nix路径 即 理论上,文本可以无限长,但我总是需要在每个空格前加一个反斜杠。 最后一个脚本将在
/mnt/data/web/web content/page 1/home page.txt
我使用以下命令获取文本,然后通过管道将其传输到另一个命令
cat somefile.txt | awk '{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=""; print $0}'
我需要将上述命令导入另一个命令以添加反斜杠
如何在每个空格前添加反斜杠以获得正确的*nix路径
即
理论上,文本可以无限长,但我总是需要在每个空格前加一个反斜杠。
最后一个脚本将在freebsd和linux中使用
谢谢 它对你有用吗
echo "/mnt/data/web/web content/page 1/home page.txt" > input.txt
然后:
然后:
如果您想对所有空间都这样做,可以尝试awk
使用awk
:
awk -F" " '{ print $1"\\ " $2"\\ " $3"\\ " $4}' input.txt > output.txt
您需要引用整个路径:
# Create file
echo Hi > "a file with spaces"
# Try to cat it
n="a file with spaces"
cat $n
cat: a: No such file or directory
cat: file: No such file or directory
cat: with: No such file or directory
cat: spaces: No such file or directory
# With quotes this time :-)
cat "$n"
Hi
这可能适用于您(GNU-sed):
此
awk
应能:
awk '{gsub(/ /,"\\ ")}8' file
/mnt/data/web/web\ content/page\ 1/home\ page.txt
上述scirpt是从以下链接添加的
在我不得不处理Gatesbox用户的硬盘驱动器之前,他们的产品一直运行良好。我个人使用这个脚本通过命令行播放音乐和电影。例如,我将上述内容保存为omx_play_lister.sh,然后chmod+x omx_play_lister
并运行
./omx_play_lister "/media/mount/cWin/Music by artist/artist name and album folder" "omxplayer -ohdmi --vol=-2500" "5"
它将愉快地采用文件路径/media/mount/cWin/Music by artist/artist name and album folder
并将其转换为/media/mount/cWin/Music\by\artist/artist\name\ and\album\folder
或在传递文件路径时不太像“”的空位。在末尾附近的look中使用sed命令的好处是,即使文件本身包含空格,也会在传递给$Commands
小小的“${entry}”
和“$File\u Path/”*
中,最棘手的格式位需要排序,但它适合我所需要的内容,可能需要稍加修改,也适合您的使用
快乐的黑客和修改一切。你可以在路径周围加上引号,这样你就不需要逃避空间。e、 g.
rm/mnt/data/web/web content/page 1/home page.txt
将失败,但是rm/mnt/data/web/web content/page 1/home page.txt“
可能会做您想要做的事情使用cat和一堆管道中的awk语句提取我的文本,我如何轻松地在输出周围添加引号?以下是相关部分cat somefile.txt | awk'{$1=$2=$3=$4=$5=$6=$7=$8=$9=$10=“”;print$0}“然后通过管道输出更多要添加的数据……不起作用吗?试试我的awk解决方案,让我知道我用sed命令做了你想做的事情
更新了我的问题,添加了我用来获取文本的命令,如果我将我的命令通过管道输入sed的| | \\\\\\\\”,我会在每行的开头得到一个\而不是在使用awk的spacestry之前解决方案谢谢,但我需要能够管道输入和管道输出我无法在系统上创建文件。此外,输入可能有无限的空格,因此我需要一个解决方案,在每个空格之前而不是在字段1、2、3之后添加\,除非它可以在每个字段之后添加\而不必键入无限数量的$1“\\”$2“\\”…我可以问一下“8”到底是做什么的吗?它的作用与1
相同。任何一个正数都是一个真实的测试。如果测试为true,则执行默认操作,打印$0
。所以它只打印所有行。
# Create file
echo Hi > "a file with spaces"
# Try to cat it
n="a file with spaces"
cat $n
cat: a: No such file or directory
cat: file: No such file or directory
cat: with: No such file or directory
cat: spaces: No such file or directory
# With quotes this time :-)
cat "$n"
Hi
sed 's/\s/\\&/g' file
awk '{gsub(/ /,"\\ ")}8' file
/mnt/data/web/web\ content/page\ 1/home\ page.txt
# Wrap file path with "" so it can be treated as $1
# Wrap command with "" if spaces are present to treat as $2
# Wraping time only nessisay for pros ;-) $3
# Example synax;
# format_space_do_time "/dir with/sp ac e s" "Comand With options" "TimeToSleep"
# Set $1, $2 and $3 variable names such that they can be hard-coded for debugging
File_Path="$1"
Commands="$2"
Timed_Sleep="$3"
format_space_do_time (){
# Check if services or command is already in proggress
while true
do
if ps ax | grep -v grep | grep "$Commands" > /dev/null
then
# Waite $Time_Sleep for $Commands to finish
sleep $Time_Sleep;
else
# $Commands not detected. Start'em now
for i in "$File_Path/"*
do
# Magic time, lets deal with spaces in $i and save it to $entry
# Option 1 replaces " " with "$'\0'" which is bashs way of dealing with spaces
entry1=$(echo "$i" | sed 's/ /$\'\\\0\'/g')
# Option 2 can be used to replace " " with "\ " of cource without the quotes around them :-)
entry2=$(echo "$i" | sed 's/ /\\\ /g')
# Clear previous outputs
clear
# run $Commands against $entry1
# Change 1 to 2 for the other option
${Commands} "${entry1}" > /dev/null
done
fi
done
}
./omx_play_lister "/media/mount/cWin/Music by artist/artist name and album folder" "omxplayer -ohdmi --vol=-2500" "5"