读取文本文件,更改某些行的列顺序 [英] Reading text file, change order of columns of some lines
问题描述
我的输入文件的格式为:
My input file is of the form:
0 1 0 0 0 1 1 0 0 0 / 1 0 / 1 0 / 1
0 1 0 3/4 1 0 0 1/4 0 0 -1 1/2
0 -1 0 1/4 -1 0 0 3/4 0 0 1 1/2
我想重新排列其中包含分数的行的顺序.目前,我有:
I want to rearrange the order of the lines that have the fraction within them. Currently I have:
#!bin/bash
filename="input.txt"
while ((i++)); read -r line; do
re='[0-9][/][0-9]';
if [[ $line =~ $re ]]
then
echo $line
fi
done < "$filename"
将回显第二行和第三行.是否可以使用awk或sed命令来获取这两行内容以将其顺序(保持第一行不变)更改为
which will echo the second and third line. Is there an awk or sed command I could use to get these two lines to change their order (leaving the first as is) to being
$1,$2,$3,$5,$6,$7,$9,$10,$11,$4,$8,$12
这将使我的文件现在看起来像
which would make my file now look like
0 1 0 0 0 1 1 0 0 0 / 1 0 / 1 0 / 1
0 1 0 1 0 0 0 0 -1 3/4 1/4 1/2
0 -1 0 -1 0 0 0 0 1 1/4 3/4 1/2
推荐答案
您可以使用awk轻松做到这一点,尽管如此,我认为定义游戏规则很重要. 在以下假设下:
you can easily do this with awk, nonetheless I think it is important to define the rules of the game a bit. Under the following assumptions :
- 分数是以下任何形式:
a/b
或a / b
或a/ b
- 如果小数出现在第4列或第8列中,请重新排列各列.
- 您要保持格式正确
- A fraction is anything of the form :
a/b
ora / b
ora/ b
- If a fraction appears in column 4 or 8, reshuffle the columns.
- you want to keep the formatting correct
请记住,您可以使用以下awk代码
With this in mind, you can use the following awk code
awk 'BEGIN{format="%4s%6s%6s%6s%6s%6s%6s%6s%6s%7s%7s%7s\n"}
{ gsub(/[[:blank:]]*\/[[:blank:]]*/,"/",$0); $0=$0 }
($4 ~ /\//) || ($8 ~ /\//) {
$12=$4" "$8" "$12
$4=""; $8=""
$0=$0
}
{ printf format,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12 }
' file.txt
这将执行以下操作:
-
用单个
/
$0 = $0
重新定义字段,即在您的前两行中,
将从18个字段移至12个
$0 = $0
redefines the fields, i.e. in your first two lines, you
will move from 18 fields to 12
如果在字段4或8中出现小数(即/
),则重新定义字段12,删除字段4和8,然后再次执行$0=$0
.
if a fraction (i.e. a /
) appears in field 4 or 8, then redefine field 12, delete field 4 and 8 and do $0=$0
again.
以正确的格式打印.
注意:在上面的示例中,分数具有不同的输出(无空格)
NOTE: in the above example, the fractions have a different output (no spaces)
以上内容将为您提供以下输出:
The above will give you the following output :
0 1 0 0 0 1 1 0 0 0/1 0/1 0/1
0 1 0 1 0 0 0 0 -1 3/4 1/4 1/2
0 -1 0 -1 0 0 0 0 1 1/4 3/4 1/2
如果您不想在第一行中更改分数,那么可以很容易地做到这一点
If you do not want change your fractions in the first line, then you can do it very easy like this
awk 'BEGIN{format="%4s%6s%6s%6s%6s%6s%6s%6s%6s%7s%7s%7s\n"}
(NF>12) { print; next }
{
$12=$4" "$8" "$12
$4=""; $8=""
$0=$0
printf format,$1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12
}
' file.txt
您在这里假设
-
如果一行中的字段超过12个,则只需打印
if a line has more then 12 fields, just print it
否则,请随机排列列
然而,它的鲁棒性较弱,因为所有内容都取决于在第4、8和12列中键入分数的方式. IE.必须 输入空格.输出将如下所示:
This is however less robust as everything depends how the fractions are typed in the 4th,8th and 12th column. I.e. they must be typed without spaces. Output will look like :
0 1 0 0 0 1 1 0 0 0 / 1 0 / 1 0 / 1
0 1 0 1 0 0 0 0 -1 3/4 1/4 1/2
0 -1 0 -1 0 0 0 0 1 1/4 3/4 1/2
这篇关于读取文本文件,更改某些行的列顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!