0

Can somebody assist in helping me solve this problem. I have a file that I've extracted and the file contents look like this.

(11213068, 2020-11-16) deleted
(1075227404, 2021-06-14) added
(11213177, 2020-11-16) deleted
(1075227413, 2021-06-14) added
(11213070, 2020-11-16) deleted
(1075193958, 2021-05-28) added
(1075194668, 2022-11-29) added
(1073757334, 2021-01-20) (1073757337, 2021-01-20) (1073757349, 2021-01-20) (1073757331, 2021-01-20) (1073757346, 2021-01-20) added
(1073757237, 2020-11-20) (1073757263, 2020-11-20) (1073757233, 2020-11-20) (1073757241, 2020-11-20) (1073757247, 2020-11-20) deleted

++ My desired results for the file would look like this --

(11213068, 2020-11-16) delete
(1075227404, 2021-06-14) add
(11213177, 2020-11-16) delete
(1075227413, 2021-06-14) add
(11213070, 2020-11-16) delete
(1075193958, 2021-05-28) add
(1075194668, 2022-11-29) add
(1073757334, 2021-01-20) add
(1073757337, 2021-01-20) add
(1073757349, 2021-01-20) add
(1073757331, 2021-01-20) add
(1073757346, 2021-01-20) add
(1073757237, 2020-11-20) delete
(1073757263, 2020-11-20) delete
(1073757233, 2020-11-20) delete
(1073757241, 2020-11-20) delete
(1073757247, 2020-11-20) delete

On the last 2 lines I am unable to figure out a solution. My output using this command --

awk '$3!="added"' | awk '$3!="deleted"' | sed 's/) (/\n/g' file.txt

(11213068, 2020-11-16) deleted
(1075227404, 2021-06-14) added
(11213177, 2020-11-16) deleted
(1075227413, 2021-06-14) added
(11213070, 2020-11-16) deleted
(1075193958, 2021-05-28) added
(1075194668, 2022-11-29) added
(1073757334, 2021-01-20
1073757337, 2021-01-20
1073757349, 2021-01-20
1073757331, 2021-01-20
1073757346, 2021-01-20) added
(1073757237, 2020-11-20
1073757263, 2020-11-20
1073757233, 2020-11-20
1073757241, 2020-11-20
1073757247, 2020-11-20) deleted

Thank you for your time.

0

4 Answers 4

3

Use the right delimiter for this.

awk -F') ' '{for (i=1;i<NF;i++) print $i FS $NF}' file

If you need to replace the last field, there are many ways to do it, for example using sub() at the beginning of the line processing.

awk -F') ' '{sub(/added$/,"add"); sub(/deleted$/,"delete"); for (i=1;i<NF;i++) print $i FS $NF}' file
2
  • Brilliant!! Thank you so much!! :)
    – Chinaj2020
    Dec 4, 2020 at 5:59
  • 1
    @Chinaj2020 If you found the answer useful, please consider accepting it so that others facing a similar problem may find it more easily.
    – AdminBee
    Dec 4, 2020 at 13:14
1

GNU sed with extensive regex mode -E.

  • Mark regions sandwiched between )
    ( with a newline marker. Then transport the last field (after it has been sanitized of its past tense) into the first marker, print upto the first marker, and then chop off upto the first marker. Repeat this process until we exhaust the pattern space.

$ sed -Ee '/\n/ba
    /e?d$/s/ (add|delete)e?d$/ \1/
    s/[)] [(]/) \n(/g;:a
    s/(\n.*)?\n.* (\S+)$/\2&/
    /\n.*\n/{P;D;}
' file

$ perl -F'\)\s' -lane '$, = ") ";
    my $l = pop(@F) =~
     s/^(add)ed$/$1/r =~
      s/^(delete)d$/$1/r;
    print $_, $l for @F;
' file

1

Perhaps a two-staged solution?

<infile sed 's/deleted/delete/; s/added/add/' | 
awk 'NF==3; NF>3 { for (i=1; i<NF; i+=2) print $i, $(i+1), $NF }'
3
  • 2
    You never need sed when youre using awk. sed 's/deleted/delete/; s/added/add/' | awk 'foo' = awk '{sub(/deleted/,"delete"); sub(/added/,"add")} foo
    – Ed Morton
    Dec 5, 2020 at 17:31
  • Indeed, but it looks cleaner
    – Thor
    Dec 6, 2020 at 15:07
  • We'll very much have to agree to disagree on that point!
    – Ed Morton
    Dec 6, 2020 at 17:09
1

With GNU awk for FPAT:

$ awk -v FPAT='[(][^)]+)|\\S+' '{for (i=1; i<NF; i++) print $i, $NF}' file
(11213068, 2020-11-16) deleted
(1075227404, 2021-06-14) added
(11213177, 2020-11-16) deleted
(1075227413, 2021-06-14) added
(11213070, 2020-11-16) deleted
(1075193958, 2021-05-28) added
(1075194668, 2022-11-29) added
(1073757334, 2021-01-20) added
(1073757337, 2021-01-20) added
(1073757349, 2021-01-20) added
(1073757331, 2021-01-20) added
(1073757346, 2021-01-20) added
(1073757237, 2020-11-20) deleted
(1073757263, 2020-11-20) deleted
(1073757233, 2020-11-20) deleted
(1073757241, 2020-11-20) deleted
(1073757247, 2020-11-20) deleted

or if you really want to change those words at the end:

$ awk -v FPAT='[(][^)]+)|\\S+' '
    BEGIN { map["deleted"]="delete"; map["added"]="add" }
    { for (i=1; i<NF; i++) print $i, map[$NF] }
' file
(11213068, 2020-11-16) delete
(1075227404, 2021-06-14) add
(11213177, 2020-11-16) delete
(1075227413, 2021-06-14) add
(11213070, 2020-11-16) delete
(1075193958, 2021-05-28) add
(1075194668, 2022-11-29) add
(1073757334, 2021-01-20) add
(1073757337, 2021-01-20) add
(1073757349, 2021-01-20) add
(1073757331, 2021-01-20) add
(1073757346, 2021-01-20) add
(1073757237, 2020-11-20) delete
(1073757263, 2020-11-20) delete
(1073757233, 2020-11-20) delete
(1073757241, 2020-11-20) delete
(1073757247, 2020-11-20) delete

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.