Unix/Linux Go Back    


UNIX for Beginners Questions & Answers If you're not sure where to post a Unix or Linux question, post it here. All unix and Linux beginners welcome in this forum!

How to print string after colon?

UNIX for Beginners Questions & Answers


Reply    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:49 AM EST
Posts: 157
Thanks: 18
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #2  
Old Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
jim mcnamara jim mcnamara is offline Forum Staff  
...@...
 
Join Date: Feb 2004
Last Activity: 22 November 2017, 2:26 PM EST
Location: NM
Posts: 11,240
Thanks: 571
Thanked 1,117 Times in 1,031 Posts
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.
Sponsored Links
    #3  
Old Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
vgersh99's Unix or Linux Image
vgersh99 vgersh99 is offline Forum Advisor  
Forum Advisor
 
Join Date: Feb 2005
Last Activity: 22 November 2017, 11:47 PM EST
Location: Foxborough, MA
Posts: 8,202
Thanks: 231
Thanked 785 Times in 732 Posts
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 Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
Scrutinizer's Unix or Linux Image
Scrutinizer Scrutinizer is offline Forum Staff  
Moderator
 
Join Date: Nov 2008
Last Activity: 22 November 2017, 8:17 PM EST
Location: Amsterdam
Posts: 11,625
Thanks: 518
Thanked 3,381 Times in 2,980 Posts
Another approach:

Code:
awk -F, '{gsub(/[^,]*: /,x); $1=$1}1' file

--
or

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


Last edited by Scrutinizer; 1 Week Ago at 10:38 AM..
The Following User Says Thank You to Scrutinizer For This Useful Post:
vgersh99 (1 Week Ago)
Sponsored Links
    #5  
Old Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:49 AM EST
Posts: 157
Thanks: 18
Thanked 0 Times in 0 Posts
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

Sponsored Links
    #6  
Old Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
apmcd47 apmcd47 is offline
Registered User
 
Join Date: Feb 2011
Last Activity: 22 November 2017, 11:43 AM EST
Posts: 272
Thanks: 12
Thanked 74 Times in 71 Posts
Quote:
Originally Posted by scriptor View Post
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
The Following User Says Thank You to apmcd47 For This Useful Post:
scriptor (1 Week Ago)
Sponsored Links
    #7  
Old Unix and Linux 1 Week Ago   -   Original Discussion by scriptor
scriptor scriptor is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 1:49 AM EST
Posts: 157
Thanks: 18
Thanked 0 Times in 0 Posts
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; 1 Week Ago at 01:29 AM.. Reason: typr error
Sponsored Links
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Print after colon anil510 Shell Programming and Scripting 2 03-12-2015 12:55 PM
Find a string and print all lines upto another string revaroo Shell Programming and Scripting 3 03-12-2015 08:10 AM
Running multiple commands stored as a semi-colon separated string svhyd Shell Programming and Scripting 2 06-06-2011 10:35 PM
Grep a string and print a string from the line below it kingpeejay Shell Programming and Scripting 4 06-23-2009 01:26 PM
Can I use sed to insert a string which has colon Jenny.palmy UNIX for Dummies Questions & Answers 2 04-28-2008 08:04 PM



All times are GMT -4. The time now is 01:43 AM.