1

I have the following situation:

A csv with users names and another Value (something like a serial number)

Test; User; WKCVW432994; ; ;
Test; Users; Middlename; WEJDFH34992; ;
Test; Some; Weird; Guy; WEFDJJ23453;
Test; Some; Even; Weirder; Guy; WEFDJJ23453

What I exactly need is to omit the delimiters and paste in a static value behind the name and before the serial number, like:

Test User StaticValue WKCVW432994
Test Users Middlename StaticValue WEJDFH34992
Test Some Weird Guy StaticValue WEFDJJ23453
Test Some Even Weirder Guy StaticValue WEFDJJ23453

I already got it managed for the guys with one fore and one surname:

awk -F";" '{k=$3; $3="StaticValue"; $4=k OFS $(NF--); print}' in.csv > out.csv

The difference between the name length / number of cells for the complete name I need is my problem at the moment.

4
  • 1
    Count from the end of the line, not from the start ;) – Panki Dec 11 '19 at 15:34
  • You should really work on fixing the format of the csv rather than working around it. It's not sane data if the number of fields can change like that. – jesse_b Dec 11 '19 at 15:35
  • @Panki: I don't think that would help either because in OP's example there are random empty fields at the end of some of the lines for no apparent reason whatsoever. Whatever created this file needs to be fixed. – jesse_b Dec 11 '19 at 15:42
  • Actually, there are always six fields (5 delimiters) in the examples given. The issue is that the serial number is in the field directly after the last component of the name. What the OP asks will actually make further processing of the file harder. The staticvalue should probably be -- a comma. Radical! – Paul_Pedant Dec 12 '19 at 19:22
0

Counting from back is actually the right approach. On GNU awk you could try

awk -F ";" '{last=NF; while ($last ~ /^[[:space:]]*$/) last--; new=last+1; $new=$last; $last="StaticValue"; print}' in.csv  > out.csv

which would determine the last "non-empty" field by RegExp matching and inserting -- just as you did -- the "static value" before that.

For mawk, the syntax needs slight modification:

awk -F ";" '{last=NF; while ($last ~ /^ *$/) last--; new=last+1; $new=$last; $last="StaticValue"; print}' in.csv  > out.csv
7
  • It just helps for the longest one: ```` Test User WKCVW432994 StaticValue Test Users Middlename WEJDFH34992 StaticValue Test Some Weird Guy WEFDJJ23453 StaticValue Test Some Even Weirder Guy StaticValue WEFDJJ23453 ```` – Degenhardt Dec 11 '19 at 16:29
  • What happens if (for debugging) you just use print last after the while ( ... ) last--;? – AdminBee Dec 11 '19 at 16:32
  • It shows: 6 6 6 6 btw: my version is mawk 1.3.3 – Degenhardt Dec 11 '19 at 16:35
  • I see. I guess my syntax requires GNU awk. Perhaps I can come up with a mawk-compatible solution ... – AdminBee Dec 11 '19 at 16:35
  • THANKS a lot :) :) It works :) – Degenhardt Dec 11 '19 at 16:40
1

All you need is:

$ awk '{gsub(/;/,FS); $NF="StaticValue " $NF} 1' file
Test User StaticValue WKCVW432994
Test Users Middlename StaticValue WEJDFH34992
Test Some Weird Guy StaticValue WEFDJJ23453
Test Some Even Weirder Guy StaticValue WEFDJJ23453

The above will work using any awk in any shell on every UNIX box.

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.