Replacing text on specific columns 2nd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text on specific columns 2nd
# 8  
Old 06-03-2009
Code:
nawk '/[AB]$/ { sub(/.$/, /A$/?"B":"A")}1' myFile

# 9  
Old 06-03-2009
another one
Code:
# awk '/A$/{c="B"}/B$/{c="A"}c{print substr($0,1,length-1)c;c="";next}1' file


Last edited by ghostdog74; 06-03-2009 at 11:36 AM..
# 10  
Old 06-03-2009
Thanks for your help ,

the awk is ok

but ko for sed:

here is the oldfile :

mfksfmfsA
fsfdsfdsC
sdfdsfdsfffffffffffD
dsfsdfmkmslkdfB
qdqdqsdqdqsdqA
qsdqsdqsdqsdqsqdqsdqdA
zietireB

# sed 's/A$/B/;n;s/B$/A/' oldfile
mfksfmfsB
fsfdsfdsC
sdfdsfdsfffffffffffD
dsfsdfmkmslkdfA
qdqdqsdqdqsdqB
qsdqsdqsdqsdqsqdqsdqdA
zietireB

the last 2 lines are not modified!

regards
ps my job is admin Lotus Notes on Unix platform , so from time to time on this forum !
# 11  
Old 06-03-2009
And one way to do it with perl:

Code:
$
$ cat input.txt
APP101A
APP103B
MSG307A
MSG308X
XADM002A
$
$ perl -ne '@x{A,B}=(B,A); $y=substr($_,-2,1); substr($_,-2,1)=defined $x{$y} ? $x{$y} : $y; print' input.txt
APP101B
APP103A
MSG307B
MSG308X
XADM002B
$
$

And another way to do it in perl:

Code:
$
$ perl -ne 'BEGIN {@x{A,B}=(B,A)}{/[AB]$/ and substr($_,-2,1)=$x{substr($_,-2,1)}; print}' input.txt
APP101B
APP103A
MSG307B
MSG308X
XADM002B
$
$

tyler_durden

Last edited by durden_tyler; 06-03-2009 at 11:40 AM..
# 12  
Old 06-03-2009
Hi,

nawk '/[AB]$/ { sub(/.$/, /A$/?"B":"A")}1' oldfile

==> OK

but this one is ko :

# cat oldfile

mfksfmfsA
fsfdsfdsC
sdfdsfdsfffffffffffD
dsfsdfmkmslkdfB
qdqdqsdqdqsdqA
qsdqsdqsdqsdqsqdqsdqdA
zietireB
ldsfjfklsjfslkjsklfsjI
qdshqjdhqskjdhT

# awk '/A$/{c="B"}/B$/{c="A"}{print substr($0,1,length-1)c}' oldfile

mfksfmfsB
fsfdsfdsB
sdfdsfdsfffffffffffB
dsfsdfmkmslkdfA
qdqdqsdqdqsdqB
qsdqsdqsdqsdqsqdqsdqdB
zietireA
ldsfjfklsjfslkjsklfsjA
qdshqjdhqskjdhA

I have no Python installed and i expect to stay in a standard way , so no Perl script please!
thanks for trying to help me , the winner will receive my eternal gratitude ! lol
# 13  
Old 06-03-2009
Note that i have changed the awk code i posted.
Code:
the winner will receive my eternal gratitude

if, it so happens that i "win" , i would not want your gratitude, BUT i would be happy if you put in effort in learning shell scripting (or learning other languages like Perl/Python to help in your work).
I still cannot believe its 6 years ago you started coming here, and learned nothing till now.
# 14  
Old 06-03-2009
Thanks for the "new" awk it works !

If you "take time" to read my previous posts , you will understand that my job is mainly admin Lotus Notes and i don't spend so much time on this forum and neither in shell script , not because i don't do effort but because i don't have so much time for this .
And from time to time i need to script , and i don't progress anymore or not so much (as my english too).

