Issue
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.
Solution
Yes, sed is a good tool to do it:
sed -E '\%^//.*\|[[:digit:]]+\|% s/.*\|([[:digit:]]+)\|/>\1/'
Explanation:
\%^//.*\|[[:digit:]]+\|%
This is an address, i.e. the followings///
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.
Answered By - choroba Answer Checked By - Candace Johnson (WPSolving Volunteer)