سؤال

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
هل كانت مفيدة؟

المحلول 2

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

نصائح أخرى

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

In bash, you can use the parameter expansion:

s=${s% *}    # Remove everything after space.
echo ${s#*:} # Remove everything before colon.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top