Insert underscore at certain places


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Insert underscore at certain places
# 1  
Old 08-11-2012
Insert underscore at certain places

Hi all,

I have to insert underscore at certain places(places before and after PAxxx/PAxxxx entries in a big file like this

Code:
ESR1 PA156 leflunomide PA450192 
CHST3 PA26503  docetaxel tungstate  Pa4586; thalidomide Pa34958;

PAxxx/PAxxxx entries are metioned between 2 names in each row

I want output shuld be

Code:
ESR1_PA156_leflunomide_PA450192_ 
CHST3_PA26503_docetaxel tungstate_Pa4586_;thalidomide_Pa34958_;

Please let me know scripting regarding this

Last edited by manigrover; 08-11-2012 at 08:07 AM..
# 2  
Old 08-11-2012
Try:
Code:
perl -pe 's/ +(P(A|a))/_\1/g;s/(P(A|a)\d+) +/\1_/g;s/(P(A|a)\d+); ?/\1_;_/g' file

# 3  
Old 08-11-2012
Quote:
Originally Posted by manigrover
Code:
ESR1_PA156_leflunomide_PA450192 
CHST3_PA26503_docetaxel tungstate_Pa4586_;_thalidomide_Pa34958_;

And how do you get THAT underscore?

Also, you have asked to "insert" an underscore. But, from your output, it seems that you want to replace the white-space (if any) before and after those entries by underscores. Also, the PA entries terminating a line need no underscore at the end?

Please make your requirement very clear and don't leave people guessing. That might be one of the reasons why you need to post your problems in bits and pieces.

Last edited by elixir_sinari; 08-11-2012 at 08:00 AM..
# 4  
Old 08-11-2012
Hi all,

I dont want to replace white space with underscore.

I want the place before and after PAxxx/PA xxxx entires with underscore so that I can replace those with a column in excel sheet other wise its not happening because there is some time white space between 2 words between PA entries and I do not want to separate those in a column. Therefore,I do not want underscore there and I wan underscore only before and after PAxxx/PAxxxx entries.

I tried the above mentioned programm but it shows following error:

Code:
-bash-3.2$ perl -pe 's/ +(P(A|a))/_\1/g;s/(P(A|a)\d+) +^1_/g;s/(P(A|a)\d+); ?^1_;_/g' T2Dnew2 >T2Dnew3
Backslash found where operator expected at -e line 1, near ")\"
        (Missing operator before \?)
syntax error at -e line 1, near "s/(P(A|a)\d+) +^1_/g;s/("
Search pattern not terminated or ternary operator parsed as search pattern at -e


But the expected output is


Code:
ESR1_PA156_leflunomide_PA450192_ 
CHST3_PA26503_docetaxel tungstate_Pa4586_;thalidomide_Pa34958_;


Last edited by manigrover; 08-11-2012 at 08:07 AM..
# 5  
Old 08-11-2012
This is not the code I posted:
Code:
perl -pe 's/ +(P(A|a))/_\1/g;s/(P(A|a)\d+) +^1_/g;s/(P(A|a)\d+); ?^1_;_/g'

This is:
Code:
perl -pe 's/ +(P(A|a))/_\1/g;s/(P(A|a)\d+) +/\1_/g;s/(P(A|a)\d+); ?/\1_;_/g'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sum the fields with 6 decimal places - getting only 2 decimal places as output

I used the below script to Sum up a field in a file based on some unique values. But the problem is when it is summing up the units, it is truncating to 2 decimals and not 6 decimals as in the input file (Input file has the units with up to 6 Decimals – Sample data below, when the units in the 2... (4 Replies)
Discussion started by: brlsubbu
4 Replies

2. Shell Programming and Scripting

To count underscore in filename

Hi All I have a filename like below adsf_ghjt_kop_pol.csv Can you please get me a command for counting the number of underscore in the filename. In the above filename I should get output as 3, since the file name is having three underscore (5 Replies)
Discussion started by: ginrkf
5 Replies

3. HP-UX

sed help with underscore problems

Hello, I have spent a couple of hours trying to answer this myself, so forgive me if the answer is simple but I have tried. I have a text file generated from svn log output which contains a list of files. Two regexps im using are * and * They both work but some lines has a mixture... (7 Replies)
Discussion started by: YogaBija
7 Replies

4. Shell Programming and Scripting

underscore to dots

Hi, I have been trying to change underscores to dots. For example: 1122_91 1022_233 . 2237_23 9382_2339 2998_234 345_257 . . Desired output: 1122.91 1022.233 . 2237.23 9382.2339 2998.234 345.257 . . Any idea? Thanks (4 Replies)
Discussion started by: iconig
4 Replies

5. Shell Programming and Scripting

Insert underscore in place of column

Hi I tried to put underscore in place of column in a big file with lots of oclumns using the programm sed 's//_/g' Its showing error bash-3.2$ sed 's//_/g'saradrugbankgenedrugnewlist.txt >saradrugbankgenedrugnewlist3.txt sed: -e expression #1, char 11: unknown option to `s' if... (24 Replies)
Discussion started by: manigrover
24 Replies

6. Shell Programming and Scripting

Changing the character after the Underscore(_)

Hi Everyone, I am looking for a command that would do the following: 1) Change all the letters/words in a file to Lower case Letters/words. 2) Remove the Underscore (_) and Change the Character after the underscore (_) to an Uppercase letter. Example: File contains the below words: ... (5 Replies)
Discussion started by: filter
5 Replies

7. UNIX for Advanced & Expert Users

google search changes with an underscore

How does adding an underscore change a google search? I was searching for John Elway and got different results with and without an unscore. (3 Replies)
Discussion started by: cokedude
3 Replies

8. Shell Programming and Scripting

allow only alphnumeric entry and an underscore

I am taking an user input which should only be an alphanumeric character or an underscore. How to i do it? (2 Replies)
Discussion started by: vickylife
2 Replies

9. Shell Programming and Scripting

usage of underscore in awk

Hi what is the purpose of using underscore in awk. I suppose it is for defining macro's and reducing repeatation but can some one show me an example? (6 Replies)
Discussion started by: ahmedwaseem2000
6 Replies

10. UNIX for Dummies Questions & Answers

problem with underscore in variable name

can you help please. variable 1 = TODAY=`date +"%Y%m%d"` i.e. echo $TODAY 20080407 DB=GERMANY echo $DB GERMANY echo $DB.$TODAY GERMANY.20080407 echo $DB.$TODAY_1.dmp GERMANY..dmp (3 Replies)
Discussion started by: pinkie
3 Replies
Login or Register to Ask a Question