0

I have a favor to ask for how to get the TextN from a string where there are different length/pattern before the TextN. Cut, sed and awk? any example, I appreciate.

2018/08/09-20:45:50.671483-06359-06359-433191088669655042-004- TextN
2018/08/09-20:45:50.668677-06359-06359-002-006-  TextN
2018/08/09-20:45:50.668677-063-063-002-006-  TextN

Thank you in advance.

Sorry, ** was part code tag.

AndrewS
  • 177
  • 5
  • 21
  • Please edit your Q to show your exact input text, the required output from that same text, your best effort to solve the problem with exact text of any error messages. Do you know about `sed 's/targStr/replStr/' file` (where replStr can be empty, i.e. `s/targStr//'`. Unless your data rely includes `*` chars, please remove them as they complicate an regexp that might be needed). Good luck. – shellter Aug 10 '18 at 15:03
  • if you just want the last field of line, see https://stackoverflow.com/questions/17921544/get-last-field-using-awk-substr .. you can set a custom field delimiter using `-F` option, default is whitespaces which may be the case for you... – Sundeep Aug 10 '18 at 15:20

2 Answers2

1

if the text is wrapped in double stars and don't exist elsewhere...

$ awk -F'**' '{print $2}' file

TextN
TextN
TextN

otherwise post a more representative example.

Also with sed

$ sed -E 's/.*\*\*([^*]+)\*\*.*/\1/' file

If the textN doesn't have spaces in them and if it's the last field separated with spaces...

$ awk '{print $NF}' file

since, characters, spaces have special meaning you have to pick your sample input carefully...

karakfa
  • 66,216
  • 7
  • 41
  • 56
  • Sorry karakfa. I added ** around TextN by accident. I will explore your methods. Thanks. – AndrewS Aug 10 '18 at 16:34
  • Ok. I just realized on my logs, i just need to skip first column, then extract all texts after that. like this. cat logs.txt | awk '{ $1=""; print}' – AndrewS Aug 10 '18 at 17:23
0

To complete @karakfa's awk example, using sed: sed -i -E "s/.*\*\*([a-zA-Z]+)\*\*/\1/" <your-text-file>

The previous example works only if your TextN is matched by [a-zA-Z]. You can also use something more sophisticated or add other chars. In case you don't actually know what chars will be there: sed -i -E "s/.*\*\*(.+)\*\*/\1/" <your-text-file>

In case your string is not in a file but is an environment variables or just a variable: echo $line | sed -E "s/.*\*\*(.+)\*\*/\1/"

azbarcea
  • 3,323
  • 1
  • 20
  • 25