But i have appreciated all the time you take for me.

Sincerely yours
Christian
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replacing entire fields with specific text at end or beginning of field

Greetings. I've got a csv file with data along these lines: Spumoni's Pizza Place, Placemats n Things, Just Lamps Counterfeit Dollars by Vinnie, Just Shades, Dollar StoreI want to replace the entire comma-delimited field if it matches something ending in "Place" or beginning with "Dollar",... (2 Replies)
Discussion started by: palmfrond
2 Replies

2. Shell Programming and Scripting

Add specific text to columns in file by sed

What is the proper syntax to add specific text to a column in a file? Both the input and output below are tab-delineated. What if there are multiple text/fields, such as /CP&/2 /CM&/3 /AA&/4 Thank you :). sed 's/*/Index&/1' del.txt.hg19_multianno.txt > matrix.del.txt (4 Replies)
Discussion started by: cmccabe
4 Replies

3. UNIX for Dummies Questions & Answers

Replacing all cells that have a specific value in a text file

I have a space delimited text file where I want to replace all cells that are 0 with NA. However I cannot simply use 'sed/0/NA/g' because there are other 0's in the text file that are part of numbers. Example input: 896.933464285715 0 874.691732142857 866.404660714286 Output:... (1 Reply)
Discussion started by: evelibertine
1 Replies

4. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

Hi, I have a text file in the following format: Code: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 ... (2 Replies)
Discussion started by: evelibertine
2 Replies

5. UNIX for Dummies Questions & Answers

Replacing a specific column of a text file with another column

I have a text file in the following format: 13412 NA06985 0 0 2 46.6432798439 4 4 4 4 13412 NA06991 NA06993 NA06985 2 48.8478948517 4 4 2 4 13412 NA06993 0 0 1 45.8022601455 4 4 2 4 13401 NA06994 0 0 1 48.780669145 4 4 4 4 13401 NA07000 0 0 2 47.7312017846 2 4 4 4 13402 NA07019... (3 Replies)
Discussion started by: evelibertine
3 Replies

6. UNIX for Dummies Questions & Answers

How do you view specific columns from a space delimited text file?

I have a space delimited text file with 1,000,000+ columns? I would only like to view specific ones (let's say through 1:10), how can I do that? Thanks! (3 Replies)
Discussion started by: evelibertine
3 Replies

7. Shell Programming and Scripting

remove white space from specific columns in text file

Hello i have a text file like this: 1 AB AC AD EE 2 WE TR YT WW 3 AS UY RF YT the file is bigger , but that's an example of the data what i want to do is to merge all columns together except the first one, it will become like this : 1 ABACADEE 2 WETRYTWW 3 ASUYRFYT (8 Replies)
Discussion started by: shelladdict
8 Replies

8. Shell Programming and Scripting

Replacing text on specific columns

Hi , i have one file like : APP101A APP103B MSG307A MSG308B XADM002A and so on , and i expect to replace the last character A by B and B by A the result would be : APP101B APP103A MSG307B MSG308A XADM002B how can i do that ? (3 Replies)
Discussion started by: Nicol
3 Replies

9. HP-UX

replacing text in specific location

i have a file that looks like this: 000000112/01/2008 D99999 000000 12/01/2008 D99999 000000 12/01/2008 1D99999 i need to replace the blanks into 1 for column 7,18-19 how can this be achieved using awk? Thanks. (1 Reply)
Discussion started by: zeontman
1 Replies

10. UNIX for Dummies Questions & Answers

searching text files on specific columns for duplicates

Is it possible to search through a large file full of rows and columns of text and retrieve only the rows that contain duplicates fields, searchiing for duplicates on col4 & col6 Sample below Col1 col2 col3 col4 col5 col6 G405H SURG FERGUSON ... (2 Replies)
Discussion started by: Gerry405
2 Replies
Login or Register to Ask a Question