How to Add Strings "<Fc=Ff00>" Before Any Line Containing The Pattern [:Alpha:], But Not Add Two Consecutive Lines

How to add strings fc=ff00 before any line containing the pattern [:alpha:], but not add two consecutive lines?

$ awk '!f && /[[:alpha:]]/{print "<fc=ff00>" $0; f=1; next} {print; f=0}' file
01
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters.
02
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters
Alphabetic characters,
03
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters,

How to add strings “ /font” after lines beginning with fc=ff00, but not add two consecutive lines?

This is what I think you're really trying to do:

$ cat file
01
00:00:00,000 --> 00:00:00,000
Alphabetic characters.
02
00:00:00,000 --> 00:00:00,000
Alphabetic characters
Alphabetic characters,
03
00:00:00,000 --> 00:00:00,000
Alphabetic characters,

$ cat tst.awk
/^[[:alpha:]]/ { buf = (buf ? buf RS : "") $0; next }
buf { printf "<fc=ff00>%s</font>\n", buf; buf="" }
{ print }
END { if (buf) printf "<fc=ff00>%s</font>\n", buf }

$ awk -f tst.awk file
01
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters.</font>
02
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters
Alphabetic characters,</font>
03
00:00:00,000 --> 00:00:00,000
<fc=ff00>Alphabetic characters,</font>

sed: joining lines depending on the fourth one

sed is for simple subsitutions on individual lines, that is all. For anything else you should be using awk:

$ awk '/[[:alpha:]]/{ if (buf=="") {buf=$0; next} else {$0=buf OFS $0; buf=""} } 1' file

5

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

6

00:00:00,000 --> 00:00:00,000

7

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

8

00:00:00,000 --> 00:00:00,000

Alphabetic characters Alphabetic characters

.....

The above will work robustly, portably, and efficiently on all UNIX systems with all POSIX-compatible awks.

add a once string matched using sed in multiple lines

with awk. For all fields, if field matches pattern, add quotes. Finally print the line (1 means the default action which is to print line)

awk '{ for (i=1;i<=NF;i++) if ($i ~ /^0[A-Za-z0-9]*$/) $i="\""$i"\"" } 1' file

How can I make a Regex that will match only alpha characters?

The answer is easy :).
You tried to match your pattern with the whole inputString "beta", but you splitted the "beta" already and you want to match the pattern not with beta, but with one element of your array "data".

Change this:

if (Pattern.matches("[a-zA-Z]+", beta)){}

to this:

for(String word : data)
{
Pattern.matches("[a-zA-Z]+", word)
}

You have to handle the situation where the pattern not match, but thats your work. :)
I hope it helps.

Regular Expression to match only alphabetic characters

You may use any of these 2 variants:

/^[A-Z]+$/i
/^[A-Za-z]+$/

to match an input string of ASCII alphabets.

  • [A-Za-z] will match all the alphabets (both lowercase and uppercase).
  • ^ and $ will make sure that nothing but these alphabets will be matched.

Code:

preg_match('/^[A-Z]+$/i', "abcAbc^Xyz", $m);
var_dump($m);

Output:

array(0) {
}

Test case is for OP's comment that he wants to match only if there are 1 or more alphabets present in the input. As you can see in the test case that matches failed because there was ^ in the input string abcAbc^Xyz.

Note: Please note that the above answer only matches ASCII alphabets and doesn't match Unicode characters. If you want to match Unicode letters then use:

/^\p{L}+$/u

Here, \p{L} matches any kind of letter from any language

Regex To Capture Alpha-Numeric and Ignore Patterns (like Interpolation) and Symbols

Using php, you might use a pattern like this to exclude the pattern */..../* or non word characters except whitespace chars:

(?:\*/.*?/\*|[^\w\s]+)(*SKIP)(*F)|\w+

The pattern in parts:

  • (?: Non capture group for the alternatives
    • \*/.*?/\* Match from */../* non greedy to stop at the first occurrence
    • | Or
    • [^\w\s]+ Match one or more non word characters excluding whitespace chars
  • ) Close the non capture group
  • (*SKIP)(*F) Skip the match
  • | Or
  • \w+ Match 1 or more word characters

Regex demo

Replace whole line containing a string using Sed

You can use the change command to replace the entire line, and the -i flag to make the changes in-place. For example, using GNU sed:

sed -i '/TEXT_TO_BE_REPLACED/c\This line is removed by the admin.' /tmp/foo


Related Topics



Leave a reply



Submit