How to print string after colon?

 
Thread Tools Search this Thread
Top Forums UNIX for Beginners Questions & Answers How to print string after colon?
# 1  
Old 11-14-2017
How to print string after colon?

Hi

How do I print character after colon":"
below is input file
Code:
BUC1  : 33157147, BUC1 COUNT : 478455,BUC1 : 9930334.18
BUC2  : 1203100,BUC2 COUNT : 318678,BUC2 GIVEN : 3493491.59
BUC3  : 234567.99

expected output

Code:
 
33157147 478455 9930334.18
12031002 318678 3493491.59
234567.99

# 2  
Old 11-14-2017
try this:

Code:
tr -s ',' ' ' < filename | 
awk '{ for (i=1;i<=NF;i++)
       { if($(i)==":"){printf("%s ",$(i+1) ) }
       }
       printf("\n")
     }'

You could also use -F '[ ,]' as the field separator instead of using tr but not all awk implementations support regexes for specifying FS.
# 3  
Old 11-14-2017
something along these lines:
Code:
awk -F'[:,]' '{for(i=1;i<=NF;i=i+2) printf("%s%s", $(i+1), (i+2>NF)?ORS:OFS)}' myFile

# 4  
Old 11-14-2017
Another approach:
Code:
awk -F, '{gsub(/[^,]*: /,x); $1=$1}1' file

--
or
Code:
sed 's/[^,]*: //g; s/,/ /g' file


Last edited by Scrutinizer; 11-14-2017 at 11:38 AM..
This User Gave Thanks to Scrutinizer For This Post:
# 5  
Old 11-15-2017
Hi Scrutinizer

below command works for me but can you please explain me . I am not able to understand it. like what is the use of
Code:
[^,]*

Code:
//g;

Code:
s/,/ /g'

Code:
 
 sed 's/[^,]*: //g; s/,/ /g' file

# 6  
Old 11-15-2017
Quote:
Originally Posted by scriptor
Hi Scrutinizer

below command works for me but can you please explain me . I am not able to understand it. like what is the use of
Code:
[^,]*

In sed, and other tools such as grep and awk, [] is a character class - a character that can match anything inside the brackets. This can be a list, a range, or a combination of the two. If the first character after the left bracket is a carat (^) you invert the class, so the above matches anything except the comma. The asterisk is a special character that tells sed to match the previous character zero or more times. So [^,]* could match anything up to and not including a comma, or anything after a comma.
Quote:
Code:
//g;

delete
Quote:
Code:
s/,/ /g'

Change comma to space
Quote:
Code:
 
 sed 's/[^,]*: //g; s/,/ /g' file

Delete all strings in the line that don't include the comma and end in ": ";
Change all commas to spaces.

Andrew
This User Gave Thanks to apmcd47 For This Post:
# 7  
Old 11-16-2017
Hi Andrew,

thx a lot, you explained it very well.
however I am still not able to understand explanation for this
Code:
 //g;

.

i an not able to understand the working as per below explanation
Code:
Delete all strings in the line that don't include the comma and end in ": ";

it will be really helpful to me if you explain it again for me

thx in advanced
Scriptor

Last edited by scriptor; 11-16-2017 at 02:29 AM.. Reason: typr error
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Print after colon

I have entries like below in a file 11.22.33.44:80 22.33.44.55:81 :::587 :::465 What I need is to take out the part after colon ( : ) Output should be as follows. 80 81 587 465 I used cut -d: -f 2 but its not working as dedired. (2 Replies)
Discussion started by: anil510
2 Replies

2. Shell Programming and Scripting

Find a string and print all lines upto another string

Ok I would like to do the following file test contains the following lines. between the lines ABC there may be any amount of lines up to the next ABC entry. I want to grep for the filename.txt entry and print the lines in between (and including that line) up to and including the last line... (3 Replies)
Discussion started by: revaroo
3 Replies

3. Shell Programming and Scripting

Use of colon

There was a sample code on forum I found sometime back: $ f() { local foo=; : ${foo=unset}; declare -p foo; }; f declare -- foo="" $ f() { local foo; : ${foo=unset}; declare -p foo; }; f declare -- foo="unset" Can someone explain why was colon (:) is being used here. Whats its use? (4 Replies)
Discussion started by: Rameshck
4 Replies

4. Shell Programming and Scripting

Print the word after the string

Hi I have a requirment here. I have to out the string after the particular word. for example i have the to extract the first word after the word disk. help me out. i have tried the folloing code but it is not giving the output which i need. awk -F"*disk " '{print $1}' grep -n -o '' file Input... (2 Replies)
Discussion started by: saaisiva
2 Replies

5. Shell Programming and Scripting

Running multiple commands stored as a semi-colon separated string

Hi, Is there a way in Korn Shell that I can run multiple commands stored as a semi-colon separated string, e.g., # vs="echo a; echo b;" # $vs a; echo b; I want to be able to store commands in a variable, then run all of it once and pipe the whole output to another program without using... (2 Replies)
Discussion started by: svhyd
2 Replies

6. UNIX for Dummies Questions & Answers

find string and and print another string

i have a file that looks like this ABC123 aaaaaaaaaaaaaaasssssssssssssssffhhh ABC234 EMPTY ABC652 jhfffffffffffffffffffffffffffffffffffkkkkkkkkkkkk i want to grep "EMPTY" and print ABC234 (3 Replies)
Discussion started by: engr.jay
3 Replies

7. Shell Programming and Scripting

How to print Following string?

Dear All, I am new to shell script.I want to print following string: "E:\OutputRef\ExtendedTestObjectModel\Test.txt" For that i am using: echo "$ADL_ODT_REF${ADL_ODT_SLASH}ExtendedTestObjectModel${ADL_ODT_SLASH}$ResultFile" where - $ADL_ODT_REF is E:\OutputRef $ADL_ODT_SLASH... (5 Replies)
Discussion started by: Sandeep Pattil
5 Replies

8. Shell Programming and Scripting

Print the string between spaces

How to print the strings within a line between two spaces . <ns1:providerErrorCode>141</ns1:providerErrorCode> <ns1:providerErrorText>business_rule_exception-Server.404:Cannot proceed because the subscriber with phone number is either suspended or the account has an unpaid... (8 Replies)
Discussion started by: raghunsi
8 Replies

9. Shell Programming and Scripting

Grep a string and print a string from the line below it

I know how to grep, copy and paste a string from a line. Now, what i want to do is to find a string and print a string from the line below it. To demonstrate: Name 1: ABC Age: 3 Sex: Male Name 2: DEF Age: 4 Sex: Male Output: 3 Male I know how to get "3". My biggest problem is to... (4 Replies)
Discussion started by: kingpeejay
4 Replies

10. UNIX for Dummies Questions & Answers

Can I use sed to insert a string which has colon

Hi, all, I wonder if I can use sed to insert a string which has a colon. I have a txt file a.txt like the following TRAIN/DR1/FCJF0/SI1027.MFC TRAIN/DR1/FCJF0/SI1657.MFC I want to insert a string C:/TIMIT/TIMIT at the begining of each line. I use the commond: TIM=C\:/TIMIT/TIMIT... (2 Replies)
Discussion started by: Jenny.palmy
2 Replies
Login or Register to Ask a Question