Replacing individual characters with a pattern.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing individual characters with a pattern.
# 1  
Old 11-24-2011
Replacing individual characters with a pattern.

Given a paragraph of text which individual words have been prepended with an equals sign, I want to replace the individual characters in each of the marked words with blank spaces (two underscores and a space) for students to fill in -- a cloze passage that lets the students know how many letters are in each of the words they are required to fill.

As an example, something like:
Code:
Truth be told, this is the =closest
 I've =ever =gotten to a =college =graduation.

should give:
Code:
Truth be told, this is the __ __ __ __ __ __ __  
I've __ __ __ __     __ __ __ __ __ __  to a 
__ __ __ __ __ __ __ __ __ __.

another variation I would like to be able to get is :
Code:
Truth be told, this is the c __ __ __ __ __ __  I've e __ __ __  
g__ __ __ __ __  to a g __ __ __ __ __ __ __ __ __.

I'm not quite sure how to go about this but I suspect awk will be the tool to use.
# 2  
Old 11-24-2011
What have you done so far?
The second version would be easier...

How would you know from when or what is to be substituted?
# 3  
Old 11-24-2011
Try this...
Code:
#Variation 1
awk '{ for(i=1;i<=NF;i++) {
                if($i~"=") {
                        l=length($i)-1
                        if($i~"[\\.,]$"){
                          l=l-1; e=substr($i,l+2,l) }
                        for(j=1;j<=l;j++) p=p"_ ";
                        $i=p;printf $i e OFS;p="";e=""
                } else { printf $i OFS }
        }printf "\n" } ' input_file

#Variation 2
awk '{ for(i=1;i<=NF;i++) {
                if($i~"=") {
                        l=length($i)-1
                        if($i~"[\\.,]$"){
                          l=l-1; e=substr($i,l+2,l) }
                        s=substr($i,2,1);
                        for(j=2;j<=l;j++) p=p"_ ";
                        $i=p;printf s $i e OFS;p="";e=""
                } else { printf $i OFS }
        }printf "\n" } ' input_file

--ahamed
This User Gave Thanks to ahamed101 For This Post:
# 4  
Old 11-24-2011
Another variation:
Code:
#  cat x.txt
Truth be told, this is the =closest
 I've =ever =gotten to a =college =graduation.
# awk '{for (i=1;i<=NF;i++) { if ($i ~ /^=/) { $i=substr($i,2); gsub ("[[:alpha:]]","__ ", $i) }} print}' x.txt
Truth be told, this is the __ __ __ __ __ __ __
I've __ __ __ __  __ __ __ __ __ __  to a __ __ __ __ __ __ __  __ __ __ __ __ __ __ __ __ __ .

These 2 Users Gave Thanks to CarloM For This Post:
# 5  
Old 11-24-2011
ahhhh!!!
Quote:
Originally Posted by CarloM
Another variation:
Code:
#  cat x.txt
Truth be told, this is the =closest
 I've =ever =gotten to a =college =graduation.
# awk '{for (i=1;i<=NF;i++) { if ($i ~ /^=/) { $i=substr($i,2); gsub ("[[:alpha:]]","__ ", $i) }} 
print}' x.txt
Truth be told, this is the __ __ __ __ __ __ __
I've __ __ __ __  __ __ __ __ __ __  to a __ __ __ __ __ __ __  __ __ __ __ __ __ __ __ __ __ .

I was trying everything for this gsub ("[[:alpha:]]","__ ", $i).
Awesome code CarloM Smilie And thanks for the info!
My code looks like junk! Smilie

--ahamed
# 6  
Old 11-24-2011
this is fun!

Code:
perl -pe 's/(?:=)(\w+)\w/\1__ /g ; 1 while s/(\w)(\w)((__ )+)/\1__ \3/' < input-text-file 

Truth be told, this is the c__ __ __ __ __ __ 
 I've e__ __ __  g__ __ __ __ __  to a c__ __ __ __ __ __  g__ __ __ __ __ __ __ __ __ .

[/code]
This User Gave Thanks to bigearsbilly For This Post:
# 7  
Old 11-24-2011
Quote:
Originally Posted by ahamed101
Try this...
Code:
#Variation 1
awk '{ for(i=1;i<=NF;i++) {
                if($i~"=") {
                        l=length($i)-1
                        if($i~"[\\.,]$"){
                          l=l-1; e=substr($i,l+2,l) }
                        for(j=1;j<=l;j++) p=p"_ ";
                        $i=p;printf $i e OFS;p="";e=""
                } else { printf $i OFS }
        }printf "\n" } ' input_file

#Variation 2
awk '{ for(i=1;i<=NF;i++) {
                if($i~"=") {
                        l=length($i)-1
                        if($i~"[\\.,]$"){
                          l=l-1; e=substr($i,l+2,l) }
                        s=substr($i,2,1);
                        for(j=2;j<=l;j++) p=p"_ ";
                        $i=p;printf s $i e OFS;p="";e=""
                } else { printf $i OFS }
        }printf "\n" } ' input_file

