Home > database >  Replace Line with Matching Pattern
Replace Line with Matching Pattern

Time:01-26

I have a file filled with two line couplets, a header line that starts with // and ends with the line number surrounded by |, followed by a line of text that is variable in length. Here's an example, where ... indicates that the file continues with sequential line numbers for many lines.

// *  *   -  - - *  |1|
textextextextextextextext
// *- *-*       * |2|
textextextextextextextexttextextextext
...
// * - * -*                          |41232|
textextextextextextext

I would like to find the line number, then replace the header line with >linenumber. This example file would be:

>1
textextextextextextextext
>2
textextextextextextextexttextextextext
...
>41232
textextextextextextext

I know this probably involves /s and sed or awk with a backreference, but I'm just can't seem to make it work.

CodePudding user response:

A sed one-liner:

sed 's%^//.*|\([0-9]*\)|$%>\1%' file

CodePudding user response:

Yes, sed is a good tool to do it:

sed -E '\%^//.*\|[[:digit:]] \|% s/.*\|([[:digit:]] )\|/>\1/' 

Explanation:

  • \%^//.*\|[[:digit:]] \|% This is an address, i.e. the following s/// command will only run on matching lines. It matches // at the start of a line, and somewhere later there must be a number between vertical bars.
  • The substitution replaces the whole line with the captured number.

CodePudding user response:

Using any awk:

$ awk -F'|' 'NF>1{$0=">" $2} 1' file
>1
textextextextextextextext
>2
textextextextextextextexttextextextext
...
>41232
textextextextextextext

The above assumes that text never includes |s.

  • Related