Finding the pattern and replacing the pattern inside the file | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Finding the pattern and replacing the pattern inside the file

Shell Programming and Scripting


Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 07-20-2013
saaisiva saaisiva is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 5 August 2013, 2:26 AM EDT
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
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:


Code:
go to &abc=ddd;
 if file found &ccc=10;
 no the value name is &abc;
  and the age is &ccc;

Output:


Code:
go to &abc=ddd;
 if file found &ccc=10;
now the value name is ddd;
  and the age is 10;


Last edited by saaisiva; 07-25-2013 at 04:26 AM.. Reason: Add CODE tags
Sponsored Links
    #2  
Old 07-20-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 July 2014, 7:43 PM EDT
Location: Galactic Empire
Posts: 3,358
Thanks: 230
Thanked 1,191 Times in 1,124 Posts
So what have you tried so far to overcome this challenge?
Sponsored Links
    #3  
Old 07-24-2013
saaisiva saaisiva is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 5 August 2013, 2:26 AM EDT
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Finding the pattern and replacing the pattern inside the file

Going to work on this part again. .

---------- Post updated 07-24-13 at 03:23 AM ---------- Previous update was 07-23-13 at 08:32 AM ----------

I have used following codes but i didnt get the required result:

Code:
sed -i "s|${variable}|${value_map[${variable}]}|g" "${input_file}"

---------- Post updated at 06:11 AM ---------- Previous update was at 03:23 AM ----------

I am getting error(command not found) if i use this code: Help me out in this:


Code:
#set -x
grep -i "&*=" sample.txt >sample1.txt
#file="Replace_sample.txt"
while read line
do
  var=`echo $line |cut -f1 -d '='`
  val=`echo $var |cut -f2 -d '='`
sed -i  's|${var}|${val}|g' "sample.txt"
done < "sample1.txt"

    #4  
Old 07-24-2013
Yoda's Avatar
Yoda Yoda is offline Forum Advisor  
Jedi Master
 
Join Date: Jan 2012
Last Activity: 30 July 2014, 7:43 PM EDT
Location: Galactic Empire
Posts: 3,358
Thanks: 230
Thanked 1,191 Times in 1,124 Posts
Using awk:

Code:
awk '
        /&/ && /=/ {
                match ( $0, /&.*=/ )
                var = sprintf ( "%s", substr ( $0, RSTART, RLENGTH - 1 ) )
                match ( $0, /=.*;/ )
                val = sprintf ( "%s", substr ( $0, RSTART + 1, RLENGTH - 2 ) )
                A[var] = val
        }
        /&/ && !/=/ {
                match ( $0, /&.*;/ )
                idx = sprintf ( "%s", substr ( $0, RSTART, RLENGTH - 1 ) )
                for ( i = 1; i <= NF; i++ )
                {
                        if ( $i == substr ( $0, RSTART, RLENGTH ) )
                                $i = A[idx]
                }
        }
        1
' file

Sponsored Links
    #5  
Old 07-24-2013
saaisiva saaisiva is offline
Registered User
 
Join Date: Jul 2013
Last Activity: 5 August 2013, 2:26 AM EDT
Posts: 16
Thanks: 2
Thanked 0 Times in 0 Posts
Hi Yoda,
It is not working for the complex things like the text below: i have 500 to 1000 lines in the txt file which i have to replace.


Code:
I have a lerarne  a &lesson=ddd;
which i have to for &mm=22;
asdfasd &G=3;
sad &aa=sss
sor sdfsdf dsfsdfs &lesson
saasdasdasd
d ther asdfadd &lesson
asda &aa
asdasdasd&AA

Sponsored Links
    #6  
Old 07-24-2013
rdrtx1 rdrtx1 is offline
Registered User
 
Join Date: Sep 2012
Last Activity: 15 July 2014, 11:14 AM EDT
Location: Houston, Texas, USA
Posts: 675
Thanks: 0
Thanked 203 Times in 195 Posts
try also:

Code:
awk '
{if ($0 ~/[&].*[=]/) {
    l=$0; gsub("[^0-9A-Za-z& ]", " & ", l);
    c=split(l,a); for (i=1; i<=c; i++) if (a[i] == "=") r[a[i-1]]=a[i+1];
  } else {
    {for (i in r) { if (sub(i, r[i])) if ($1 == "no") sub("no", "now", $1) }; s=0}
  }
} 1
' sample.txt

Sponsored Links
    #7  
Old 07-24-2013
RudiC RudiC is online now Forum Advisor  
Registered User
 
Join Date: Jul 2012
Last Activity: 31 July 2014, 7:27 AM EDT
Location: Aachen, Germany
Posts: 3,939
Thanks: 63
Thanked 936 Times in 888 Posts
And, last but not least, try as well:
Code:
awk     'm=match ($0, /&.*=.*;/)        {split (substr($0,RSTART,RLENGTH-1), T, "=");  VAL[T[1]]=T[2]}
         /&/ && !m                      {for (i=1; i<=NF; i++) if ($i in VAL) $i=VAL[$i]}
         1
        ' file

You are not quite consistent between your specifications: sometimes the "variables" are terminated with a ";" sometimes they are not. Remove the -1 from RLENGTH if you want it included.
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Replacing a pattern in different cases in different columns with a single pattern sudeep.id Shell Programming and Scripting 5 05-15-2012 11:04 PM
CSV: Replacing multiple occurrences inside a pattern OCanada Shell Programming and Scripting 1 12-08-2010 08:56 PM
Need help in sed command ( Replacing a pattern inside a file with a variable value ) frozensmilz Shell Programming and Scripting 2 12-22-2008 05:40 AM
Finding Last occurance of another pattern when a pattern is found. rakeshou Shell Programming and Scripting 1 07-23-2008 03:29 PM
help with finding & replacing pattern in a file dusoo Shell Programming and Scripting 0 05-23-2008 03:59 AM



All times are GMT -4. The time now is 07:27 AM.