Unix的单行交换/转在多个文本文件的两行? [英] Unix one-liner to swap/transpose two lines in multiple text files?

查看:227
本文介绍了Unix的单行交换/转在多个文本文件的两行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想换或转对线根据自己的线路编号(例如,转换线10和15的位置)中使用UNIX工具等多个文本文件作为awk或者sed。

I wish to swap or transpose pairs of lines according to their line-numbers (e.g., switching the positions of lines 10 and 15) in multiple text files using a UNIX tool such as sed or awk.

例如,我相信这个sed命令应该掉在一个文件中的行14和26:

For example, I believe this sed command should swap lines 14 and 26 in a single file:

sed -n '14p' infile_name > outfile_name
sed -n '26p' infile_name >> outfile_name

这怎么能扩展到多个文件?任何一个班轮解决方案的欢迎。

How can this be extended to work on multiple files? Any one-liner solutions welcome.

推荐答案

这可能会为你工作(GNU SED):

This might work for you (GNU sed):

sed -ri '10,15!b;10h;10!H;15!d;x;s/^([^\n]*)(.*\n)(.*)/\3\2\1/' f1 f2 fn

该存储范围内保留空间线,然后交换范围的完成后的第一个和最后一个行。

This stores a range of lines in the hold space and then swaps the first and last lines following the completion of the range.

I 标记编辑每个文件( F1 F2 ... FN )。

The i flag edits each file (f1,f2 ... fn) in place.

这篇关于Unix的单行交换/转在多个文本文件的两行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