输入
这是文件test.txt
this is row4 row4
row2 xxx row2
row11 // row11
row10 mmm row10
row8row8 fubar
row1row1
row6 and row6
row7 row7
row3row3
/row9 row9
row5 /row5
在每一行上,都会显示应该放置行的位置。例如,当前行9包含字符串" row3row3",这意味着第9行应放在第3位。每行包含应该放置行的位置的两倍的指示,而不是两行具有相同的索引。
预期输出
以下是test2.txt
row1row1
row2 xxx row2
row3row3
this is row4 row4
row5 /row5
row6 and row6
row7 row7
row8row8 fubar
/row9 row9
row10 mmm row10
row11 // row11
你能帮我分类一下这个文件吗?
我尝试了什么
这是我到目前为止的审判。我想我不是太远但是有一个我找不到的错误。
# Get the rows indication
a=$(grep -o row[0-9]* test.txt | sed s/row//)
a=( $a )
# Remove the double indication
a2=()
for i in $(seq 1 ${#a[@]})
do
[ $(($i%2)) -ne 0 ] && a2+=(${a[i]})
done
# Loop through each row
for row in $(seq 1 ${#a2[@]})
do
# Search for the row that should be placed at position $row
for i in "${!a2[@]}"; do
if [[ "${a2[$i]}" = "${row}" ]]
then
# Once the correct row was found, read it and print it on another file
p=$(sed "${a2[$i]}q;d" test.txt)
echo $p >> test2.txt
break
fi
done
done
答案 0 :(得分:3)
使用gnu awk
可以在一个命令中执行此操作:
awk -F '.*row' 'BEGIN {
PROCINFO["sorted_in"] = "@ind_num_asc"
}
{
a[$2] = $0
}
END {
for(k in a)
print a[k]
}' file
.*row
,我们会在每行row
之后提取数字a
,其中值为实线PROCINFO["sorted_in"] = "@ind_num_asc"
用于按数组索引的升序数字顺序对关联数组进行排序<强>输出:强>
row1row1
row2 xxx row2
row3row3
this is row4 row4
row5 /row5
row6 and row6
row7 row7
row8row8 fubar
/row9 row9
row10 mmm row10
row11 // row11
答案 1 :(得分:2)
您可以随时关注装饰/排序/不合格模式
$ sed -r 's/.*row([0-9]+)/\1\t&/' rows | sort -n | cut -f2-
row1row1
row2 xxx row2
row3row3
this is row4 row4
row5 /row5
row6 and row6
row7 row7
row8row8 fubar
/row9 row9
row10 mmm row10
row11 // row11
答案 2 :(得分:1)
试试这个:
tr -c '0-9\n' ' ' <file | awk '{print $1}' | paste -d " " - file | sort -k1,1n | cut -d " " -f 2-
答案 3 :(得分:0)
Perl解决方案:
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public string getParent()
{
using (var context = new Data.Entities())
{
IQueryable<ERPv2.Data.Links> dataQuery = from x in context.Links
where x.ParentID == 68
select x;
var newQry = dataQuery.AsEnumerable().Select(c => new
{
ID = c.ID,
Name = c.Name,
Children = HasChildren(231)
});
JavaScriptSerializer JSON = new JavaScriptSerializer();
return JSON.Serialize(newQry);
}
}
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public bool HasChildren(int ID)
{
using (var context = new Data.Entities())
{
IQueryable<ERPv2.Data.Links> dataQuery = from x in contextLinks
where x.ParentID == ID
select x;
return dataQuery.AsEnumerable().Any();
}
}