I am attempting to create a bash script that prepends characters to the start of all lines in a markdown file which do not begin with a '#' character.
For example, say we have example.md:
# Title
Words
More words
## Title 2
123
I want to add a string (let's say, 'PREFIX') to the beginning of every line except those two title lines.
Relatedly, how would I do this to only lines which begin with a letter?
For clarity, the bash script would look something like this other script I use, trim_duplicate_newlines.sh
:
#!/bin/bash
awk 'BEGIN{RS="\n+" ; ORS="\n";}{ print }'
which I would run in the cmd like this: cat ./example.md | ./trim_duplicate_newlines.sh
I am attempting to create a bash script that prepends characters to the start of all lines in a markdown file which do not begin with a '#' character.
For example, say we have example.md:
# Title
Words
More words
## Title 2
123
I want to add a string (let's say, 'PREFIX') to the beginning of every line except those two title lines.
Relatedly, how would I do this to only lines which begin with a letter?
For clarity, the bash script would look something like this other script I use, trim_duplicate_newlines.sh
:
#!/bin/bash
awk 'BEGIN{RS="\n+" ; ORS="\n";}{ print }'
which I would run in the cmd like this: cat ./example.md | ./trim_duplicate_newlines.sh
7 Answers
Reset to default 2This will do
awk '!/^#/ { printf "PREFIX " } { print }' example.md
The regular expression /^#/
matches the title lines, but the exclamation point negates it.
With GNU sed
:
sed '/^#/!s/^/PREFIX/' file.md
Output:
# Title PREFIXWords PREFIXMore words PREFIX ## Title 2 PREFIX123
I would harness GNU AWK
for this task following way, let file.txt
content be
# Title
Words
More words
## Title 2
123
then
awk '{print (/^#/?"":"PREFIX") $0}' file.txt
gives output
# Title
PREFIXWords
PREFIXMore words
PREFIX
## Title 2
PREFIX123
Explanation: I use so-called ternary operator condition?valueiftrue:valueiffalse so if line starts with (^
) hash (#
) I use empty string (""
) otherwise string dictated by requirements ("PREFIX"
) which I concatenate with whole line ($0
) and print
.
(tested in GNU Awk 5.3.1)
Given the use of RS="\n+"
in the awk script in your question, either of these might be what you're trying to do, using any awk:
$ awk 'BEGIN{RS=""; FS="\n"; OFS="\nPREFIX"} {$1=$1} 1' file
# Title
PREFIXWords
PREFIXMore words
## Title 2
PREFIX123
$ awk 'BEGIN{RS=""; ORS="\n\n"; FS="\n"; OFS="\nPREFIX"} {$1=$1} 1' file
# Title
PREFIXWords
PREFIXMore words
## Title 2
PREFIX123
Use this Perl one-liner:
To add PREFIX
to every line except for those that start with #
:
cat example.md | perl -pe 'next if m{^#}; $_ = "PREFIX${_}"'
To add PREFIX
to every line that starts with a letter (this also skips the lines that start with #
, of course):
cat example.md | perl -pe 'if ( m{^[A-Za-z]} ) { $_ = "PREFIX${_}"; }'
The Perl one-liner uses these command line flags:
-e
: Tells Perl to look for code in-line, instead of in a file.
-p
: Loop over the input one line at a time, assigning it to $_
by default. Add print $_
after each loop iteration.
Regexes:
m{^#}
: Matches literal #
at the beginning of the line.
m{^[A-Za-z]}
: Matches any letter, uppercase or lowercase, at the beginning of the line.
See also:
perldoc perlrun
: how to execute the Perl interpreter: command line switchesperldoc perlre
: Perl regular expressions (regexes)perldoc perlrequick
: Perl regular expressions quick start
awk
one-liner by only modifying FS
:
awk 'NF != 1 || $1 = __$1' FS='^#' __='PREFIX'
# Title
PREFIXWords
PREFIXMore words
## Title 2
PREFIX123
This is quite easy with Raku/Sparrow, here is an example with prepending with PREFIX all the lines started containing 123 but not started with #:
within: ^ 123
!regexp: ^^ \x[23]
end:
code: <<RAKU
!raku
for streams().values -> $s {
replace(
"data.txt",
$s<>[0]<index>,
"PREFIX " ~ $s<>[0]<data>,
);
}
RAKU
Assumption is input data is taken from data.txt file
发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744167156a4561356.html
sed
instead ofawk
, you could:sed '/^[^#]/s/^/PREFIX /'
– F. Hauri - Give Up GitHub Commented Mar 26 at 7:34while read -r line;do case $line in \#*|'' ) echo "$line";; * ) echo PREFIX "$line";;esac;done
– F. Hauri - Give Up GitHub Commented Mar 26 at 7:41