1

I have this two types of outputs:

UID:474D229F698D494E889D85CEF9303B97:480 f
UID:474D229F698D494E889D85CEF9303B97:480

I want to get the 32 char long uid with the 480 at the end of it. (Note that there is nothing after 480 for the second type of input) Desired output:

474D229F698D494E889D85CEF9303B97:480
474D229F698D494E889D85CEF9303B97:480

I am using sed:

cat input.txt | sed 's!UID:\(.*\):\([0-9]*\)[\s]*!Captured:\1:\2!'

but the output is:

Captured:474D229F698D494E889D85CEF9303B97:480 f
Captured:474D229F698D494E889D85CEF9303B97:480
fedorqui
  • 275,237
  • 103
  • 548
  • 598
Juto
  • 1,246
  • 1
  • 13
  • 24

3 Answers3

1

is this ok?

grep -oE '[^:]{32}:[^: ]*' file

for example:

kent$  echo "UID:474D229F698D494E889D85CEF9303B97:480 f
UID:474D229F698D494E889D85CEF9303B97:480"|grep -oE '[^:]{32}:[^: ]*'
474D229F698D494E889D85CEF9303B97:480
474D229F698D494E889D85CEF9303B97:480

same idea with sed:

sed -r 's/.*([^:]{32}:[^: ]*).*/\1/' file
Kent
  • 189,393
  • 32
  • 233
  • 301
  • Can you please elaborate a bit more? What does [^:] do? – Juto Jul 24 '13 at 14:19
  • @Juto `[^:]` means any char except `:` – Kent Jul 24 '13 at 14:20
  • echo "UID:474D229F698D494E889D85CEF9303B97:480 f" | sed 's!UID:\(.{32}:[^: ]*\).*!Captured:\1!' is not giving the correct answer... UID:474D229F698D494E889D85CEF9303B97:480 f – Juto Jul 24 '13 at 14:29
  • @Juto did you try my sed command? do you have gnu sed? I had `-r`. I post a command, you test something different, and report not working... :\ – Kent Jul 24 '13 at 14:30
0

awk to the rescue?

$ awk -F"[: ]" '{print $2":"$3}' file
474D229F698D494E889D85CEF9303B97:480
474D229F698D494E889D85CEF9303B97:480

Explanation: we define different possible field separators : or space . Once the text has been splitted, then we print the 2nd and 3rd field.

sed way could be the following:

$ sed 's/UID:\([^:]*\):\([^ ]*\).*/Captured:\1:\2/g' file
Captured:474D229F698D494E889D85CEF9303B97:480
Captured:474D229F698D494E889D85CEF9303B97:480

Explanation: We see the text is based on the pattern UID:number:number something. Hence, we get it with UID:\([^:]*\):\([^ ]*\).*. With \( expression \) we capture the text we need, so that it can be printed later on with \1, \2...

fedorqui
  • 275,237
  • 103
  • 548
  • 598
  • Thanks, I will try that, but any way in sed to do it? I have been trying to figure this out for the past 2 hours... – Juto Jul 24 '13 at 14:18
  • Sorry that I didn't check it myself, it works, I think my problem was not knowing how to not get the space! Thanks very much! – Juto Jul 24 '13 at 14:41
  • Did it solve your problem, @Juto ? Since you're new here, please don't forget to mark the answer accepted if your problem is already solved. All posted ones are fine! – fedorqui Jul 24 '13 at 14:45
0

In bash, you can use the parameter expansion:

s=${s% *}    # Remove everything after space.
echo ${s#*:} # Remove everything before colon.
choroba
  • 231,213
  • 25
  • 204
  • 289