4

This is the line format of the my file:

29.101.26.176 [11/Oct/2017:10:21:41 /pmk/clk?aff_id=5863&off_id=85635&trans_id=easoli4ddq8sssdf&prm2=23398_27530&device_id=864792012331091090 "Mozilla/5.0 Mobile

I need to cut aff_id=5863 and off_id=85635 and trans_id=easoli4ddq8sssdf.

I can only cut the specific field from cut command not the word inside the field. How can I separate the given word?

1
  • 1
    do you need to extract them or modify the line by deleting them?
    – Sundeep
    Oct 13, 2017 at 6:32

2 Answers 2

2

Short grep approach:

grep -Eo '([ao]ff|trans)_id=[^&]+' file

The output:

aff_id=5863
off_id=85635
trans_id=easoli4ddq8sssdf

----------

Or awk solution for fixed item order:

awk -F'[?&]' '{ printf "%s %s %s\n",$2,$3,$4 }' file

The output:

aff_id=5863 off_id=85635 trans_id=easoli4ddq8sssdf
0
1

Use GNU sed to remove matched patterns:

sed -E 's#([ao]ff|trans)_id=[^&]*&##g' infile

If you want get those string value.

sed 's#.*\(aff_id=[^&]\).*\(off_id=[^&]\+\).*\(trans_id=[^&]\+\).*#\1 \2 \3#' infile
aff_id=5863 off_id=85635 trans_id=easoli4ddq8sssdf

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.