LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 06-20-2005, 07:39 AM   #1
sgracelin
LQ Newbie
 
Registered: Jun 2005
Posts: 10

Rep: Reputation: 0
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..
 
Old 06-20-2005, 09:46 AM   #2
jim mcnamara
Member
 
Registered: May 2002
Posts: 964

Rep: Reputation: 34
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 "
 
Old 06-20-2005, 10:31 AM   #3
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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
 
Old 06-20-2005, 07:44 PM   #4
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,261

Rep: Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028Reputation: 2028
Can you install Cygwin on the MS box?
 
Old 06-21-2005, 01:54 AM   #5
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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

Last edited by ta0kira; 06-21-2005 at 02:08 AM.
 
Old 06-21-2005, 12:23 PM   #6
elsheikhmh
Member
 
Registered: Aug 2004
Location: Cairo, Egypt
Distribution: Slackware
Posts: 101

Rep: Reputation: 15
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
 
Old 06-22-2005, 12:55 AM   #7
ta0kira
Senior Member
 
Registered: Sep 2004
Distribution: FreeBSD 9.1, Kubuntu 12.10
Posts: 3,078

Rep: Reputation: Disabled
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...

Last edited by ta0kira; 06-23-2005 at 04:22 AM.
 
Old 06-22-2005, 07:04 AM   #8
keefaz
Senior Member
 
Registered: Mar 2004
Distribution: Slackware
Posts: 4,443

Rep: Reputation: 94
What about perl ?

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
File Size Shell Script BlackLabel Programming 7 11-27-2007 07:48 PM
Replacing a particular position in a file sgracelin Linux - Newbie 14 06-23-2005 06:40 AM
Replacing a particular position in a file sgracelin Linux - Newbie 1 06-20-2005 05:54 PM
How can I check wether a file is empty in Linux by using shell script programming ??? Bassam Linux - General 2 02-26-2004 01:01 AM
How to read ans parse MS word file using a Linux Shell script. Alek Linux - General 2 11-10-2003 02:07 PM


All times are GMT -5. The time now is 10:43 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration