How to modify lines without certain patterns?


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers How to modify lines without certain patterns?
# 1  
Old 12-13-2012
How to modify lines without certain patterns?

I have file like this:
Code:
rs111 A T 0.9 0.8 ...
rs112 AT T 0.8 0.2 ...
rs113 G CT 0.9 0.1...

I wish to replace all the NONE "A" or "T" or "G" or "C" values in column 3 and column 4 to " ", how can I do this?

thanks!

Last edited by Scrutinizer; 12-14-2012 at 04:13 AM.. Reason: code tags
# 2  
Old 12-13-2012
Checking column 3 & replacing with " "
Code:
awk '$3 !~ /(^A$|^T$|^G$|^C$)/ { $3=" "; print $0 } $3 ~ /(^A$|^T$|^G$|^C$)/ { print $0; } ' infile

This User Gave Thanks to Yoda For This Post:
# 3  
Old 12-14-2012
Quote:
Originally Posted by bipinajith
Checking column 3 & replacing with " "
Code:
awk '$3 !~ /(^A$|^T$|^G$|^C$)/ { $3=" "; print $0 } $3 ~ /(^A$|^T$|^G$|^C$)/ { print $0; } ' infile

thanks for this! It works! One issue my file is very huge, it's about 11G. I have two columns I need to modify, while keep the rest the same. Ran this code twice to write in new files really takes time. Is there a way to speed it up? Or just modify the original file?

Another question is I am really new for unix, may I know what does print $0 mean?

thanks!
# 4  
Old 12-14-2012
Quote:
Originally Posted by luoruicd
I have file like this:
rs111 A T 0.9 0.8 ...
rs112 AT T 0.8 0.2 ...
rs113 G CT 0.9 0.1...

I wish to replace all the NONE "A" or "T" or "G" or "C" values in column 3 and column 4 to " ", how can I do this?

thanks!
i dont understand the above highlighted statement. can you give more input and the expected output.

$0 - indicates the current line (processing line) in awk
# 5  
Old 12-14-2012
For example:
my original file looks like this:
Code:
rs111 A T 0.9 0.8 ...
rs112 AT T 0.8 0.2 ...
rs113 G CT 0.9 0.1...

I want to modify column 2 and column3. In column 2, line2 it is not A or T or G or C, then I want to change it to " ", the same for column3, line3

The output file should be:
Code:
rs111 A T 0.9 0.8 ...
rs112   T 0.8 0.2 ...
rs113 G   0.9 0.1...


Last edited by Scrutinizer; 12-14-2012 at 04:13 AM.. Reason: code tags
# 6  
Old 12-14-2012
try this..

Code:
 
awk '$2!~/(^A$|^T$|^G$|^C$)/{$2=" "}$3!~/(^A$|^T$|^G$|^C$)/{$3=" "}{print}' inputfile

---------- Post updated at 11:29 AM ---------- Previous update was at 11:27 AM ----------

Code:
 
perl -lane 'if($F[1]!~/(^A$|^T$|^G$|^C$)/){$F[1]=" ";}if($F[2]!~/(^A$|^T$|^G$|^C$)/){$F[2]=" ";}print join " ",@F;' input.txt

This User Gave Thanks to itkamaraj For This Post:
# 7  
Old 12-14-2012
thanks! Unix code works, but not perl. I never used perl, so will just go with the unix one.
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

How to print lines from a files with specific start and end patterns and pick only the last lines?

Hi, I need to print lines which are matching with start pattern "SELECT" and END PATTERN ";" and only select the last "select" statement including the ";" . I have attached sample input file and the desired input should be as: INPUT FORMAT: SELECT ABCD, DEFGH, DFGHJ, JKLMN, AXCVB,... (5 Replies)
Discussion started by: nani2019
5 Replies

2. UNIX for Beginners Questions & Answers

Delete multiple lines between blank lines containing two patterns

Hi all, I'm looking for a way (sed or awk) to delete multiple lines between blank lines containing two patterns ex: user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 15 parameter_2 = 1 parameter_3 = 0 user: alpha parameter_1 = 16... (3 Replies)
Discussion started by: ce9888
3 Replies

3. Shell Programming and Scripting

How to print only lines in between patterns?

Hi, I want to print only lines (green-italic lines) in between first and last strings in column 9. there are different number of lines between each strings. 10 AUGUSTUS exon 4558 4669 . - . 10.g1 10 AUGUSTUS exon 8771 8889 . ... (6 Replies)
Discussion started by: jamo
6 Replies

4. Shell Programming and Scripting

Perl - need script for modify lines

Hello, does somebody can make script for me, which replace: ąćę ąćę ąćę (input file) to ace;|;ąćę ace;|;ąćę ace;|;ąćę (output file) (3 Replies)
Discussion started by: Xadrian
3 Replies

5. Programming

Need to modify contents of file with complex patterns.

hi, my fstab file content is like this along with some other lines: /dev/vg0/var1 /var1 ext3 defaults 0 2 /dev/vg0/flx1 /flx1 ext3 defaults 0 2 /dev/vg0/var /var ext3 defaults 0 1 /dev/vg0/flx /flx ext3 defaults 0 2 I want to remove lines with /dev/vg0/var and... (5 Replies)
Discussion started by: success
5 Replies

6. Shell Programming and Scripting

How can I get the lines between two patterns?

hi, I have the following file hello world this is to say bye to everyone so bye I want to get the lines from hello to the first bye inclusive into another file? how can I do this (11 Replies)
Discussion started by: JamesByars
11 Replies

7. Shell Programming and Scripting

about the modify lines

Hello, I am trying to convert lines from text file, I got lines in text files like that ALPHA.cab tomtom.cab vgame.cab converter.cabWhat i want to do is I want to conver these lines like that, cabwiz.exe ALPHA.cab extract "C:\1extract\ALPHA" cabwiz.exe tomtom.cab extract ... (5 Replies)
Discussion started by: davidkhan
5 Replies

8. Shell Programming and Scripting

Searching patterns in 1 file and deleting all lines with those patterns in 2nd file

Hi Gurus, I have a file say for ex. file1 which has 3500 lines in it which are different account numbers and another file (file2) which has 230000 lines in it. I want to read all the lines in file1 and delete all those lines from file2 which has that same pattern as in file1. I am not quite... (4 Replies)
Discussion started by: toms
4 Replies

9. UNIX for Dummies Questions & Answers

How to use sed modify specific lines

Could anybody tell me how I can use sed to modify lines following specific lines? the file is as following: "TEST/SI1573.lab" 3670 8920 h# 8920 9530 hh 9530 10694 ih . "TEST/DR1/FAKS0/SI2203.lab" 9730 9580 h# 9580 9840 dh 9840 10652 ix 10652 11997 r ........ I want to modify the... (5 Replies)
Discussion started by: Jenny.palmy
5 Replies

10. Shell Programming and Scripting

How to get lines in between Patterns?

Hi, I need to create a script that does the following: 1. Read the file for the occurrences of "EXECUTE" and "END" strings. There will be several occurrences of EXECUTE and END strings on the file. 2. The resulting lines in #1, needs to be searched for the word... (11 Replies)
Discussion started by: racbern
11 Replies
Login or Register to Ask a Question