--ahamed
Work brilliantly. Thank you.

---------- Post updated at 01:17 AM ---------- Previous update was at 01:11 AM ----------

Quote:
Originally Posted by CarloM
Another variation:
Code:
#  cat x.txt
Truth be told, this is the =closest
 I've =ever =gotten to a =college =graduation.
# awk '{for (i=1;i<=NF;i++) { if ($i ~ /^=/) { $i=substr($i,2); gsub ("[[:alpha:]]","__ ", $i) }} print}' x.txt
Truth be told, this is the __ __ __ __ __ __ __
I've __ __ __ __  __ __ __ __ __ __  to a __ __ __ __ __ __ __  __ __ __ __ __ __ __ __ __ __ .

Works brilliantly, too. Just one question. I use the bash shell and can usually repeat the previous command by using the up arrow. This command doesn't show up. When I press the up arrow, the command before it appears. What gives?

[COLOR="#738fbf"][SIZE=1]---------- Post updated at 01:23 AM ----------

---------- Post updated at 01:26 AM ---------- Previous update was at 01:23 AM ----------

Quote:
Originally Posted by bigearsbilly
this is fun!

Code:
perl -pe 's/(?:=)(\w+)\w/\1__ /g ; 1 while s/(\w)(\w)((__ )+)/\1__ \3/' < input-text-file 

Truth be told, this is the c__ __ __ __ __ __ 
 I've e__ __ __  g__ __ __ __ __  to a c__ __ __ __ __ __  g__ __ __ __ __ __ __ __ __ .

[/code]
Works as advertised. Thanks. Pity I am illiterate in perl.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Finding the pattern and replacing the pattern inside the file

i have little challenge, help me out.i have a file where i have a value declared and and i have to replace the value when called. for example i have the value for abc and ccc. now i have to substitute the value of value abc and ccc in the place of them. Input File: go to &abc=ddd; if... (16 Replies)
Discussion started by: saaisiva
16 Replies

2. Shell Programming and Scripting

Replacing a pattern in different cases in different columns with a single pattern

Hi All I am having pipe seperated inputs like Adam|PeteR|Josh|PEter Nick|Rave|Simon|Paul Steve|smith|PETER|Josh Andrew|Daniel|StAlin|peter Rick|PETer|ADam|RAVE i want to repleace all the occurrence of peter (in any case pattern PeteR,PEter,PETER,peter,PETer) with Peter so that output... (5 Replies)
Discussion started by: sudeep.id
5 Replies

3. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

4. Shell Programming and Scripting

Replacing Complex Characters in vi

ok, so i have a number of commands like the below in a script: /usr/bin/awk "/ I need to replace the bolded with "^\ but i'm having tremendous difficulties accomplishing this. This needs to be done in vi. can someone please help me out? (4 Replies)
Discussion started by: SkySmart
4 Replies

5. Shell Programming and Scripting

Replacing characters

Hi fellow experts, I have a question for you. Data looks like: 00877,05/13/2010,PBO,P,0000708331,518 00877,05/13/2010,PBO,P,0000708331,519 ... ... 00877,05/13/2010,PBO,P,0000708331,2103 00877,05/13/2010,PBO,P,0000708331,2104,etc,etc Basically I have to replace 518,519,2103,2104,... (4 Replies)
Discussion started by: Devski123
4 Replies

6. Shell Programming and Scripting

Replacing Characters with |

Hi All, example data.log 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 526569346 66815531961 09 I want like this to 526569346|66815531961|09 526569346|66815531961|09... (4 Replies)
Discussion started by: ooilinlove
4 Replies

7. Shell Programming and Scripting

Replacing Characters

Hi All, I have a file which is delimeted with the character '. i need to replace this character with the same character and also a new line. Can anyone please help me with the tr command for this. Many thanks Karan (11 Replies)
Discussion started by: karansachdeva
11 Replies

8. Shell Programming and Scripting

replacing characters

hi all I have a file that has sone spaces in start then / at last. i want to get rid of this. how to do? eg. 11414/ 49878/ 27627/ I WANT THE FILE AS 11414 49878 27627 PLEASE HELP (3 Replies)
Discussion started by: infyanurag
3 Replies

9. UNIX for Dummies Questions & Answers

replacing characters

Hi, I have a script for replacing bad characters in filenames for f in *; do mv $f `echo $f | tr '+' '_'` done; this replaces + for _ But I need to replace all bad characters ? / % + to _ Pls how can i do this in one script ? (3 Replies)
Discussion started by: palmer18
3 Replies

10. UNIX for Dummies Questions & Answers

replacing few characters in a file

Hi All, I have huge xml file. The file contains some comment tags . I have requirement to replace comment tag with another comment tag. Say for example : file X has -- Part of the file <?xml version="1.0" encoding="ISO-2"?><translationResults jobDate="20070123 23:20:51"... (1 Reply)
Discussion started by: purnakarthik
1 Replies
Login or Register to Ask a Question