Linux匹配字符串并移动一行

时间:2015-12-30 23:25:25

标签: regex linux awk sed

我在linux上的文本文件中有以下文字。

#### linkedin-scraper
### Scrapes the  public profile of the linkedin page
git clone https://github.com/yatish27/linkedin-scraper.git
#### rext
### Router EXploitation Toolkit - small toolkit for easy creation and usage of various python scripts that work with embedded devices.
git clone https://github.com/j91321/rext.git
#### in-hackers-mind
### Cyber Security: In Hacker's Mind
git clone https://github.com/Octosec/in-hackers-mind.git
#### tap
### The TrustedSec Attack Platform is a reliable method for droppers on an infrastructure in order to ensure established connections to an organization.
git clone https://github.com/trustedsec/tap.git
#### DocDropper
### REB00T Spear Phishing
git clone https://github.com/tfairane/DocDropper.git
#### WebAppSec
### Web Application Security
git clone https://github.com/ajinabraham/WebAppSec.git

我想找到一种方法来匹配以###开头的所有行并将它们附加到前一行,因此最终的结果将是。

#### project name ### description 

我已经尝试了几个awk和sed语句但无法解决它:|

谢谢, 罗伊

4 个答案:

答案 0 :(得分:2)

使用

$ awk '$1=="####"{x=$0;next} $1=="###"{print x, $0; next} 1' file

输出:

#### linkedin-scraper ### Scrapes the  public profile of the linkedin page
git clone https://github.com/yatish27/linkedin-scraper.git
#### rext ### Router EXploitation Toolkit - small toolkit for easy creation and usage of various python scripts that work with embedded devices.
git clone https://github.com/j91321/rext.git
#### in-hackers-mind ### Cyber Security: In Hacker's Mind
git clone https://github.com/Octosec/in-hackers-mind.git
#### tap ### The TrustedSec Attack Platform is a reliable method for droppers on an infrastructure in order to ensure established connections to an organization.
git clone https://github.com/trustedsec/tap.git
#### DocDropper ### REB00T Spear Phishing
git clone https://github.com/tfairane/DocDropper.git
#### WebAppSec ### Web Application Security
git clone https://github.com/ajinabraham/WebAppSec.git

答案 1 :(得分:0)

使用^(#[^\n]+)\s*(#[^\n]+)并将所有匹配项替换为\1 \2(捕获组1& 2)。

在这里演示:https://regex101.com/r/fY1tO8/1

答案 2 :(得分:0)

这可能适合你(GNU sed):

sed 'N;/\n### /s/\n/ /;P;D' file

这一次读取两行到模式空间中,如果第二行与所需的字符串匹配,则用空格替换换行符。

答案 3 :(得分:0)

使用缩短

perl -0pe 's/\n(#{3}\s+)/ $1/g' file