Sponsored Content
Top Forums UNIX for Beginners Questions & Answers How to print string after colon? Post 303007439 by Don Cragun on Thursday 16th of November 2017 02:53:21 PM
Old 11-16-2017
Quote:
Originally Posted by MadeInGermany
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

This User Gave Thanks to Don Cragun For This Post:
 

10 More Discussions You Might Find Interesting

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

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

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

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

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

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

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

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

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

10. 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
All times are GMT -4. The time now is 06:27 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy