Replace last character of every second line


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace last character of every second line
# 1  
Old 10-14-2010
Replace last character of every second line

Hi all.

I wonder if this possible.... any help advice is very much appreciated..

I n a shell script I create a latex file that looks like this

\documentclass{article}
\usepackage{graphics}
\begin{document}
\begin{figure}
\begin{center}
\begin{tabular}{cc}

\resizebox{60mm}{!}{\includegraphics{e20011730124.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730225.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730326.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730428.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730529.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730631.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730732.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730834.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730935.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011731036.eps}} &


\end{tabular}
\caption{This is sample figures.}
\label{test4}
\end{center}
\end{figure}
\end{document}

Now I want to change this file so that it looks like as shown below. The highlighted part, I change every second line to replace the '&' by the '\\' chanracter

\documentclass{article}
\usepackage{graphics}
\begin{document}
\begin{figure}
\begin{center}
\begin{tabular}{cc}

\resizebox{60mm}{!}{\includegraphics{e20011730124.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730225.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730326.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730428.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730529.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730631.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730732.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730834.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730935.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011731036.eps}} \\


\end{tabular}
\caption{This is sample figures.}
\label{test4}
\end{center}
\end{figure}
\end{document}


Any one has an idea please help!
Malandisa
# 2  
Old 10-14-2010
What about using code tags for a start?
# 3  
Old 10-14-2010
vbe,

I appreciate your advice, I am very new to forums and I see people post things here in nicer ways but I had no idea how to do that. Just now after your reply I am looking through here and I see there is something I can do to make my post nicer and more professional.

Thanks you for the advise. and here my questions perhaps in a nicer way...


Hi all.

I wonder if this possible.... any help advice is very much appreciated..

I n a shell script I create a latex file that looks like this
Code:
\documentclass{article}
\usepackage{graphics}
\begin{document}
\begin{figure}
\begin{center}
\begin{tabular}{cc}

\resizebox{60mm}{!}{\includegraphics{e20011730124.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730225.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730326.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730428.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730529.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730631.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730732.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730834.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730935.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011731036.eps}} &

\end{tabular}
\caption{This is sample figures.}
\label{test4}
\end{center}
\end{figure}
\end{document}

Now I want to change this file so that it looks like as shown below. The highlighted part, I change every second line to replace the '&' by the '\\' character

Code:
\documentclass{article}
\usepackage{graphics}
\begin{document}
\begin{figure}
\begin{center}
\begin{tabular}{cc}

\resizebox{60mm}{!}{\includegraphics{e20011730124.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730225.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730326.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730428.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730529.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730631.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730732.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011730834.eps}} \\
\resizebox{60mm}{!}{\includegraphics{e20011730935.eps}} &
\resizebox{60mm}{!}{\includegraphics{e20011731036.eps}} \\

\end{tabular}
\caption{This is sample figures.}
\label{test4}
\end{center}
\end{figure}
\end{document}


Any one has an idea please help!
Malandisa
# 4  
Old 10-14-2010
Code:
sed '/\\resizebox/{n;s/&$/\\\\/}' infile


Last edited by Scrutinizer; 10-14-2010 at 02:46 PM.. Reason: Removed backslash in front of the & (superfluous)
# 5  
Old 10-14-2010
Try this:
Code:
awk '
/\\begin{tabular}/{f=1} 
f && /resizebox/ && c++%2 {$NF="\\\\"}
f && /\\end{tabular}/{f=0;c=0} 
1' file

or maybe:

Code:
awk '/resizebox/&&c++%2{$NF="\\\\"}1' file

# 6  
Old 10-14-2010
Maybe this:

Code:
awk 'BEGIN {countRow=1} !/resize/ {print $0} /resize/ {if((countRow%2)!=0){print $1 " & "}else{print $1 " \\\\ "} countRow++}' infile

# 7  
Old 10-14-2010
Quote:
Originally Posted by Scrutinizer
Code:
sed '/\\resizebox/{n;s/\&$/\\\\/}' infile

Just in case there are not modulo-two resizebox lines, and in pretty indented form with shell stuff on different lines than sed stuff:

Code:
sed '
  /\\resizebox/{
    n
    s/\(\n\\resizebox.*\)\&$/\1\\\\/
   }
 ' infile

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace Control M (^M) character with new line

Hi All, We are getting an external file in abc.csv format. When opened in excel spread sheet, it is opening alright. But when opened in notepad, I see the contents in a single line. Ftp'd the file in binary mode to AIX UNIX host. When opened, I see Control M (^M) characters in place of New Line... (16 Replies)
Discussion started by: njny
16 Replies

2. Shell Programming and Scripting

Replace x-y character in line

I have a file which has n number of lines, i want to replace 3rd line position 3-5 to some text. 111111111111111111 222222222222222222 333333333333333333 444444444444444444 expected output 111111111111111111 222222222222222222 33abc3333333333333 444444444444444444 it is... (7 Replies)
Discussion started by: greenworld123
7 Replies

3. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

4. Shell Programming and Scripting

How to replace all after 4th character in line ?

How to replace all after 4 character at line? & convert to upper case ? google.com -> GOOG (2 Replies)
Discussion started by: Trump
2 Replies

5. Shell Programming and Scripting

how to replace character in the line

I have unix text file which has the following data aadjdfad;fa;fjjd;lakd;lkaslkd;k;k;lk;k;lk;l;lk;lkj;lj;lkj;k;lkj;lj;lkj;lkj;lkj;j sdkadk;adlf;lajf;akdjf;lkdjf;lkadjf;lkajsd;lfkj;lkj;lkj;lk;lk;lk;lk;k;lkj;k;lkm... (2 Replies)
Discussion started by: Raju Datla
2 Replies

6. UNIX for Dummies Questions & Answers

match a character in a line and replace

Hi, I have a file with large number of records. Sample below: 123456789QWERT2U 2 erter 987123678ZXCVB6Y 5 7689 934567123GHJKUI4O 7 - -- -- I want the 16th character in each record to be replaced with the below as follows;so 2 will become K, 6 will become O and 4 will become... (3 Replies)
Discussion started by: er_ashu
3 Replies

7. Shell Programming and Scripting

replace > with new line character

<reward_data><date><datetime>071308000804</datetime></date> I want the above data to be displayed as <reward_data> <date> <datetime>071308000804</datetime> </date> How can i accomplish this. I tried the below tr "><" ">\n" < filename (4 Replies)
Discussion started by: borncrazy
4 Replies

8. Shell Programming and Scripting

Replace certain character with a new a new line.

Hello all... please help with the following. I am parsing the following type of file... W001; W003; W025;W044; W030; W022;W024;W099;W098; Would like to make it look like this... W001 W003 W025 W044 W030 W022 W024 W099 W098 (8 Replies)
Discussion started by: djsal
8 Replies

9. Shell Programming and Scripting

to replace a new line character

sample I/p: S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO Required O/p: S12J LLL H77K PPP J25O LOP I73S lOP K99O PLO how to replace a new line character with space using sed command only Cheers, Chan (2 Replies)
Discussion started by: chan
2 Replies

10. Shell Programming and Scripting

Replace a character in last line

Hello Sed Experts, I have got a file which contain entries as below pmNoOfSwDownHsCong, pmUlUpswitchAttemptHigh, pmUlUpswitchAttemptLow, pmUlUpswitchSuccessHigh, pmUlUpswitchSuccessLow, pmUpswitchFachHsAttempt, ... (6 Replies)
Discussion started by: Mohammed
6 Replies
Login or Register to Ask a Question