Replace specific words with nothing


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Replace specific words with nothing
# 8  
Old 12-30-2013
Quote:
Originally Posted by RavinderSingh13
Hello,

May be this will be helpful.

Code:
$ awk -F"_" 'NR==1 {print $1 OFS $2} NR==2 || NR==3 {print $1}' OFS=_ file_name
 
Output will be as follows:
 
intergenic_Nedd4
Gapvd1
Gapvd1

Thanks,
R. Singh

Ravinder :

Assume if file contains 100 lines,

intergenic_Nedd4_exon_0_F ---> will be line no 1

Gapvd1_intron_24_R---> will be line no 2 and 3

intergenic_Nedd4_exon_0_F---> will be in between somewhere within 100

What do you do ?
# 9  
Old 12-30-2013
for entries like this
Code:
filed1 field2 gene_intron_24_R ...filed10 field11 gene_intron_24_

it is just replacing only those that occur after field2. it is not replacing after field11. I should have been clear with the question
# 10  
Old 12-30-2013
Quote:
Originally Posted by raj_k
for entries like this
Code:
filed1 field2 gene_intron_24_R ...filed10 field11 gene_intron_24_

it is just replacing only those that occur after field2. it is not replacing after field11. I should have been clear with the question
It's not clear to me, show your real input and expected output.
# 11  
Old 12-30-2013
input
Code:
Id1   chr9   fox12_exon_0_F    chr9    56    72        72660772    H3K4ME2_E    726  72  promoter       intergenic_Nedd4_exon_0_F

output
Code:
Id1   chr9   fox12   chr9    56    72         72660772    H3K4ME2_E    726  72  promoter        intergenic_Nedd4

# 12  
Old 12-30-2013
Quote:
Posted by Akshay:

Ravinder :
Assume if file contains 100 lines,

intergenic_Nedd4_exon_0_F ---> will be line no 1

Gapvd1_intron_24_R ---> will be line no 2 and 3

intergenic_Nedd4_exon_0_F ---> will be in between somewhere within 100

What do you do ?
Hello Akshay,

I have just given code for the lines given by user. Will try to give a code which may apply to all.


Thanks,
R. Singh
# 13  
Old 12-30-2013
Quote:
Originally Posted by raj_k
input
Code:
Id1   chr9   fox12_exon_0_F    chr9    56    72        72660772    H3K4ME2_E    726  72  promoter       intergenic_Nedd4_exon_0_F

output
Code:
Id1   chr9   fox12   chr9    56    72         72660772    H3K4ME2_E    726  72  promoter        intergenic_Nedd4

We shall replace sub with gsub, here is code

Code:
$ awk 'gsub(/_(intron|exon)_([[:digit:]]+)_([[:upper:]])/, x) + 1' file
Id1   chr9   fox12    chr9    56    72        72660772    H3K4ME2_E    726  72  promoter       intergenic_Nedd4

This User Gave Thanks to Akshay Hegde For This Post:
# 14  
Old 12-30-2013
Hello Raj/Akashay,

Here are the some more solutions.

1st: When we have a file in which Input is having Record seprator as new line.

Code:
sed 's/\(.*\)\(_exon.*\)/\1/g;s/\(.*\)\(_intron.*\)/\1/g' check_lines12

2nd: When we have record seprator as space then code is as follows.

Code:
awk -F" " '{for(i=1;i<=NF;i++) print $i"\n"}' check_lines12_2 | sed 's/\(.*\)\(_exon.*\)/\1/g;s/\(.*\)\(_intron.*\)/\1/g' | tr '\n' ' '
 
OR
 
cat file_name | tr ' ' '\n' | sed 's/\(.*\)\(_exon.*\)/\1/g;s/\(.*\)\(_intron.*\)/\1/g' | tr '\n' ' '

Output will be as follows in all these commands.


Code:
Id1  chr9  fox12  chr9  56  72  72660772  H3K4ME2_E  726  72  promoter  intergenic_Nedd4



