Guest User

Untitled

a guest
Aug 22nd, 2018
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.87 KB | None | 0 0
  1. Bash regex finding and replacing
  2. <3 digit number> <data>
  3.  
  4. <word>:<4 digit number>
  5.  
  6. 020 Word
  7. 021 Word:0001
  8. Replace with
  9. 020 021
  10. 021 0210001
  11.  
  12. #!/usr/bin/perl
  13.  
  14. $in= "input.txt";
  15. $out= "output.txt";
  16.  
  17. # Buffer the whole file for replacing:
  18. open(INFILE, $in);
  19. @lines = <INFILE>;
  20. open(INFILE, $in);
  21.  
  22. # Iterate through each line:
  23. while(<INFILE>) {
  24. # If the line matches "word:number", replace all instances in the file
  25. if (/^(d{3}) (w+:)d{4}$/) {
  26. $num = $1; word = $2;
  27. s/$word/$num/ foreach @lines;
  28. }
  29. }
  30.  
  31. open(OUTFILE, $out);
  32. print OUTFILE foreach @lines;
  33.  
  34. #file name:t
  35. kent$ cat t
  36. 020 Word
  37. 021 Word:0001
  38.  
  39. #first we find out the replacement, 021 in this case:
  40. kent$ v=$(grep -oP "(d{3})(?= Word:d{4})" t|head -n1)
  41.  
  42. #do replace by sed:
  43. kent$ sed -r "s/Word[:]?/$v/g" t
  44. 020 021
  45. 021 0210001
  46.  
  47. number=$(gawk --posix '/[0-9]{3} '${word}':[0-9]{4}/ { print $1; exit }' $file)
  48.  
  49. if [ "$number" != "" ]; then
  50. sed -r "s/${word}:?/${number}/" $file
  51. fi
  52.  
  53. gawk '
  54. NR == FNR {
  55. if (match($2, /^([^:]+):[0-9][0-9][0-9][0-9]$/, a))
  56. repl[a[1] ":?"] = $1
  57. next
  58. }
  59. {
  60. for (word in repl)
  61. if ($2 ~ word) {
  62. sub(word, repl[word], $2)
  63. break
  64. }
  65. print
  66. }
  67. ' filename filename > new.file
  68.  
  69. v=`grep -m1 'Word[:][0-9][0-9][0-9][0-9]'< filename | awk '{print $1}'` &&
  70. sed -i '' 's/Word[:]*/'$v'/g' filename
  71.  
  72. # sweep the file and make a lookup table variable
  73.  
  74. lookup=$(sed -nr 's/(.*) (.*:).*/21/p' <source_file |tr 'n' ' ')
  75.  
  76. # append the lookup to each line and substitute using a backreference
  77. # N.B. remove the lookup whatever!
  78.  
  79. sed -r "s/$/@@${lookup}/;
  80. s/^(... )(.*)$@@.*2:(S*).*/13/;
  81. s/^(... )(.*:)(.*)@@.*2(S*).*/143/;
  82. s/@@.*//" <source_file
Add Comment
Please, Sign In to add comment