命令的最后一部分:
| cut -d' ' -f3
输出:
4
10
所需格式:
4,10
我猜测echo
(和sort
?)将以某种方式使用,但我无法完全确定它。
命令的最后一部分:
| cut -d' ' -f3
输出:
4
10
所需格式:
4,10
我猜测echo
(和sort
?)将以某种方式使用,但我无法完全确定它。
我正在使用这种晦涩的文件格式处理数据:
SNP A1 A2 F1 I1 F2 I2 F3 I3
rs0001 A C 0.02 0.00 1.99
(注意前三个字段周围的空格)
标头很长(500k 条目),我想将其转换为如下内容:
SNP A1 A2 F1_I1 F2_I2 F3_I3
rs0001 A C 0.02 0.00 1.99
...这样无论是否删除不规则的空白,都可以更轻松地处理。作为参考,这也是可以接受的,只要它是一致的:
SNP A1 A2 F1_I1 F2_I2 F3_I3
rs0001 A C 0.02 0.00 1.99
有什么办法可以在 Unix/Linux 中重新格式化它吗?谢谢
我有一个巨大的文件,其中包含许多列和行中的数值。第 6 列之后的列中的值都是数字加上缺少NA
(0
或1
或)。2
NA
我想以这种方式更改第 7 列及以后所有列中的值: 0
to A A
、1
to A B
、2
toB B
和NA
to 0 0
。这样,第 7 列的标题将是两次。我可以这样做awk
吗?我的输入文件是这样的,但在此之后有很多列。我的输出文件应该是制表符分隔的。
id1 id2 parental maternal sex phenotype A_101 A_102 A_103
20907153 20907153 0 0 -9 -9 1 0 0 0 0 0
31405729 31405729 0 0 -9 -9 0 0 0 0 0 0
31450731 31450731 0 0 -9 -9 0 0 0 2 0 0
41940308 41940308 0 0 -9 -9 0 0 0 NA 0 0
52428081 52428081 0 0 -9 -9 0 0 0 0 0 0
41943104 41943104 0 0 -9 -9 0 0 0 0 0 0
我想知道如何按照自定义规则递归地缩进越来越多的诗行。例如
假设我们有:
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: Or if Sion Hill
Delight thee more, and Siloa's Brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
And chiefly Thou O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread.
我们想递归缩进它,在第一行之后的任何三行添加3个空格,以这种方式
OF Mans First Disobedience, and the Fruit
Of that Forbidden Tree, whose mortal tast
Brought Death into the World, and all our woe,
With loss of Eden, till one greater Man
Restore us, and regain the blissful Seat,
Sing Heav'nly Muse, that on the secret top
Of Oreb, or of Sinai, didst inspire
That Shepherd, who first taught the chosen Seed,
In the Beginning how the Heav'ns and Earth
Rose out of Chaos: Or if Sion Hill
Delight thee more, and Siloa's Brook that flow'd
Fast by the Oracle of God; I thence
Invoke thy aid to my adventrous Song,
That with no middle flight intends to soar
Above th' Aonian Mount, while it pursues
Things unattempted yet in Prose or Rhime.
And chiefly Thou O Spirit, that dost prefer
Before all Temples th' upright heart and pure,
Instruct me, for Thou know'st; Thou from the first
Wast present, and with mighty wings outspread
实现这一目标的最简单方法是什么?
该column
命令的手册页描述了这样的-s
选项:
-s, --separator separators
Specify the possible input item delimiters (default is whitespace).
默认情况下,它说的-s
是 whitespace。我做了一些测试:
[root@192 ~]# awk 'substr($0,1,1)=="U"' /etc/fstab
UUID=75278d9c-4f90-4597-93d9-471186f5c1e7 / xfs defaults 0 0
UUID=8d7fa4f2-9852-41d7-9bf7-5f578895a5be /boot xfs defaults 0 0
UUID=11c7f961-4859-46e9-aa60-8110cf01ee0e none swap defaults 0 0
[root@192 ~]# awk 'substr($0,1,1)=="U"' /etc/fstab | column -t
UUID=75278d9c-4f90-4597-93d9-471186f5c1e7 / xfs defaults 0 0
UUID=8d7fa4f2-9852-41d7-9bf7-5f578895a5be /boot xfs defaults 0 0
UUID=11c7f961-4859-46e9-aa60-8110cf01ee0e none swap defaults 0 0
[root@192 ~]# awk 'substr($0,1,1)=="U"' /etc/fstab | column -to"|"
UUID=75278d9c-4f90-4597-93d9-471186f5c1e7|/ |xfs |defaults|0|0
UUID=8d7fa4f2-9852-41d7-9bf7-5f578895a5be|/boot|xfs |defaults|0|0
UUID=11c7f961-4859-46e9-aa60-8110cf01ee0e|none |swap|defaults|0|0
[root@192 ~]#
如果我添加 的“默认值” -s
,我会得到:
[root@192 ~]# awk 'substr($0,1,1)=="U"' /etc/fstab | column -to"|" -s " "
UUID=75278d9c-4f90-4597-93d9-471186f5c1e7|/ ||||||||||||||||||| | |||xfs| ||||defaults|||| | |||0|0
UUID=8d7fa4f2-9852-41d7-9bf7-5f578895a5be|/boot|||||||||||||||||||xfs| ||| |defaults|||| ||||0|0||| |
UUID=11c7f961-4859-46e9-aa60-8110cf01ee0e|none ||||||||||||||||||| |swap||| |defaults|||| ||||0|0||| |
[root@192 ~]#
为什么会这样?我期待它产生与awk 'substr($0,1,1)=="U"' /etc/fstab | column -to"|"
. 我也尝试给出诸如, , ,之类的-s
值,但都未能产生由.[:space:]+
[[:space:]]+
\s+
+
awk 'substr($0,1,1)=="U"' /etc/fstab | column -to"|"
我的问题是,如果我必须手动指定 的值-s
,我应该给它什么值才能产生与 相同的结果awk 'substr($0,1,1)=="U"' /etc/fstab | column -to"|"
?基本上,请帮我填空:
[root@192 ~]# awk 'substr($0,1,1)=="U"' /etc/fstab | column -to"|" -s <fill_your_answer_here>
UUID=75278d9c-4f90-4597-93d9-471186f5c1e7|/ |xfs |defaults|0|0
UUID=8d7fa4f2-9852-41d7-9bf7-5f578895a5be|/boot|xfs |defaults|0|0
UUID=11c7f961-4859-46e9-aa60-8110cf01ee0e|none |swap|defaults|0|0
[root@192 ~]#
我无法完全理解命令的-t
参数是如何expand
工作的。以下是其手册页的摘录。
NAME
expand - convert tabs to spaces
...
-t, --tabs=N
have tabs N characters apart, not 8
制表符 N 个字符分开是什么意思?我做了一些测试。
root@u2004:~# printf "a\tb\n"
a b
root@u2004:~# printf "a\tb\n" | od -a
0000000 a ht b nl
0000004
root@u2004:~# printf "a\tb\n" | expand | od -a
0000000 a sp sp sp sp sp sp sp b nl
0000012
root@u2004:~# printf "a\tb\n" | expand -t 4 | od -a
0000000 a sp sp sp b nl
0000006
root@u2004:~# printf "a\tb\n" | expand -t 5 | od -a
0000000 a sp sp sp sp b nl
0000007
root@u2004:~#
如您所见,当我通过时-t 4
,制表符被替换为 3 个空格。所以,实际上,“制表符间隔 4 个字符”只是意味着制表符被替换为 3 个空格?我无法理解。顺便说一句,我不是以英语为母语的人,这可能是一个与英语相关的问题。
我正在搜索组织在各种子目录中的大量文本文件。我可以运行诸如 之类的命令grep -lr foobar ./
,得到如下结果:
./dirA/dirA.A/abc.txt
./dirA/dirA.A/def.txt
./dirA/dirA.A/dirA.A.A/ghi.txt
./dirA/dirA.B/jkl.txt
./dirB/mno.txt
我想要某种方式在可视化树中显示这些,类似于tree
命令的工作方式。大致是这样的:
./
dirA/
dirA.A/
abc.txt
def.txt
dirA.A.A/
ghi.txt
dirA.B/
jkl.txt
dirB/
mno.txt
在一些带有堆栈的 Python 脚本中执行此操作似乎是微不足道的,但如果有办法,我真的很想通过 bash 直接执行此操作。所以我想我正在寻找一种方法来(a)格式化/转换 的输出grep
,或者(b)我迄今为止无法找到的其他一些通用的“indent-by-common-prefix”实用程序.
我可以使用这样的罗马数字按顺序编号一首诗的任何节:
Injurious love, why still to mar accord
Between desires has been thy favourite feat?
Why does it please thee so, perfidious lord,
Two hearts should with a different measure beat?
Thou wilt not let me take the certain ford,
Dragging me where the stream is deep and fleet.
Her I abandon who my love desires,
While she who hates, respect and love inspires.
Thou to Rinaldo show'st the damsel fair,
While he seems hideous to that gentle dame;
And he, who when the lady's pride and care,
Paid back with deepest hate her amorous flame,
Now pines, himself, the victim of despair,
Scorned in his turn, and his reward the same.
By the changed damsel in such sort abhorred,
She would choose death before that hated lord.
He to the Pagan cries: "Forego thy theft,
And down, false felon, from that pilfer'd steed;
I am not wont to let my own be reft.
And he who seeks it dearly pays the deed.
More -- I shall take from thee yon lovely weft;
To leave thee such a prize were foul misdeed;
And horse and maid, whose worth outstrips belief,
Were ill, methinks, relinquished to a thief."
"Thou liest," the haughty Saracen retorts,
As proud, and burning with as fierce a flame,
"A thief thyself, if Fame the truth reports:
But let good deeds decide our dubious claim,
With whom the steed or damsel fair assorts:
Best proved by valiant deeds: though, for the dame,
That nothing is so precious, I with thee
(Search the wide world throughout) may well agree."
进入这个:罗马大写数字与最后一节对齐?
Injurious love, why still to mar accord
Between desires has been thy favourite feat?
Why does it please thee so, perfidious lord,
Two hearts should with a different measure beat?
Thou wilt not let me take the certain ford,
Dragging me where the stream is deep and fleet.
Her I abandon who my love desires,
I While she who hates, respect and love inspires.
Thou to Rinaldo show'st the damsel fair,
While he seems hideous to that gentle dame;
And he, who when the lady's pride and care,
Paid back with deepest hate her amorous flame,
Now pines, himself, the victim of despair,
Scorned in his turn, and his reward the same.
By the changed damsel in such sort abhorred,
II She would choose death before that hated lord.
He to the Pagan cries: "Forego thy theft,
And down, false felon, from that pilfer'd steed;
I am not wont to let my own be reft.
And he who seeks it dearly pays the deed.
More -- I shall take from thee yon lovely weft;
To leave thee such a prize were foul misdeed;
And horse and maid, whose worth outstrips belief,
III Were ill, methinks, relinquished to a thief."
"Thou liest," the haughty Saracen retorts,
As proud, and burning with as fierce a flame,
"A thief thyself, if Fame the truth reports:
But let good deeds decide our dubious claim,
With whom the steed or damsel fair assorts:
Best proved by valiant deeds: though, for the dame,
That nothing is so precious, I with thee
IV (Search the wide world throughout) may well agree."
我想知道是否有任何方法可以使用文本实用程序在 linux 中进行此操作。它可能不适合 awk,以罗马风格生成数字,但我已经在互联网上的某个地方找到了一个 bash 脚本来生成罗马数字
#/bin/bash
# roman.sh
#
# Function
#
num2roman() { # NUM
# Returns NUM in roman letters
#
input=$1 # input num
output="" # Clear output string
len=${#input} # Initial length to count down
roman_val() { # NUM one five ten
# This sub does the basic 'roman' algorythm
#
N=$1
one=$2
five=$3
ten=$4
out=""
case $N in
0) out+="" ;;
[123]) while [[ $N -gt 0 ]]
do out+="$one"
N=$(($N-1))
done
;;
4) out+="$one$five" ;;
5) out+="$five" ;;
[678]) out+="$five"
N=$(($N-5))
while [[ $N -gt 0 ]]
do out+="$one"
N=$(($N-1))
done
;;
9) while [[ $N -lt 10 ]]
do out+="$one"
N=$(($N+1))
done
out+="$ten"
;;
esac
echo $out
}
while [[ $len -gt 0 ]]
do # There are letters to add
num=${input:0:1}
# Do action according position
case $len in
1) # 1
output+="$(roman_val $num I V X)"
;;
2) # 10
output+="$(roman_val $num X L C)"
;;
3) # 100
output+="$(roman_val $num C D M)"
;;
*) # 1000+
# 10'000 gets a line above, 100'000 gets a line on the left.. how to?
num=${input:0:(-3)}
while [[ $num -gt 0 ]]
do output+="M"
num=$(($num-1))
done
;;
esac
input=${input:1} ; len=${#input}
done
echo $output
}
#
# Call it
#
num2roman $1
我用这种语法调用:
for N in `seq 1 10`;do ./roman.sh $N; done
谁的输出是:
I
II
III
IV
V
VI
VII
VIII
IX
X
因此,从另一个角度来看,这可能只是从生成的罗马数字列表中挑选任何项目,然后将其中任何一项与任何一节的最后一节对齐
我想使用printf
带有特定格式字符串的快捷命令,并想出了以下内容。
local PF="printf %s\n"
$PF "Some Text"
它可以完成这项工作,但想知道在使用这种方法时是否存在任何警告,这可能会导致对格式字符串的误解。
--table-noextreme
选项如何column
工作?
-E, --table-noextreme columns Specify columns where is possible to ignore unusually long (longer than average) cells when calculate column width. The option has impact to the width calculation and table formatting, but the printed text is not affected.
$ cat table.md
Lorem ipsum | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet
Lorem | Lorem ipsum | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut | Lorem ipsum dolor
Lorem | Lorem ipsum dolor | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do | Lorem
$ column -t -s '|' -o '|' < table.md
Lorem ipsum | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet
Lorem | Lorem ipsum | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut | Lorem ipsum dolor
Lorem | Lorem ipsum dolor | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do | Lorem
$ column -t -s '|' -o '|' --table-noextreme 3 < table.md
Lorem ipsum | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet
Lorem | Lorem ipsum | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut | Lorem ipsum dolor
Lorem | Lorem ipsum dolor | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do | Lorem
我怎样才能让它格式化表格
Lorem ipsum | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet | Lorem ipsum dolor sit amet
Lorem | Lorem ipsum | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut | Lorem ipsum dolor
Lorem | Lorem ipsum dolor | Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do | Lorem
(来自 util-linux 2.37.2/arch 的列)