Replace text based on column


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace text based on column
# 1  
Old 09-18-2010
Replace text based on column

Replace these if the column is -
p = q
q = p
r = s
s = r

input
Code:
-  PQRSSP
+  PQRS

output
Code:
-  QPSRRQ
+ PQRS


Some thing,like
Code:
awk '{if ($1 == "-") {gsub(/PQRS/,"QPSR",1)}; print $0}' 

# 2  
Old 09-18-2010
Quote:
Originally Posted by bumblebee_2010
Replace these if the column is -
p = q
q = p
r = s
s = r

input
Code:
-  PQRSSP
+  PQRS

output
Code:
-  QPSRRQ
+ PQRS

...
Code:
$ 
$ cat f9
-  PQRSSP
+  PQRS
$ 
$ 
$ perl -plne 'tr/PQRS/QPSR/ if /^-/' f9
-  QPSRRQ
+  PQRS
$ 
$

tyler_durden
# 3  
Old 09-18-2010
With sed:
Code:
sed '/^-/y/PQRS/QPSR/' file

# 4  
Old 09-18-2010
Or in Bash -

Code:
$ 
$ 
$ cat f9
-  PQRSSP
+  PQRS
$ 
$ 
$ while read X; do
>   if [ "${X:0:1}" == "-" ]; then
>     echo $X | tr 'PQRS' 'QPSR'
>   else
>     echo $X
>   fi
> done < f9
- QPSRRQ
+ PQRS
$ 
$ 

tyler_durden
# 5  
Old 09-18-2010
And one with awk:
Code:
awk '/^-/{print $0 |"tr \"PQRS\" \"QPSR\" ";next}1' file

# 6  
Old 09-18-2010
Code:
ruby -ne 'print $_.tr("P-S","QPSR")' file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need a Linux command for find/replace column based on specific criteria.

I'm new to shell programming, I have a huge text file in the following format, where columns are separated by single space: ACA MEX 4O_ $98.00 $127.40 $166.60 0:00 0:00 0 ; ACA YUL TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ACA YYZ TS_ $300.00 $390.00 $510.00 0:00 0:00 0 ; ADZ YUL TS_ $300.00... (3 Replies)
Discussion started by: transat
3 Replies

2. Shell Programming and Scripting

sorting based on a specified column in a text file

I have a tab delimited file with 5 columns 79 A B 20.2340 6.1488 8.5086 1.3838 87 A B 0.1310 0.0382 0.0054 0.1413 88 A B 46.1651 99.0000 21.8107 0.2203 89 A B 0.1400 0.1132 0.0151 0.1334 114 A B 0.1088 0.0522 0.0057 0.1083 115 A B... (2 Replies)
Discussion started by: Lucky Ali
2 Replies

3. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on the first column

I have a tab delimited text file where the first column can take on three different values : 100, 150, 250. I want to extract all the rows where the first column is 100 and put them into a separate text file and so on. This is what my text file looks like now: 100 rs3794811 0.01 0.3434 100... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. UNIX for Dummies Questions & Answers

Extracting rows from a text file based on the first column

I have a tab delimited text file where the first column can take on three different values : 100, 150, 250. I want to extract all the rows where the first column is 100 and put them into a separate text file and so on. This is what my text file looks like now: 100 rs3794811 0.01 0.3434... (1 Reply)
Discussion started by: evelibertine
1 Replies

5. Shell Programming and Scripting

Replace a text with a particular column and particular line

I have a file test.txt cat test.txt shows 10 200 5 600 1000 4 88 9 3 6 66 101 Suppose, I want to replace a number with a particular line and column. For instance, the number 88 in line 3, column 2. I want to replace it by 99. How can I do that? Note that I am... (11 Replies)
Discussion started by: hbar
11 Replies

6. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (1 Reply)
Discussion started by: perl_beginner
1 Replies

7. Shell Programming and Scripting

Help with replace column one content based on reference file

Input file 1 testing 10 20 1 A testing 20 40 1 3 testing 23 232 2 1 testing 10 243 2 . . Reference file 1 final 3 used . . Output file (2 Replies)
Discussion started by: perl_beginner
2 Replies

8. Shell Programming and Scripting

Replace text in column

Hi: I have a file as below: #LINE 3841.0 14663859 358272.0 676600.0 20.00 1510.00 91.00 1534.00 215.00 1624.00 374.00 1688.00 14663899 ... (2 Replies)
Discussion started by: total_ysf
2 Replies

9. Shell Programming and Scripting

Replace Text Based On Pattern

Hi I'm trying to replace text in a file based upon a pattern. The pattern I'm looking for is: <styleURL>#style0002</styleURL> <name>#######6105#######</name>The # are seven alphanumeric characters before and after 6105. I need it to replace that with this recursively: ... (4 Replies)
Discussion started by: Grizzly
4 Replies

10. Shell Programming and Scripting

Adding a column to a text based on file name

Dear all, Does anyone know how I could to add a column of numbers (1s, or 2s, or..., or 6s) to two-column text files (tab-delimited), where the specific number to be added varies as a function of the file naming? Currently, each of my text files has two columns, so the column with the... (12 Replies)
Discussion started by: rlapate
12 Replies
Login or Register to Ask a Question