Replacing characters in a file


 
Thread Tools Search this Thread
Top Forums UNIX for Advanced & Expert Users Replacing characters in a file
# 1  
Old 07-15-2014
Question Replacing characters in a file

Suppose I have a file which has 1000 columns (5 SHOWN FOR EXAMPLE)
two alphabets are separated by a space and then tab
Code:
A A"\t"C C"\t"G G"\t"0 0"\t"T T
A G"\t"C C"\t"G G"\t"A T"\t"0 0
G A"\t"0 0"\t"G C"\t"A A"\t"T C


whenever there is a 0 0 in any column, the output should be printed as

Code:
A A"\t"0 0"\t"G G"\t"0 0"\t"0 0
A G"\t"0 0"\t"G G"\t"0 0"\t"0 0
G A"\t"0 0"\t"G C"\t"0 0"\t"0 0


Can someone tell be how to solve this problem for 1000 columns.

Thank You in advance

Moderator's Comments:
Mod Comment Please select text first before pressing CODE tags button

Last edited by rossi; 07-15-2014 at 03:50 PM.. Reason: Removed excess CODE tags
# 2  
Old 07-15-2014
Hi, try:
Code:
awk '$0~s{$2=s; for(i=4; i<=NF; i++) $i=s}1' s="0 0" FS='\t' OFS='\t' file


Last edited by Scrutinizer; 07-15-2014 at 04:05 PM..
This User Gave Thanks to Scrutinizer For This Post:
# 3  
Old 07-15-2014
Not clear. What are the columns? Are those columns space separated or TAB separated? Your sample shows five, not four columns. Will Col three always be retained? What about Col 1000?

Please specify more carefully in English.

Last edited by RudiC; 07-15-2014 at 02:56 PM.. Reason: Nonsense!
# 4  
Old 07-15-2014
Hi

Sorry, the example has 5 columns.
Lets's take the 1st row
There is a space between A A, tab 0 0, tab G G, tab 0 0, tab T T.
Column three won't always be retained.

---------- Post updated at 06:58 PM ---------- Previous update was at 06:49 PM ----------

Hi,

Thank you so much, it tested it on the above example and it works.
Can you explain the code a bit. Like why is i=4, $2=s.

Thanks once again
# 5  
Old 07-15-2014
Hi, sure:
Code:
awk '
  $0~s {                         # if the line contains the search string in variable s
    $2=s                         # set the second field to that string 
    for(i=4; i<=NF; i++) $i=s    # and set field 4 and higher to that string (thus leaving only field 1 and 3 unchanged)
  }
  1                              # print the line
' s="0 0" FS='\t' OFS='\t' file  # set s (the search string variable) to "0 0" and 
                                 # and set both input and output field separators to TAB

This User Gave Thanks to Scrutinizer For This Post:
# 6  
Old 07-15-2014
Hi

I understand now from the comments what i=4 does in this example.

But what if I don't know the exact positions of the columns which has 0 0 in the 1000 column file. Let's suppose an example, column 4 doesn't have 0 0, column 5 has 0 0, no 0 0 in column 6 and 7, 0 0 in column 8 and so on.

What needs to be done in this case?
Hope that I was able to explain the situation a bit.

Thanks
# 7  
Old 07-15-2014
Let me guess: Is the requirement that if a column has "0 0" in any row, the entire column should become "0 0"? That would be an entirely different approach. You'd need to read the entire file to decide if any column goes zero and then print out all rows/columns.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Any tip to replacing the special characters in a file

Hi, Please find attached a file that has special characters on it. It is a copy and paste from a Micro$oft file. I don't want to use strings as it remove all the 'indentations' / 'formatting' so I am replacing them with space instead. I am using the sed command below sed "s/$(printf... (1 Reply)
Discussion started by: newbie_01
1 Replies

2. HP-UX

Replacing Hex Characters In A File Using awk?

Hi guys, First off, i'm a complete noob to UNIX and LINUX so apologies if I don't understand the basics! I have a file which contains a hex value of '0D' at the end of each line when I look at it in a hex viewer. I need to change it so it contains a hex value of '0D0A0A' I thought... (10 Replies)
Discussion started by: AndyBSG
10 Replies

3. Shell Programming and Scripting

sed replacing specific characters and control characters by escaping

sed -e "s// /g" old.txt > new.txt While I do know some control characters need to be escaped, can normal characters also be escaped and still work the same way? Basically I do not know all control characters that have a special meaning, for example, ?, ., % have a meaning and have to be escaped... (11 Replies)
Discussion started by: ijustneeda
11 Replies

4. Shell Programming and Scripting

Translating/Replacing characters in a file

Hi, i have a given file named hugo.dat. In this file there are several lines that contain characters like } and ~ Now, i need a script that replaces the character } to ü and character ~ to ß Can anyone help for a working ksh script? Kind Regards FranzB (3 Replies)
Discussion started by: FranzB
3 Replies

5. UNIX for Dummies Questions & Answers

Replacing digit with characters in a file

Hi, I have a file with 40 columns out of which 15 are amount fields. There are approximately 6 mn records in this file. The file has data in following format: 123A,Ank,00.468,US,IL,780,53489 253A,Tng,-00.456,US,CA,452,46781 363A,nkk,-00.023,US,NJ,539,09625 I need to take all amount fields... (1 Reply)
Discussion started by: wahi80
1 Replies

6. UNIX for Dummies Questions & Answers

Replacing characters in csv file

Hello all, This is my first post here, so please excuse me if this question is too obvious or has been asked before. I am new to Unix and although I tried to search your forum for the answer to my question, I could not find an answer that would help me. I have a 500MB csv file with numeric values... (1 Reply)
Discussion started by: finwhiz
1 Replies

7. UNIX for Dummies Questions & Answers

replacing the characters in a file

hi i want to replace the characters between positions 2 to 30 in each line in a file how to do it suggestions welcome (2 Replies)
Discussion started by: trichyselva
2 Replies

8. Shell Programming and Scripting

Help Replacing Characters in Flat File

I was wondering if somebody could help me with something on UNIX. I have a file that looks like this - "nelson,bill","bill","123 Main St","Mpls","MN",55444,8877,william I want to replace all comma with pipes (|), except if the comma is within double quotes. (The first field is an example of... (8 Replies)
Discussion started by: nelson553011
8 Replies

9. Shell Programming and Scripting

Replacing characters in file with line break

Hi, Apologies if this has been asked before, but I searched and was not able to find an answer. It's probably a simple question to answer for those of you with some experience, though... I have a relatively long string where tokens are separated by the colon (':') character. Let's say the... (10 Replies)
Discussion started by: johnemb
10 Replies

10. UNIX for Dummies Questions & Answers

replacing few characters in a file

Hi All, I have huge xml file. The file contains some comment tags . I have requirement to replace comment tag with another comment tag. Say for example : file X has -- Part of the file <?xml version="1.0" encoding="ISO-2"?><translationResults jobDate="20070123 23:20:51"... (1 Reply)
Discussion started by: purnakarthik
1 Replies
Login or Register to Ask a Question