AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / unix / 问题

问题[text-formatting](unix)

Martin Hope
kesarling He-Him
Asked: 2024-03-05 07:12:33 +0800 CST

如何将 cut 的输出格式化为以下格式?[复制]

  • 6
这个问题在这里已经有了答案:
将一列中的数百个数字转换为以逗号分隔的行 (9 个答案)
5 小时前关闭。

命令的最后一部分:

| cut -d' ' -f3

输出:

4
10

所需格式:

4,10

我猜测echo(和sort?)将以某种方式使用,但我无法完全确定它。

text-formatting
  • 1 个回答
  • 13 Views
Martin Hope
Giulio Centorame
Asked: 2023-05-25 11:40:25 +0800 CST

使用额外的空格更改不规则标题

  • 6

我正在使用这种晦涩的文件格式处理数据:

   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 中重新格式化它吗?谢谢

text-formatting
  • 2 个回答
  • 43 Views
Martin Hope
motor 30
Asked: 2022-10-01 10:05:59 +0800 CST

将数值重新格式化为两个值

  • -2

我有一个巨大的文件,其中包含许多列和行中的数值。第 6 列之后的列中的值都是数字加上缺少NA (0或1或)。2NA

我想以这种方式更改第 7 列及以后所有列中的值: 0to A A、1to A B、2toB B和NAto 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   
awk text-formatting
  • 1 个回答
  • 45 Views
Martin Hope
Dingo
Asked: 2022-06-13 02:34:00 +0800 CST

按照给定规则缩进文本行

  • 2

我想知道如何按照自定义规则递归地缩进越来越多的诗行。例如

假设我们有:

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

实现这一目标的最简单方法是什么?

text-processing text-formatting
  • 2 个回答
  • 175 Views
Martin Hope
Fajela Tajkiya
Asked: 2022-04-17 10:24:15 +0800 CST

“column”命令的默认 -s 选项如何工作?如何通过手动传递一些值来模仿默认的 -s 选项?

  • 1

该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 ~]#
text-processing text-formatting
  • 1 个回答
  • 60 Views
Martin Hope
Fajela Tajkiya
Asked: 2022-04-16 02:20:55 +0800 CST

对扩展命令的理解

  • 0

我无法完全理解命令的-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 个空格?我无法理解。顺便说一句,我不是以英语为母语的人,这可能是一个与英语相关的问题。

text-processing text-formatting
  • 1 个回答
  • 48 Views
Martin Hope
loneboat
Asked: 2022-03-17 08:43:42 +0800 CST

将 `grep -lr` 结果显示为树

  • 1

我正在搜索组织在各种子目录中的大量文本文件。我可以运行诸如 之类的命令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”实用程序.

text-processing text-formatting
  • 1 个回答
  • 151 Views
Martin Hope
Dingo
Asked: 2021-12-15 17:19:01 +0800 CST

用罗马数字给诗节编号

  • 3

我可以使用这样的罗马数字按顺序编号一首诗的任何节:

       
       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

因此,从另一个角度来看,这可能只是从生成的罗马数字列表中挑选任何项目,然后将其中任何一项与任何一节的最后一节对齐

text-processing text-formatting
  • 3 个回答
  • 282 Views
Martin Hope
Aardvark
Asked: 2021-11-26 19:39:25 +0800 CST

具有特定格式字符串的 printf 快捷命令

  • 1

我想使用printf带有特定格式字符串的快捷命令,并想出了以下内容。

local PF="printf %s\n"
$PF "Some Text"

它可以完成这项工作,但想知道在使用这种方法时是否存在任何警告,这可能会导致对格式字符串的误解。

bash text-formatting
  • 2 个回答
  • 165 Views
Martin Hope
laktak
Asked: 2021-11-18 13:42:01 +0800 CST

列--table-noextreme

  • 0

--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 的列)

text-formatting
  • 1 个回答
  • 25 Views

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    模块 i915 可能缺少固件 /lib/firmware/i915/*

    • 3 个回答
  • Marko Smith

    无法获取 jessie backports 存储库

    • 4 个回答
  • Marko Smith

    如何将 GPG 私钥和公钥导出到文件

    • 4 个回答
  • Marko Smith

    我们如何运行存储在变量中的命令?

    • 5 个回答
  • Marko Smith

    如何配置 systemd-resolved 和 systemd-networkd 以使用本地 DNS 服务器来解析本地域和远程 DNS 服务器来解析远程域?

    • 3 个回答
  • Marko Smith

    dist-upgrade 后 Kali Linux 中的 apt-get update 错误 [重复]

    • 2 个回答
  • Marko Smith

    如何从 systemctl 服务日志中查看最新的 x 行

    • 5 个回答
  • Marko Smith

    Nano - 跳转到文件末尾

    • 8 个回答
  • Marko Smith

    grub 错误:你需要先加载内核

    • 4 个回答
  • Marko Smith

    如何下载软件包而不是使用 apt-get 命令安装它?

    • 7 个回答
  • Martin Hope
    user12345 无法获取 jessie backports 存储库 2019-03-27 04:39:28 +0800 CST
  • Martin Hope
    Carl 为什么大多数 systemd 示例都包含 WantedBy=multi-user.target? 2019-03-15 11:49:25 +0800 CST
  • Martin Hope
    rocky 如何将 GPG 私钥和公钥导出到文件 2018-11-16 05:36:15 +0800 CST
  • Martin Hope
    Evan Carroll systemctl 状态显示:“状态:降级” 2018-06-03 18:48:17 +0800 CST
  • Martin Hope
    Tim 我们如何运行存储在变量中的命令? 2018-05-21 04:46:29 +0800 CST
  • Martin Hope
    Ankur S 为什么 /dev/null 是一个文件?为什么它的功能不作为一个简单的程序来实现? 2018-04-17 07:28:04 +0800 CST
  • Martin Hope
    user3191334 如何从 systemctl 服务日志中查看最新的 x 行 2018-02-07 00:14:16 +0800 CST
  • Martin Hope
    Marko Pacak Nano - 跳转到文件末尾 2018-02-01 01:53:03 +0800 CST
  • Martin Hope
    Kidburla 为什么真假这么大? 2018-01-26 12:14:47 +0800 CST
  • Martin Hope
    Christos Baziotis 在一个巨大的(70GB)、一行、文本文件中替换字符串 2017-12-30 06:58:33 +0800 CST

热门标签

linux bash debian shell-script text-processing ubuntu centos shell awk ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve