-3

Following is the content of the file and i need to print only the ticket numbers which are enclosed in "[]". Curious to know if this can be done using both sed and grep commands.

[request://Problem/26727777] SSO Components (UAT)
[request://Problem/25709048] [SERVER-SETUP] Setup 14xMDN, 10xPRN 
[request://Problem/26716590] Logs not populated properly from all servers
[request://Problem/23995808] Prod: Create requestmary

Output should be:

26727777
25709048
26716590
23995808

I tried sed -e 's/[^0-9]//g' ticket | sed '/^$/d', but I'm unable to getthe desired output. It also includes other numeric values in the line, and I need to get only the ticket number:

226829515211
226855182
2268555334
226819615
226781310
226853999
226828948216
  • I tried this sed -e 's/[^0-9]//g' ticket | sed '/^$/d’’ But I’m unable to fetch the desired output. The output comprises other numeric values in the line I need to get only the ticket number. 226829515211 226855182 2268555334 226819615 226781310 226853999 226828948216 – Sai Feb 19 '16 at 11:15
  • Please edit your question to add extra information. It is hard to read and easy to miss in the comments. Also, comments can be deleted with no warning. That said, your output shows text that isn't present in your input. You need to show us example input that is actually representative of the real input you are dealing with – terdon Feb 19 '16 at 12:04
1

You can use GNU grep:

$ grep -oP '/\K\d+(?=])' ticket 
26727777
25709048
26716590
23995808

Or sed:

sed -E 's#.*/([0-9]*).*#\1#g' ticket
26727777
25709048
26716590
23995808

Both of the above will work on the example you gave but whether they work on your real file will depend on the other lines in your file. They print all stretches of numbers that are after a / and, for the grep, that are before a ]. If you have other lines matching those criteria, you will need to show us your entire file.

1

I'd use perl, personally.

perl -lne 'print /(\d+)\]/'

Should do the trick.

-l to say 'sort out linefeeds' - it strips them automatically off the line, and inserts them after each print.

-n says 'wrap this in a while (<>) { loop - which iterates line by line of STDIN (or the file) rather like grep would.

And -e is "expression" (or "execute" I guess) to run the command in quotes. (Otherwise perl read the program from STDIN)

We use the fact that the default match for a regex is against $_ (in this context, that's the current line).

Because we specify a capture group, rather than returning "true" or "false" it returns a (single element) list of captured values:

\d+ is one or more digits. (It's an 'advanced' regex expression, that is approximately similar to [0-9]+, although works a bit differently if you're using non-latin numbers), and we require a ] after it.

And because we don't use g at the end of the regex, it only matches once per line.

0

sed 's/[^0-9]*\([0-9]*\).*/\1/'

But if the ticket number had fixed width, I'd rather use cut.

Edit: with grep: grep -o '^\[[^0-9]*[0-9]*' | grep -o '[0-9]*'

  • I just ran the command you suggested but i did not get the output i was looking for. It is just printing the first numbers of ticket. Output i got is only 2. Also, the ticket number is fixed to 9 digits. Besides cut command, would it possible to fetch the output using sed as well. – Sai Feb 19 '16 at 11:38
  • Be sure not to omit any *. – L. Levrel Feb 19 '16 at 12:05
  • BTW, I have GNU sed 4.2.2 – L. Levrel Feb 19 '16 at 12:06
-1
tr "\[\]/" " "| awk '/request/ {  print $3 }'

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.