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
    #8  
Old Unix and Linux 6 Days Ago   -   Original Discussion by scriptor
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 3:49 AM EST
Location: San Jose, CA, USA
Posts: 10,673
Thanks: 572
Thanked 3,737 Times in 3,189 Posts
You're looking at too small a part of the command. A sed substitute command is usually seen in the form:

Code:
s/BasicRegularExpression/ReplacementString/flags

There are two sed substitute commands in this case separated by the semicolon (;).

In the first sed substitute command, the Basic Regular Expression is [^,]*: which matches a string of adjacent characters that are not a comma ([^,]) appearing zero or more times (*) followed by a colon (:) followed by a single space character. The Replacement String in this case is the empty string (which effectively removes the matched string). And the flags in this case is g which requests that the substitution be applied globally to all non-overlapping strings that match the BRE.

In the second sed substitute command, the BRE is , which matches a comma character, the replacement string is a space character, and, again, the g flag requests that each comma found on the line be replaced by a space.
Sponsored Links
    #9  
Old Unix and Linux 5 Days Ago   -   Original Discussion by scriptor
MadeInGermany MadeInGermany is offline Forum Staff  
Moderator
 
Join Date: May 2012
Last Activity: 22 November 2017, 2:40 AM EST
Location: Simplicity
Posts: 3,827
Thanks: 319
Thanked 1,283 Times in 1,161 Posts
I wonder if one can include an optional comma ,\{0,1\} in the main substitution

Code:
sed 's/,\{0,1\}[^,]*: //g' file

Sponsored Links
    #10  
Old Unix and Linux 5 Days Ago   -   Original Discussion by scriptor
Don Cragun's Unix or Linux Image
Don Cragun Don Cragun is offline Forum Staff  
Administrator
 
Join Date: Jul 2012
Last Activity: 22 November 2017, 3:49 AM EST
Location: San Jose, CA, USA
Posts: 10,673
Thanks: 572
Thanked 3,737 Times in 3,189 Posts
Quote:
Originally Posted by MadeInGermany View Post
I wonder if one can include an optional comma ,\{0,1\} in the main substitution

Code:
sed 's/,\{0,1\}[^,]*: //g' file

One can do that, but if you do the output from the input line:

Code:
BUC2  : 1203100,BUC2 COUNT : 318678,BUC2 GIVEN : 3493491.59

becomes:

Code:
12031003186783493491.59

instead of:

Code:
1203100 318678 3493491.59

One could also get rid of the space after the colon in the BRE:

Code:
sed 's/,\{0,1\}[^,]*://g' file

and that would make the output from the above input line be:

Code:
 1203100 318678 3493491.59

which is almost what is wanted, but has an extraneous leading space on every output line.

One could also try:

Code:
sed 's/[^,]*: \([0-9.]*\),\{0,1\}/\1 /g' file

and that looks like the desired output to the naked eye, but has an extraneous trailing space on every output line.

The following seems to do what is wanted with a single sed substitution command:

Code:
sed 's/[^,]*: \([0-9.]*\),\{0,1\}[^ ]*\( \{0,1\}\)/\1\2/g' file

but it is not something that I would suggest for someone who is just learning how to use sed and learning how to write sed BREs (unless I was trying to show an example of the sed-specific extensions to standard BREs and the use of back references in replacement strings).

Note that all of the examples above work with standards-conforming versions of sed, but might need an added option on the command line to make it work with GNU sed. For example, the last example above, when using GNU sed, probably needs to be invoked with:

Code:
sed --posix 's/[^,]*: \([0-9.]*\),\{0,1\}[^ ]*\( \{0,1\}\)/\1\2/g' file

The Following User Says Thank You to Don Cragun For This Useful Post:
MadeInGermany (5 Days Ago)
    #11  
Old Unix and Linux 2 Days Ago   -   Original Discussion by scriptor
Aia's Unix or Linux Image
Aia Aia is offline
Registered User
 
Join Date: May 2008
Last Activity: 20 November 2017, 9:27 AM EST
Posts: 1,673
Thanks: 49
Thanked 641 Times in 601 Posts

Code:
perl -nle '@n = /:\s?([\d.]*)/g and print "@n"' scriptor.file

Output:

Code:
33157147 478455 9930334.18
1203100 318678 3493491.59
234567.99


Explanation:


Code:
@n = /:\s?([\d.]*)/g # extract decimal and non decimal numbers
and print "@n"'  # display if it got extraction

The Following User Says Thank You to Aia For This Useful Post:
MadeInGermany (18 Hours Ago)
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 05:22 AM.