Replacing text on specific columns 2nd


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replacing text on specific columns 2nd
# 1  
Old 06-03-2009
Computer Replacing text on specific columns 2nd

Hi ,

I'm so surprised to see my thread closed !

i come here many times and work with some great guys like Perderabo , each time i search for many solutions to write some scripts for my job , at this time i don't see how i can solve this one , so please don't take me as a pupil !

i can test the 7th character then cut and replace but some lines are longer than 7 character and to avoid many tests , i believe that 'AWK' can be my solution for testing the last character !

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 without doing about ten tests !

thanks in advance

Christian
# 2  
Old 06-03-2009
Quote:
Originally Posted by Nicol
i come here many times and work with some great guys like Perderabo , each time i search for many solutions to write some scripts for my job , at this time i don't see how i can solve this one , so please don't take me as a pupil !
with 120++ posts, your "newbie" license is already expired.
Also, in your previous posts you were already exposed to awk , sed and unix tools. Therefore, one would expect you to show something you have done, (its your job isn't it??), or else how do you prove you are worth the salary you are taking home?
# 3  
Old 06-03-2009
Quote:
Originally Posted by Nicol
Hi ,

I'm so surprised to see my thread closed !
Don't be surprised. If you "don't have time to try" to solve your issue, you really cannot expect us to have time to do it for you, can you?

Please stick with the technical aspect of your issue and show the effort first.

It seems that relying on the 7-th character replacement is not generic enough. How about dealing with the LAST character on a line?
# 4  
Old 06-03-2009
Well i'm here because i need to modify a script while i'm doing many other admin tasks other than unix.
I'm still a newbie , i'm not an unix admin .
I'm not asking for a complete code , maybe my previous post was a little bit awkward.

"How about dealing with the LAST character on a line?"
i think it's the best way to search , and i don't see how i can do this with awk or sed.
Maybe shifting all the character on the left until the last then replace ?

.........................

i tried this with success :

sed '1,$ s/A$/B/g' oldfile > newfile

can i do in the same line the change of A to B and B to A ?
without doing :

sed '1,$ s/A$/B/g' oldfile > newfile1
sed '1,$ s/B$/A/g' oldfile > newfile2
cat newfile1 newfile2 newfile3

regards
Christian
# 5  
Old 06-03-2009
Quote:
Originally Posted by Nicol
Well i'm here because i need to modify a script while i'm doing many other admin tasks other than unix.
I'm still a newbie , i'm not an unix admin .
One does not have to be a 'unix admin' to address these questions.
Once again, please stick with the technical side of your question.
If you "have time" to discuss the merits of your post(s), please do so in the non-technical forums of this site.
Quote:
Originally Posted by Nicol
I'm not asking for a complete code , maybe my previous post was a little bit awkward.

"How about dealing with the LAST character on a line?"
i think it's the best way to search , and i don't see how i can do this with awk or sed.
Maybe shifting all the character on the left until the last then replace ?

.........................

i tried this with success :

sed '1,$ s/A$/B/g' oldfile > newfile

can i do in the same line the change of A to B and B to A ?
without doing :

sed '1,$ s/A$/B/g' oldfile > newfile1
sed '1,$ s/B$/A/g' oldfile > newfile2
cat newfile1 newfile2 newfile3

regards
Christian
Sure:
Code:
sed 's/A$/B/;n;s/B$/A/' oldFile

# 6  
Old 06-03-2009
Quote:
Originally Posted by Nicol
Well i'm here because i need to modify a script while i'm doing many other admin tasks other than unix.
I'm still a newbie , i'm not an unix admin .
no, you are definitely not a newbie in unix since you joined this forum 6 years ago. you are exposed to unix since then. BUT i will give you the benefit, here's a Python script, if you have it in your system
Code:
#!/usr/bin/env python
for line in open("file"):
    line=line.strip()
    if line.endswith("A"):
        print line[:-1]+"B" #print from start till the last second character.
    elif line.endswith("B"):
        print line[:-1]+"A"

the algorithm can be adapted to any other language, even awk.
# 7  
Old 06-03-2009
Another approach with awk:

Code:
awk '
substr($0,length)=="A"{sub(/.$/,"B");print;next}
substr($0,length)=="B"{sub(/.$/,"A")}
1' file

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