NOTE: Where file_name and check_lines12_2 are the input files.


Thanks,
R. Singh
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace particular words in file based on if finds another words in that line

Hi All, I need one help to replace particular words in file based on if finds another words in that file . i.e. my self is peter@king. i am staying at north sydney. we all are peter@king. How to replace peter to sham if it finds @king in any line of that file. Please help me... (8 Replies)
Discussion started by: Rajib Podder
8 Replies

2. Shell Programming and Scripting

Search and replace specific positions of specific lines

Hi, I have a file with hundreds of lines. I want to search for particular lines starting with 4000, search and replace the 137-139 position characters; which will be '000', with '036'. Can all of this be done without opening a temp file and then moving that temp file to the original file name. ... (7 Replies)
Discussion started by: dsid
7 Replies

3. Shell Programming and Scripting

How to replace some specific words from file?

I have the file like this. cat 123.txt <p> <table border='1' width='90%' align='center' summary='Script output'> <tr><td>text </td> </tr> </table> </p> I want to replace some tags and want the output like below. I tried with awk & sed commands. But no luck. Could someone help me on this? ... (4 Replies)
Discussion started by: thomasraj87
4 Replies

4. UNIX for Dummies Questions & Answers

Replace the words in the file to the words that user type?

Hello, I would like to change my setting in a file to the setting that user input. For example, by default it is ONBOOT=ON When user key in "YES", it would be ONBOOT=YES -------------- This code only adds in the entire user input, but didn't replace it. How do i go about... (5 Replies)
Discussion started by: malfolozy
5 Replies

5. Shell Programming and Scripting

Replace specific field on specific line sed or awk

I'm trying to update a text file via sed/awk, after a lot of searching I still can't find a code snippet that I can get to work. Brief overview: I have user input a line to a variable, I then find a specific value in this line 10th field in this case. After asking for new input and doing some... (14 Replies)
Discussion started by: crownedzero
14 Replies

6. Shell Programming and Scripting

Shell script to find out words, replace them and count words

hello, i 'd like your help about a bash script which: 1. finds inside the html file (it is attached with my post) the code number of the Latest Stable Kernel, 2.finds the link which leads to the download location of the Latest Stable Kernel version, (the right link should lead to the file... (3 Replies)
Discussion started by: alex83
3 Replies

7. Shell Programming and Scripting

Find and replace a string a specific value in specific location in AIX

Hi, I have following samp.txt file in unix. samp.txt 01Roy2D3M000000 02Rad2D3M222222 . . . . 10Mik0A2M343443 Desired Output 01Roy2A3M000000 02Rad2A3M222222 . . (5 Replies)
Discussion started by: techmoris
5 Replies

8. Shell Programming and Scripting

Using sed to replace specific character and specific position

I am trying to use sed to replace specific characters at a specific position in the file with a different value... can this be done? Example: File: A0199999123 A0199999124 A0199999125 Need to replace 99999 in positions 3-7 with 88888. Any help is appreciated. (5 Replies)
Discussion started by: programmer22
5 Replies

9. Shell Programming and Scripting

using sed to replace a specific string on a specific line number using variables

using sed to replace a specific string on a specific line number using variables this is where i am at grep -v WARNING output | grep -v spawn | grep -v Passphrase | grep -v Authentication | grep -v '/sbin/tfadmin netguard -C'| grep -v 'NETWORK>' >> output.clean grep -n Destination... (2 Replies)
Discussion started by: todd.cutting
2 Replies

10. Shell Programming and Scripting

How to replace a specific word in specific column?

Hi My orginal file is like (100s of lines) id host ip location remarks 1 host1 ip1 - xxx 2 host2 ip2 - xxx 3 host3 ip3 - xxx -- -- 9 host9 ip9 - xxx I have a ref file like host1 location1 host2 location2 host3 location3 -- --... (6 Replies)
Discussion started by: ./hari.sh
6 Replies
Login or Register to Ask a Question