Delete a semicolon and numbers after a semicolon


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Delete a semicolon and numbers after a semicolon
# 1  
Old 05-15-2013
Delete a semicolon and numbers after a semicolon

I have this:

((9:0.010,(11:0.089,13:0.004))

and I would like this:

((A9,(A11,A13))

How do I delete the semi colon and the number (i.e. 0.010) after the semi colon? Also, how can I add the letter before the number that is NOT removed?

Thank you in advance!

---------- Post updated at 06:28 PM ---------- Previous update was at 06:23 PM ----------

Whoops, I meant to type COLON
# 2  
Old 05-15-2013
Quote:
Originally Posted by MDeBiasse
I have this:

((9:0.010,(11:0.089,13:0.004))

and I would like this:

((A9,(A11,A13))

How do I delete the semi colon and the number (i.e. 0.010) after the semi colon? Also, how can I add the letter before the number that is NOT removed?

Thank you in advance!

---------- Post updated at 06:28 PM ---------- Previous update was at 06:23 PM ----------

Whoops, I meant to type COLON
Are there any characters in you input other than colon, comma, period, open and close parentheses, and digits?

Is this a line in a file or is it the value stored in a shell variable? If it is in a file, will there be more than one line?

What shell are you using?

What OS are you using?

Is the letter you want to add always a uppercase A?

Will the parentheses always be arranged the same way?

Will there always be 3 numbers you want to keep and 1 colon after each number you want to keep?
# 3  
Old 05-15-2013
Are there any characters in you input other than colon, comma, period, open and close parentheses, and digits?

Nope, just digits, colons, commas, and parentheses

Is this a line in a file or is it the value stored in a shell variable? If it is in a file, will there be more than one line?

The lines are in a text file. Files have between 1 and 1000 lines

What OS are you using?

Mac OSX

Is the letter you want to add always a uppercase A?

No. I could add any letter, upper or lower case.

Will the parentheses always be arranged the same way?

Yes

Will there always be 3 numbers you want to keep and 1 colon after each number you want to keep?


I want to delete the four digits and decimal point after the colon and retain the digits before the colon. The number after the colon is always x.xxx and the number before the colon is either one digit or two but never three

I thought I could do something like this: awk \:\d\.\d+ file > file_b for the first task at least, but have not been successful. Thank you for your help!
# 4  
Old 05-15-2013
Here is a sed approach:
Code:
$ sed 's#:[0-9.]*##g;s#[0-9]\+#A&#g' file
((A9,(A11,A13))

This User Gave Thanks to Yoda For This Post:
# 5  
Old 05-15-2013
The following seems to do what you want:
Code:
awk -v letter='A' '
{       # Delete all occurrences of ":x.xxx" where each x is a digit.
        gsub(/:[0-9.]+/, "")
        # Insert "letter" before remaining digit strings.
        gsub(/[0-9]+/, letter "&")
        # Print the modified line.
        print
}' file

assuming your input is in a file named "file". If anyone wants to try this scrip using a Solaris/SunOS system, they should use /usr/xpg4/bin/awk, /usr/xpg6/bin/awk, or nawk instead of awk.

With the input you specified in the 1st message in this thread, the output produced is:
Code:
((A9,(A11,A13))

If you'd like to use some string other than "A" before the numbers added in the output, change the value assigned to letter in the first line of code.

---------- Post updated at 18:19 ---------- Previous update was at 18:00 ----------

With a version of sed that conforms to the standards (including the sed available on OS X), Yoda's sed command can be simplified to:
Code:
sed 's/\([0-9]*\):[0-9.]*/A\1/g' file

and still produce the same output.
This User Gave Thanks to Don Cragun For This Post:
 
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Replace semicolon within double quotes in a file with semicolon delimiter

Hello Team, Could you please help me with the below question? I have a file with the following properties 1) File Delimiter is ; 2) Text columns are within double quotes 3) Numeric columns will not have double quotes 4) File has total 6 columns Please see a sample record from file ... (3 Replies)
Discussion started by: sam99
3 Replies

2. Shell Programming and Scripting

How to use substr to extract character between two semicolon?

Dear folks Hello I have a data set which one of the column of this data set are string and I want to extract numbers which is between two ":". However, I know the substr command which will do this operation but my problem is the numbers between two ":" have different digits. this will make my... (11 Replies)
Discussion started by: sajmar
11 Replies

3. Shell Programming and Scripting

How to have semicolon at the end of every line?

Hi, I wanted to create an automated script that will output a create table statement in unix. Below is the input and the desired output: INPUT: desc ZZ_APL_TIDDATELIST ( TID NUMBER AEX_DATE TIMESTAMP(6) ) desc ZZ_APL_TIDLIST ( TID NUMBER ) desc... (5 Replies)
Discussion started by: reignangel2003
5 Replies

4. Shell Programming and Scripting

Adding semicolon at the end of each line

Hi, I have a script which I need to change. I want to add a semicolon at the end of each line where the line starts with "grant" for e.g. create table(.... ); grant select on TABL1 to USER1 grant select on TABL1 to USER2should become create table(.... ); grant select on TABL1 to... (3 Replies)
Discussion started by: pparthiv
3 Replies

5. Shell Programming and Scripting

Replace dot with semicolon in PERL

Hi, I have a file in PERL in the following pattern filename| 06-Dec-11 03.04.14.000000 PM filename1| 06-Dec-11 05.05.14.000000 PM I need to replace .(dot) with :(semicolon) in the timestamp value of the file How can this be done. Any help will be appreciated Thanks in advance (5 Replies)
Discussion started by: irudayaraj
5 Replies

6. Shell Programming and Scripting

Need to put semicolon

Hi guys, I want to write script so that i can put semicolon after every numeric e.g input would be like that 50060E80058F49A4 Output should be 50:06:0E:80:05:8F:49:A4 Please help Thanks & Regards Nirjhar (11 Replies)
Discussion started by: nirjhar17
11 Replies

7. Shell Programming and Scripting

Delete lines after semicolon

char str; char str ; char *ptr; char * ptr; int CASE; int CASE; double temp; double temp; Output should be: char str; char *ptr; int CASE; double temp; How can i do this with awk,sed,perl? (5 Replies)
Discussion started by: cola
5 Replies

8. Shell Programming and Scripting

Replace a string after n semicolon every line

I have a file that is formatted in this way. a1;b2;c33;d4;e5;e;f;f;f;s d;ds;d;a;v;b;g;gr;r;rt;fdf s1;s2;s2;s3;s4; b1;f2;g3;h4;a3c4e;xcsd;fds; sd2;fs4;fs2;sdf3; I want to replace the value just before the 4th semicolon to empty string, regardless the value, such that it looks... (3 Replies)
Discussion started by: alienated
3 Replies

9. UNIX for Dummies Questions & Answers

wget with semicolon in page name

So, I'd like to wget a webpage, as its not going to stick around forever - but the problem is the webpage has a semicolon in it. wget http://example.com/stuff/asdf;asdf obviously doesn't get the right webpage. Any good way around this? (2 Replies)
Discussion started by: Julolidine
2 Replies

10. Shell Programming and Scripting

Extract semicolon separated delimiter

The log reads as follows. fname1;lname1;eid1;addr;pincode1; fname2;lname2;eid2;addr2;pincode2; fname3;lname3;eid3;addr3;pincode3; fname4;lname4;eid;addr4;pincode4; how do i extract only fname and save it in an array similarly for lname and so on i tried reading a file and cutting each... (5 Replies)
Discussion started by: vkca
5 Replies
Login or Register to Ask a Question