Kar*_*hik 14 grep sed awk shell-script
我有一个包含 10 列的输入文本文件,在处理这个文件时,在中间一列中,我得到了这种类型的数据。我要求列值如下:
输入列值:“这是我的新程序:“Hello World””
必需的列值:“这是我的新程序:Hello World”。
请在任何 Unix shell 脚本或任何命令中帮助我。非常感谢您的时间并提前致谢。
Jes*_*hez 28
如果您想删除所有双引号,一个非常简单的选择是使用 sed 作为@Dani 建议。
$ echo "This is my program \"Hello World\"" | sed 's/"//g'
This is my program Hello World
Run Code Online (Sandbox Code Playgroud)
尽管如此,如果您只想删除内部引号,我建议删除所有引号并在开头添加一个,在结尾添加一个,如下所示。
假设我们有一个包含以下内容的文件 sample.txt:
$ cat sample.txt
"This is the "First" Line"
"This is the "Second" Line"
"This is the "Third" Line"
Run Code Online (Sandbox Code Playgroud)
然后,如果您只想删除内部引号,我建议如下:
$ cat sample.txt | sed 's/"//g' | sed 's/^/"/' |sed 's/$/"/'
"This is the First Line"
"This is the Second Line"
"This is the Third Line"
Run Code Online (Sandbox Code Playgroud)
解释:
sed 's/"//g'删除每一行的所有双引号
sed 's/^/"/'在每行的开头添加双引号
sed 's/$/"/'在每行末尾添加双引号
sed 's/|/"|"/g'在每个管道之前和之后添加一个引号。
希望这可以帮助。
编辑:根据管道分隔符注释,我们必须稍微更改命令
让 sample.txt 为:
$ cat sample.txt
"This is the "First" column"|"This is the "Second" column"|"This is the "Third" column"
Run Code Online (Sandbox Code Playgroud)
然后,为管道添加替换命令为我们提供最终解决方案。
$ cat sample.txt | sed 's/"//g' | sed 's/^/"/' |sed 's/$/"/' | sed 's/|/"|"/g'
"This is the First column"|"This is the Second column"|"This is the Third column"
Run Code Online (Sandbox Code Playgroud)
使用这个 sample.txt 文件
$ cat sample.txt
"This is the "first" column"|12345|"This is the "second" column"|67890|"This is the "third" column"
Run Code Online (Sandbox Code Playgroud)
还有这个脚本
#!/bin/ksh
counter=1
column="initialized"
result=""
while [[ "$column" != "" ]]
do
eval "column=$(cat sample.txt | cut -d"|" -f$counter)"
eval "text=$(cat sample.txt | cut -d"|" -f$counter | grep '"')"
if [[ "$column" = "$text" && -n "$column" ]]
then
if [[ "$result" = "" ]]
then
result="_2quotehere_${column}_2quotehere_"
else
result="${result}|_2quotehere_${column}_2quotehere_"
fi
else
if [[ -n "$column" ]]
then
if [[ "$result" = "" ]]
then
result="${column}"
else
result="${result}|${column}"
fi
fi
fi
echo $result | sed 's/_2quotehere_/"/g' > output.txt
(( counter+=1 ))
done
cat output.txt
exit 0
Run Code Online (Sandbox Code Playgroud)
你会得到这个:
$ ./process.sh
"This is the first column"|12345|"This is the second column"|67890|"This is the third column"
$ cat output.txt
"This is the first column"|12345|"This is the second column"|67890|"This is the third column"
Run Code Online (Sandbox Code Playgroud)
我希望这是您需要的处理。
让我知道!
此脚本处理您提供的输入行,包括多次。唯一的限制是所有 20 列必须在同一行上。
#!/bin/ksh
rm output.txt > /dev/null 2>&1
column="initialized"
result=""
lineCounter=1
while read line
do
print "LINE $lineCounter: $line"
counter=1
while [[ ${counter} -le 20 ]]
do
eval 'column=$(print ${line} | cut -d"|" -f$counter)'
eval 'text=$(print ${line} | cut -d"|" -f$counter | grep \")'
print "LINE ${lineCounter} COLUMN ${counter}: $column"
if [[ "$column" = "$text" && -n ${column} ]]
then
if [[ "$result" = "" ]]
then
result="_2quotehere_$(echo ${column} | sed 's/\"//g')_2quotehere_"
else
result="${result}|_2quotehere_$( echo ${column} | sed 's/\"//g')_2quotehere_"
fi
else
if [[ "$result" = "" ]]
then
result=${column}
else
result="${result}|${column}"
fi
fi
(( counter+=1 ))
done
(( lineCounter+=1 ))
echo -e $result | sed 's/_2quotehere_/"/g' >> output.txt
result=""
done < input.txt
print "OUTPUT CONTENTS:"
cat output.txt
exit 0
Run Code Online (Sandbox Code Playgroud)
从这里开始,您必须能够使其适用于您的特定情况。
| 归档时间: |
|
| 查看次数: |
67826 次 |
| 最近记录: |