How to use substr to extract character between two semicolon?


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting How to use substr to extract character between two semicolon?
# 1  
Old 03-13-2015
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 extraction difficult. I will put a part of my data set for better understanding.
Code:
1/1:0,6:6:12:150,12,0
0/1:6,4:10:99:126,0,195
1/1:100,50:150:34,0,210

In this case, I want to extract the red number out of my data set.
I will be glad that anyone could help me.
Thanks
Sajmar

Last edited by Don Cragun; 03-13-2015 at 05:10 PM.. Reason: Add CODE tags.
# 2  
Old 03-13-2015
Code:
$ cat  yourfile
1/1:0,6:6:12:150,12,0
0/1:6,4:10:99:126,0,195
1/1:100,50:150:34,0,210


 $ cat  yourfile|awk 'BEGIN{FS=OFS=":"}{$3="";}1'
1/1:0,6::12:150,12,0
0/1:6,4::99:126,0,195
1/1:100,50::34,0,210

# 3  
Old 03-13-2015
Thank you kenshinhimura for your suggestion. Because this column in the data set is the tenth column of my data set. Could you please tell me how could I specify the number of column in this command?
# 4  
Old 03-13-2015
Code:
From:
awk 'BEGIN{FS=OFS=":"}{$3="";}1'



To:
awk 'BEGIN{FS=OFS=":"}{$10="";}1'

# 5  
Old 03-13-2015
Or to parameterize it:
Code:
FieldNumber=10
awk -v f="$FieldNumber" 'BEGIN{FS = OFS = ":"}{$f = ""}1' input_file

# 6  
Old 03-13-2015
I want to extract the red numbers from the string column I have. when I run your suggestion command it remove the red numbers which are between two colon.I just want to keep those numbers.

---------- Post updated at 04:33 PM ---------- Previous update was at 04:29 PM ----------

Dear Don Cragun
I have 300 files which I used this command below to extract column 1, 2, 4, 5, and the first three number of the 10 column of my data set.

Code:
for file in *.work; do awk '{print $1,$2,$4,$5,substr($10,1,1),substr($10,3,1)}' $file > "$(basename "$file" .work).info"; done

My problem is that the number which are in the middle of the sting column tenth have different digit numbers.
Moderator's Comments:
Mod Comment Please use CODE tags when posting sample input, output, AND code segments.
Don't assume that a moderator will always be available to clean up your posts for you.

Last edited by Don Cragun; 03-13-2015 at 06:28 PM.. Reason: Add CODE tags.
# 7  
Old 03-13-2015
Nest time give a desired output.

maybe this is what you want

Code:
awk -F: '{print $10}'

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

Substr with % - extract numbers only

# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.5 (Maipo) I have this script that will monitor filesystems and send me e-amil alerts. #! /bin/ksh DIST_LIST=monitor@...com WORKDIR=/home/monitor WARNLEVEL=90 MAIL_SUBJ="filesystems monitor on "$(hostname) ... (3 Replies)
Discussion started by: danielshell
3 Replies

2. Shell Programming and Scripting

Substr/Instr in shell script or extract part of text

Hi, I need to extract part of a text to two variables text is "PL/SQL procedure successfully completed. ERROR ----------------------------------------------------------------- Test Error Message PLUSVAR ---------- 1" I want "Test Error Message" in one variable and "1" in another variable.... (11 Replies)
Discussion started by: vedavrath
11 Replies

3. 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

4. Shell Programming and Scripting

Extract First character in Second column

Hi, I need to extract the first character of second column of my file. If the condition matches, then I need to print the 2nd and 3rd column as my output I tried below mentioned query but it was not working awk -F'|' '$2~/^5/' Sgn_group.txt File Name : Sgn_group.txt country... (2 Replies)
Discussion started by: suresh_target
2 Replies

5. UNIX for Dummies Questions & Answers

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... (4 Replies)
Discussion started by: MDeBiasse
4 Replies

6. Shell Programming and Scripting

awk extract certain digits from file with index substr

I would like to extract a digit from $0 starting 2,30 to 3,99 or 2.30 to 3.99 Can somebody fix this? awk --re-interval '{if($0 ~ /{1}{2}/) {print FILENAME, substr($0,index($0,/{1}{2}/) , 4)}}'input abcdefg sdlfkj 3,29 g. lasdfj alsdfjasl 2.86 gr. slkjds sldkd lskdjfsl sdfkj kdjlksj 3,34 g... (4 Replies)
Discussion started by: sdf
4 Replies

7. 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

8. Shell Programming and Scripting

Extract character from string

ps -eaf | grep “oracleTRLV (LOCAL=NO)” | while read ora_proc do echo $ora_proc done I would like to modify the above shell so that if character 13 and 14 equal "12" to do something. Sorry I'm new to shell:( (14 Replies)
Discussion started by: NicoMan
14 Replies

9. Shell Programming and Scripting

extract character + 1

Hi, I would like extract from a file a character or pattern after ( n + 1) a specific pattern (n) . ( i supposed with awk) how could i do ? Thanks in advance. (1 Reply)
Discussion started by: francis_tom
1 Replies

10. UNIX for Advanced & Expert Users

Extract a character

HI Guys, Have a Doubt...... I have a pattern "abcdef" and i need to extract the third character..ie(c) How to achieve it? (10 Replies)
Discussion started by: aajan
10 Replies
Login or Register to Ask a Question