4

Hello I have one file with output

Name        : ABC
Name        : CDE
Name        : ZYS

I want to get the result

ABC
CDE
ZYS

How can i get it through SED command.

I have tried. sed s/'Name' but it returns me the output like: ABC

I want to trim the space and also : from the string.

7

You can use awk for this:

awk '{print $3}' yourfile

This simply prints the third field/column for every record/row.

0
3

If you use sed:

sed 's/^.*: //' yourfile

or to be a bit more precise:

sed 's/^Name[^:]*: //' yourfile

1
  • 2
    take care, the first one is greedy and eats too much if there's a ': ' in the part right of the first ':'.
    – reto
    Apr 17 '13 at 12:18
3

With recent GNU grep versions you could do it like this with positive lookbehind:

grep -oP '(?<=: ).*' infile

Or more portably:

grep -o ':.*' infile | grep -o '[^ ]*$'

Output:

ABC
CDE
ZYS
3

To complete the other answers with another way:

cat file | cut -d: -f 2- 

you can cut out the remaining whitespace

cat file | cut -d: -f 2- | cut -c 2-

Or more compact, a bit less readable:

cut -d: -f 2- < file | cut -c 2-
2
  • 1
    No need to use cat here. cut -d: -f 2- file works.
    – user26112
    Apr 17 '13 at 12:35
  • good point, thamks!
    – reto
    Apr 17 '13 at 12:59
1

You can use the awk command below:

awk -F ':' '{print $2}' 

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.