Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Bash regex finding and replacing
- <3 digit number> <data>
- <word>:<4 digit number>
- 020 Word
- 021 Word:0001
- Replace with
- 020 021
- 021 0210001
- #!/usr/bin/perl
- $in= "input.txt";
- $out= "output.txt";
- # Buffer the whole file for replacing:
- open(INFILE, $in);
- @lines = <INFILE>;
- open(INFILE, $in);
- # Iterate through each line:
- while(<INFILE>) {
- # If the line matches "word:number", replace all instances in the file
- if (/^(d{3}) (w+:)d{4}$/) {
- $num = $1; word = $2;
- s/$word/$num/ foreach @lines;
- }
- }
- open(OUTFILE, $out);
- print OUTFILE foreach @lines;
- #file name:t
- kent$ cat t
- 020 Word
- 021 Word:0001
- #first we find out the replacement, 021 in this case:
- kent$ v=$(grep -oP "(d{3})(?= Word:d{4})" t|head -n1)
- #do replace by sed:
- kent$ sed -r "s/Word[:]?/$v/g" t
- 020 021
- 021 0210001
- number=$(gawk --posix '/[0-9]{3} '${word}':[0-9]{4}/ { print $1; exit }' $file)
- if [ "$number" != "" ]; then
- sed -r "s/${word}:?/${number}/" $file
- fi
- gawk '
- NR == FNR {
- if (match($2, /^([^:]+):[0-9][0-9][0-9][0-9]$/, a))
- repl[a[1] ":?"] = $1
- next
- }
- {
- for (word in repl)
- if ($2 ~ word) {
- sub(word, repl[word], $2)
- break
- }
- print
- }
- ' filename filename > new.file
- v=`grep -m1 'Word[:][0-9][0-9][0-9][0-9]'< filename | awk '{print $1}'` &&
- sed -i '' 's/Word[:]*/'$v'/g' filename
- # sweep the file and make a lookup table variable
- lookup=$(sed -nr 's/(.*) (.*:).*/21/p' <source_file |tr 'n' ' ')
- # append the lookup to each line and substitute using a backreference
- # N.B. remove the lookup whatever!
- sed -r "s/$/@@${lookup}/;
- s/^(... )(.*)$@@.*2:(S*).*/13/;
- s/^(... )(.*:)(.*)@@.*2(S*).*/143/;
- s/@@.*//" <source_file
Add Comment
Please, Sign In to add comment