New answers tagged

0 votes

awk print only modified lines

awk -v k=1100 '{if(length($2) !=4 && length($2)>0){k=k+1;print $1,$2,k }}' file.txt output d1001 100 1101 d1002 10 1102 d1003 1 1103
user avatar
4 votes
Accepted

awk print only modified lines

You can print the existing line ($0) and a new field like this, , will use the output separator between the arguments. awk -v n=1100 'length($2)!=4 {print $0,++n}' file Output: d1001 100 1101 d1002 ...
user avatar
  • 6,040
0 votes

How can I replace a character within a specific context in each line of the whole file?

Using Raku (formerly known as Perl_6) raku -pe 'state $ph; $ph = 0 if /^ \\phrase $/; s:g/r/ɹ/ if ++$ph == 4;' You might want to try Raku, since it was built from the ground-up to handle Unicode. ...
user avatar
  • 1,098
6 votes

How can I replace a character within a specific context in each line of the whole file?

With standard sed: sed '/^\\phrase$/{n;n;n;s/r/ɹ/g;}' y/r/ɹ/ in place of s/r/ɹ/g would also work in POSIX compliant sed implementations provided the ɹ character is regarded as one in the user's ...
user avatar
3 votes

How can I replace a character within a specific context in each line of the whole file?

perl -Mutf8 -CSD -pe '$phrase = $. if /\\phrase/; s/r/ɹ/g if $. == $phrase + 3' fairly strightforward; set flags for unicode handling, remember the line number ($.) if we see \phrase, and do a ...
user avatar
  • 677
9 votes
Accepted

How can I replace a character within a specific context in each line of the whole file?

Since you're on Emacs... The Evil/Vim Way If you have evil-mode installed (or you switch to Vim), you can do this: :g/^\\phrase/+3s/r/ɹ/g That's the simplest. The Keyboard Macro Way Staying with ...
user avatar
  • 4,072
11 votes

How can I replace a character within a specific context in each line of the whole file?

awk 'c&&!--c {gsub(/r/,"ɹ")} /\\phrase/ {c=3} 1' file > newfile c&&!--c is a common awk idiom, implementing the while getline logic, see reference. The action following ...
user avatar
  • 6,040
2 votes

How can I replace a character within a specific context in each line of the whole file?

Since we're getting other answers here's a worked solution from an almost-duplicate question. This is for GNU sed, but on the linked answer there are also POSIX suggestions: sed '/^\\phrase/,+3 { /^\\...
user avatar
  • 88.9k
7 votes

How can I replace a character within a specific context in each line of the whole file?

If you always have a blank line between each section, you can try perl's "paragraph" mode to read each section as a single "line": $ perl -F'\n' -00ane '$F[3]=~s/r/ɹ/g; print join &...
user avatar
  • 215k
18 votes

How can I replace a character within a specific context in each line of the whole file?

an awk version (you'll need a relay file, you can one-line it) awk '/\\phrase/ { p=NR ; } NR == p+3 { gsub("r","ɹ") ; } {print;} ' old-file.tex > new-file.tex where ...
user avatar
  • 29.3k

Top 50 recent answers are included