ITPub博客

首页 > Linux操作系统 > Linux操作系统 > [20210819]给文件内容编行号.txt

[20210819]给文件内容编行号.txt

原创 Linux操作系统 作者:lfree 时间:2021-08-19 22:05:36 0 删除 编辑

[20210819]给文件内容编行号.txt

--//昨天在小区里被一位老者拦下,请求处理一个小问题,给一个文件内容编行号。
--//实际上就是U盘里面有一堆MP3文件,使用MP3播放器时,只要输入数字就能播放对应歌曲。
--//这个问题对于我很简单,不过麻烦的是对方机器是windows XP环境,没有nl,cat之类的命令。
--//上网安装unxutils包,安装后执行如下:
nl mp3.txt > mp3a.txt
cat -n mp3.txt> mp3a.txt

--//不过对方测试按对应数字播放的歌曲对应不上,我估计他可能往U盘添加MP3文件,导致顺序发生变化。
--//重新生成后再编排,执行如下:
dir /s/b *.mp3 | nl > mp3a.txt

--//再次测试,问题搞定。回家后上网查询到如下链接:
https://www.cnblogs.com/kerrycode/p/12781340.html =>Linux shell中如何给文本加上行号呢

--//awk 可以执行如下:
$ awk '{ print FNR " " $0}' redo_anly_script.sql

--//sed呢?我自己以前写过:
[20201202]sed加行号.txt

--//链接提供一些加行号的方法:http://www.itpub.net/thread-2140068-1-1.html
--//想使用sed实现,上网看了一下:

R:\>cat a.txt
this is a test 1
this is a test 2
this is a test 3
this is a test 4

R:\> sed "=" a.txt | sed "N;s/\n/ /"
1 this is a test 1
2 this is a test 2
3 this is a test 3
4 this is a test 4

--//有点麻烦。通过例子展开分析。

D:\>c:\windows\system32\echo 111\n222\n333\n
111
222
333

D:\>c:\windows\system32\echo 111\n222\n333\n | sed =
1
111
2
222
3
333
4

--//等号实际上输出行号,但是出现换行。
D:\>c:\windows\system32\echo 111\n222\n333 | sed = | paste -d" " - -
1 111
2 222
3 333

--//sed "N;s/\n/ /" ,这里分成2个命令,N是两行连接起来;s/\n/ /替换中间的\n使用空格。这类的命令不常用,基本记不住。

--//在vim下如何完成呢?vim下有一个插件Inc,我的机器已经安装这个插件:
: help Inc

:[line1,line2]Inc [s<number>] [i<number>] [r<number>] [w<number>] [h] [o] [p<regexp>] [w<number>] [c]
   STARTING  [s<number>]:  to change the starting value
                           (default: 0)
   INCREMENT [i<number>]:  to increase the value by this amount between matches
                           (default: 1)
   REPEAT    [r<number>]:  to increase the value after <number> matches
                           (default: 1)
   WIDTH     [w<number>]:  to align all the numbers to the right with the given width
                           (default: 4)
   FILLER    [f<char>]:    to align numbers to the right, use the given character
   HEX       [h]:          to use a hexadecimal base
   OCTAL     [o]:          to use an octal base
   PATTERN   [p<regexp>]:  if not using w parameter:
                               to replace the pattern
                           if using w parameter:
                               to search the line that matches the pattern and change a specific word
                           NOTE: with regexp be careful to use \ before spaces and \\ to place a literal \
   CONFIRM   [c]        :  to confirm each substitution; one to confirm before, two to confirm before AND after.
                           (default: not active)
   NOTE: The default values can be changed.

--//看了一下例子很简单,执行如下:
:%s/^/@ /
:%Inc s1 i1 w4

--//当然应该还有许多方法,我在自己cygwin下测试:
$ paste -d" " <(seq $(wc -l <aa.txt)) <( cat aa.txt)
1 altair:x:1001:1001:Altair Ibn La Ahad,,,,:/home/altair:/bin/bash
2 bind:x:120:132::/var/cache/bind:/bin/false
3 dnsmasq:x:121:65534:dnsmasq,,,:/var/lib/misc:/bin/false
4 mysql:x:115:129:MySQL Server,,,:/nonexistent:/bin/false
5 postfix:x:105:126::/var/spool/postfix:/bin/false
6 raghu:x:1000:1000:raghu,,,:/home/raghu:/bin/bash
7 smmsp:x:119:131:Mail Submission Program,,,:/var/lib/sendmail:/bin/false
8 smmta:x:118:130:Mail Transfer Agent,,,:/var/lib/sendmail:/bin/false
9 sshd:x:117:65534::/var/run/sshd:/usr/sbin/nologin
10 statd:x:116:65534::/var/lib/nfs:/bin/false

来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/267265/viewspace-2787888/ ,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论
熟悉oracle相关技术,擅长sql优化,rman备份与恢复,熟悉linux shell编程。

注册时间:2008-01-03

  • 博文量
    4006
  • 访问量
    7970383