Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从数组的所有元素中删除空格_Arrays_Bash_Awk_Sed_Whitespace - Fatal编程技术网

Arrays 从数组的所有元素中删除空格

Arrays 从数组的所有元素中删除空格,arrays,bash,awk,sed,whitespace,Arrays,Bash,Awk,Sed,Whitespace,我有一个名为“test.in”的示例文件,如下所示,带有前导空格和尾随空格: Hi | hello how | are you? I need | to remove | leading & trailing spaces | where ever | it's located 我需要将由“|”分隔的元素放入以“\n”为主分隔符的数组中 我希望数组中的每个元素不应该有前导空格或尾随空格,只允许字符之间有空格。在我将代码作为主要部署之前,我使用一个示例代码来测试结果 示例脚

我有一个名为“test.in”的示例文件,如下所示,带有前导空格和尾随空格:

 Hi | hello how | are you?
 I need | to remove | leading & trailing
 spaces |  where  ever   | it's located
我需要将由“|”分隔的元素放入以“\n”为主分隔符的数组中

我希望数组中的每个元素不应该有前导空格或尾随空格,只允许字符之间有空格。在我将代码作为主要部署之前,我使用一个示例代码来测试结果

示例脚本的一部分:

OIFS="$IFS"
IFS=$'\n'
while read LINE
do
IFS='|'
my_tmpary=($LINE)
echo ${my_tmpary[@]}
my_ary=`echo ${my_tmpary[@]} | awk '$1=$1'`
echo ${my_ary[@]}
done < test.in
我应该这样做。因此:

$ my_ary=$(sed -E 's/[\t]{1,}/ /g;s/^ *| *$//g;s/[ ]{2,}/ /g;s/ *\| */ /g;' test.in)
$ echo "$my_ary"
Hi hello how are you?
I need to remove leading & trailing
spaces where ever it's located
This lines has many tabs
解决你的问题


注释 1.
s/[\t]{1,}//g
将制表符ie
\t
转换为空白。 2.
s/^*|*$//g
删除前导和尾随空格。 3.
s/[]{2,}//g
将多个空格压缩为一个空格。 4.
s/*\\\\*/\\\\\\\\*/g
删除
5.
-E
允许使用扩展正则表达式和
sed

您可以(ab)使用这样一个事实,即当
IFS
为默认值时,read将删除前导和尾随空格:

while read -r line; do
  printf "%s\n" "$line" # Leading and trailing spaces are removed.
done < test.in
在AWK中:

awk '{gsub(/^ *| *$|/,"",$0); gsub(/ *\| *| +/," ",$0); print $0}' test.in
gsub(/^*|*$|/,“”,$0)
删除前导和尾随空格,
gsub(/*\\\*.+/,“”,$0)
将空间管道空间组合和多个空间替换为单个空间,
打印$0
打印整个记录$0可以像@mona_sax注释一样省略,但为了清晰起见,我将其保留在代码中

当然,它可以循环,每个管道分隔的字段可以分别修剪:

awk -F\| -v OFS=" " '       # set input field separator to "|" and output separator to " "
function trim(str) {
  gsub(/^ *| *$/,"",str);   # remove leading and trailing space from the field
  gsub(/ +/," ",str);       # mind those multiple spaces as well
  return str                # return cleaned field
} 
{
  for(i=1;i<=NF;i++)        # loop thru all pipe separated fields
    printf "%s%s", trim($i),(i<NF?OFS:ORS) # print field and OFS or  
}                                          # output record separator in the end of record
' test.in
awk-F\|-v OFS=“”#将输入字段分隔符设置为“|”,将输出分隔符设置为“”
功能微调(str){
gsub(/^*.*$/,“”,str)#从字段中删除前导和尾随空格
gsub(+/+/,“”,str)#也要注意那些多个空格
返回str#返回已清理的字段
} 
{

对于(i=1;iprintf是一个不错的选择,sed不起作用,您正在匹配整行,然后出于某种原因使用g。@123有一个替代(
\\\\\124;
)在那里。@andlrc我的错,我认为这是偶然的,因为我很确定他们想在
之间进行修剪,而不仅仅是在这行的开始和结束。@123我想你可能是对的,这就是为什么我对sjsam的答案投了更高的票,我把这个留在这里以备将来参考。因为OP的问题不是很清楚——至少对我来说是这样:-)我认为你的假设是正确的,OP也希望在
@andlrc之前和之后删除空格。嗯,我认为这就是OP希望在这里做的,是的!:)将第一个改为
s/^*\\\*$//g
来处理后面的空格。@123:我明白了,添加了这个。请注意,我已经添加了
s/[\t]{1,}//g
由于这里的顺序很重要,所以从头到尾都是这样。谢谢你,Ed。我浏览了你上面的链接。我现在明白了重点。@Edmorton你的问题目前还不是很清楚。
你想不想删减一下周围的空格。无论如何,放一个示例输出来更好地表达你自己总是好的。还有
我的
不是数组。它只保存命令
echo${my_tmpary[@]}的结果| awk'$1=$1'
这是一个字符串。停止,你的方法完全错误,你甚至不应该有一个shell数组。请参阅。如果你发布了一个新问题,带有简明的可测试样本输入和预期输出,我们可以帮你做对。
print$0
是多余的。
print
默认方式是
print$0
。也可以删除输入中的制表符。@mona_sax,您也可以说
awk'{gsub(…)}1'文件
。这个
1
(或任何其他真值)计算为真,并触发
{print$0}
@fedorqui:当然可以!:D这是一种更为惯用和优雅的方法。
sed 's/^[[:space:]]*\|[[:space:]]*$//g' test.in
awk '{gsub(/^ *| *$|/,"",$0); gsub(/ *\| *| +/," ",$0); print $0}' test.in
awk -F\| -v OFS=" " '       # set input field separator to "|" and output separator to " "
function trim(str) {
  gsub(/^ *| *$/,"",str);   # remove leading and trailing space from the field
  gsub(/ +/," ",str);       # mind those multiple spaces as well
  return str                # return cleaned field
} 
{
  for(i=1;i<=NF;i++)        # loop thru all pipe separated fields
    printf "%s%s", trim($i),(i<NF?OFS:ORS) # print field and OFS or  
}                                          # output record separator in the end of record
' test.in