string - Conditionally add characters to beginning of every line in a file - Stack Overflow

I am attempting to create a bash script that prepends characters to the start of all lines in a markdow

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

Share Improve this question edited Mar 26 at 16:48 Timur Shtatland 12.4k3 gold badges38 silver badges64 bronze badges asked Mar 26 at 2:51 BirdsAreBastardsBirdsAreBastards 311 bronze badge 7
  • 1 talisman./~erlkonig/documents/… – William Pursell Commented Mar 26 at 3:21
  • 2 Please edit your question and add your desired output for that sample input. – Cyrus Commented Mar 26 at 5:30
  • 1 It is ambiguous about what you want to do with the blank line and the "123" line. – tshiono Commented Mar 26 at 5:37
  • Simply using sed instead of awk, you could: sed '/^[^#]/s/^/PREFIX /' – F. Hauri - Give Up GitHub Commented Mar 26 at 7:34
  • TIMTOWTDI: pure bash: while 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
 |  Show 2 more comments

7 Answers 7

Reset to default 2

This 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 switches
  • perldoc 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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信