Replacing all occurences depending on parity


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing all occurences depending on parity
# 1  
Old 10-24-2009
Replacing all occurences depending on parity

I want to replace all occurences of '$' in a LaTeX source code in the following way: The dollars on odd position in the file must be changed in '$latex ' and the other dollars can remain the same.

I was thinking to make a script which replaces all odd occurences of a given character with a string, and all even occurences of the same character with another string.

I would be glad to get some help.
Thanks.
# 2  
Old 10-24-2009
Hi.

Can you post a representative sample of your input data, and expected results (output data).

Thanks.
# 3  
Old 10-24-2009
Input:
Solve the equation $x^2=5$. Prove that $x$ is an irrational number.

Output:
Solve the equation $latex x^2=5$. Prove that $latex x$ is an irrational number.

So the 1st, 3rd, ... (2k+1)th dollar becomes '$latex '. The rest remain the same.

Last edited by beni22sof; 10-24-2009 at 03:15 PM..
# 4  
Old 10-24-2009
Code:
awk -F'$' -v OFS="$" '{$2="latex " $2; $4="latex " $4} 1' file1  
Solve the equation $latex x^2=5$. Prove that $latex x$ is an irrational number.

If you have more than 4 $'s you can always do this in a for-loop:

Code:
awk -F'$' -v OFS='$' '{ for( I = 2; I <= NF; I+=2 )
  $I = "latex " $I
} 1' file1

Solve the equation $latex x^2=5$. Prove that $latex x$ is an irrational number.


Last edited by Scott; 10-24-2009 at 03:34 PM.. Reason: added for loop
# 5  
Old 10-25-2009
perl:
Code:
my $str='$1 first $a second $2 third $b forth $3';
print $str,"\n";
$str=~s/\$
(?=
(?:[^\$]*\$[^\$]*\$)*[^\$]*$)
/-/gx;
print $str,"\n";

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Order as per top occurences

Hi I have a file with entries like below top 5 a 5 b 4 c 3 d 2 e 1 top 5 b 5 d 4 c 3 e 2 a 1 top 5 e 5 d 4 c 3 b 2 a 1 (2 Replies)
Discussion started by: Viswanatheee55
2 Replies

2. Solaris

solaris raids parity

what actually stored in the parity reserved on each slice in raid 5 configuration. how to restore the data if a disk is failed in raid5 configuration (1 Reply)
Discussion started by: revathireddy
1 Replies

3. Shell Programming and Scripting

Awk to count occurences

Hi, i am in need of an awk script to accomplish the following: Input table looks like: Student1 arts Student2 science Student3 arts Student4 science Student5 science Student6 science Student7 science Student8 science Student9 science Student10 science Student11 science... (8 Replies)
Discussion started by: saint2006
8 Replies

4. UNIX for Dummies Questions & Answers

Parity, SRM, VSA/Fusion, Service Portal, Remotely Anywhere, and Tivoli TEC.

Can some one explain or send me the links about these tools Parity, SRM, VSA/Fusion, Service Portal, Remotely Anywhere, and Tivoli TEC. (2 Replies)
Discussion started by: rags
2 Replies

5. Shell Programming and Scripting

Perl - Count occurences

I have enclosed the script. I am able to find the files that contain my search string but when I try to count the occurences within the file I get zero always. Any help on this. #!/usr/bin/perl my $find = $ARGV; my $replace = $ARGV; my $glob = $ARGV; @filelist = <*$glob>; # process each... (22 Replies)
Discussion started by: TimHortons
22 Replies

6. Linux

kernel: Additional sense: Scsi parity error ()

Hey guys how do you troubleshoot this kind of problem kernel: Additional sense: Scsi parity error () (2 Replies)
Discussion started by: sbn
2 Replies

7. Shell Programming and Scripting

no of occurences of q word

hi I hace a string "abc,def,ghi,abc,def ,ghi,abc,def,ghi,abc,def ,ghi,abc" i replaced commas with spaces, now i want to calculate nof occurences of "abc" word. thanks in advance Satya (6 Replies)
Discussion started by: Satyak
6 Replies

8. Shell Programming and Scripting

occurences of words

I have a string like this. $str="The astrocyte profile might contribute to the identification of possible therapies in profiles profiling and profiled als."; Lets consider for example: a)If user enters the term profile* it should highlight profile,profiles only. b)If user enters the... (3 Replies)
Discussion started by: vanitham
3 Replies

9. UNIX for Dummies Questions & Answers

Counting number of occurences

Hi All, I have to count the number of occurences of the character " ; " in a given line. I had used the following awk command to achieve the same echo $KOP.dat|awk '{split($1,my,";"); for(i in my)c++ }END{print c-1}' My file KOP.dat had the following data ... (1 Reply)
Discussion started by: kingofprussia
1 Replies
Login or Register to Ask a Question