user2512450
user2512450

Reputation: 41

Linux match string and move one line up

I have the following text in a text file on 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

i would like to find a way to match all lines that start with ### and attach them to the previous line, so the final outcome would be.

#### project name ### description 

i have tried several awk and sed statements but couldn't resolve it :|

Thanks, Roy

Upvotes: 0

Views: 308

Answers (4)

Gilles Quénot
Gilles Quénot

Reputation: 185151

Shorter using :

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

Upvotes: 0

potong
potong

Reputation: 58420

This might work for you (GNU sed):

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

This reads two lines at a time into the pattern space and if the second line matches the required string, replaces the newline with a space.

Upvotes: 0

James Buck
James Buck

Reputation: 1640

With : ^(#[^\n]+)\s*(#[^\n]+) and replace all matches with \1 \2 (capture groups 1 & 2).

Demo here: https://regex101.com/r/fY1tO8/1.

Upvotes: 0

Gilles Quénot
Gilles Quénot

Reputation: 185151

With :

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

Output:

#### 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

Upvotes: 2

Related Questions