LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Replacing a particular position in a file thru shell script in Linux (https://www.linuxquestions.org/questions/programming-9/replacing-a-particular-position-in-a-file-thru-shell-script-in-linux-335393/)

sgracelin 06-20-2005 07:39 AM

Replacing a particular position in a file thru shell script in Linux
 
My file content is

"4444412000 ":"444441001362":"Kodiak2Grp "
"9880222221 ":"333331001362":"TestGrp1 "
"3333312221 ":"333331333331":"TestGrp1 "
"3333312221 ":"333333313331":"TestGrp1 "

The file has three fields....Requirement is in the second column if the first 6 characters match the string then the first six characters only should be replaced with another string..[if the last 6 or the any combination of six characters in the second column matches other than first 6 characters, then it should not be replaced]...Any help on this??

say OLDSTR=333331

NEWSTR=777771

and the output should be

"4444412000 ":"444441001362":"Kodiak2Grp "
"9880222221 ":"777771001362":"TestGrp1 "
"3333312221 ":"777771333331":"TestGrp1 "
"3333312221 ":"333333313331":"TestGrp1 "

the value of the OLDSTR and the NEWSTR will be changed programatically..

jim mcnamara 06-20-2005 09:46 AM

Code:

awk ' BEGIN { FS=":"}
      {
        if(substr($2,2,6)=="333331")
        {
            sub(/33333/,"77777", $2)
        }
        print $0
      } ' filename

Test - input file:
Code:

     
"4444412000 ":"444441001362":"Kodiak2Grp "
"9880222221 ":"333331001362":"TestGrp1 "
"3333312221 ":"333331333331":"TestGrp1 "
"3333312221 ":"333333313331":"TestGrp1 "

result:
Code:

"4444412000 ":"444441001362":"Kodiak2Grp "
"9880222221 " "777771001362" "TestGrp1 "
"3333312221 " "777771333331" "TestGrp1 "
"3333312221 ":"333333313331":"TestGrp1 "


elsheikhmh 06-20-2005 10:31 AM

what about bash script:
Code:

echo file |
cut -d ':' -f1,2,3 | echo $1:$2:${${2:1:5},#33333,77777}

${2:1:5} would extract the substring from $2 begining in postion 1 through 5

substituting by ${var,#old_prefix,new}

actually, i'm "on-break" in work on a microtheft machine :(

does anyone know a free online bash access?

mustafa

chrism01 06-20-2005 07:44 PM

Can you install Cygwin on the MS box?

ta0kira 06-21-2005 01:54 AM

How about this ($1 is the string, $2 is the replacement, $3 is the file):
Code:

#!/bin/bash
rm -f .$3
grep ".*" "$3" | while read nextline
do
{
  begin="`echo "$nextline" | sed -r "s*\"[^\"\n]*\":\"[^\"\n]*\"$**"`"
  end="`echo "$nextline" | sed -r "s*^$begin**"`"
  end2="`echo "$end" | sed -r "s*^\"$1*/\"$2*"`"
  echo "$begin$end2" >> .$3
}
done
echo "" > $3
grep ".*" ".$3" >> $3
rm -f .$3

Sorry, couldn't test it because I don't have a Linux box here right now.

It should take any line "...":"...":"..." and turn it into "...": and "...":"...". Then "...":"..." is checked for "(before) which is replaced with "(after) then is put back together with "...":. (I'm sure I over-complicated it...)
ta0kira

elsheikhmh 06-21-2005 12:23 PM

hi,
i tried something else
Code:

cat /dev/null > temp.2 # force empty file
cut -d ':' -f1 in.txt > temp.1 # first column
cut -d ':' -f3 in.txt > temp.3 # second column
cut -d ':' -f2 in.txt | while read var # target column
do {
        echo $var | sed -e 's/^"33333/"77777/' >> temp.2 # substitute the first 5 three's by 5 seven's
} done;
paste -d ':' temp.1 temp.2 temp.3 > out.txt # add it all again
rm temp.1 temp.2 temp.2 # clean out

i think the most ineffecient script ever written is this one! looping + temp files are fatal deficiencies; eh?

,,
mustafa

ta0kira 06-22-2005 12:55 AM

Code:

#!/bin/bash
text="[^\"\n]*"
quoted_text="\"$text\""
sed -ri "s/^($quoted_text:\")$1($text\":$quoted_text)$/\1$2\2/g" $3

Maybe this? Again, no Linux here so it might need to be tweaked. ($1 is the string, $2 is the replacement, $3 is the file)
ta0kira

PS
Quote:

i think the most ineffecient script ever written is this one! looping + temp files are fatal deficiencies; eh?
Yeah, I didn't like the looping in mine either, but I don't think you can really get around the temp file unless you store the entire file in a $ variable...

keefaz 06-22-2005 07:04 AM

What about perl ?

Code:

perl -pi -e 's/(.*:")(333331)(.*":.*)/${1}777771${3}/' file


All times are GMT -5. The time now is 11:14 PM.