Skip to main content
Tweeted twitter.com/StackUnix/status/999916207267446784
added 7 characters in body
Source Link
bachr
  • 483
  • 1
  • 4
  • 10

I've a CSV file with the following format (; is the delimiter):

KEY;..;..;..;..;id1;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. KEY;..;..;..;..;id2;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. 

I need to take the id (6th column) from each line starting with KEY then append it to the following rows starting with SUBKEY. So the result should be something like this:

SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id2 SUBKEY;..;..;id2 SUBKEY;..;..;id2 

Any idea how this can be done in bash script?

I've a CSV file with the following format (; is the delimiter):

KEY;..;..;..;..;id1;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. KEY;..;..;..;..;id2;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. 

I need to take the id (6th column) from each line starting with KEY then append it to the following rows starting with SUBKEY. So the result should be something like this:

SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id2 SUBKEY;..;..;id2 SUBKEY;..;..;id2 

Any idea how this can be done in bash?

I've a CSV file with the following format (; is the delimiter):

KEY;..;..;..;..;id1;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. KEY;..;..;..;..;id2;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. 

I need to take the id (6th column) from each line starting with KEY then append it to the following rows starting with SUBKEY. So the result should be something like this:

SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id2 SUBKEY;..;..;id2 SUBKEY;..;..;id2 

Any idea how this can be done in bash script?

edited tags
Link
Jeff Schaller
  • 68.8k
  • 35
  • 122
  • 266
Source Link
bachr
  • 483
  • 1
  • 4
  • 10

Append a column from a previous match in CSV file

I've a CSV file with the following format (; is the delimiter):

KEY;..;..;..;..;id1;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. KEY;..;..;..;..;id2;..;.. SUBKEY;..;.. SUBKEY;..;.. SUBKEY;..;.. 

I need to take the id (6th column) from each line starting with KEY then append it to the following rows starting with SUBKEY. So the result should be something like this:

SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id1 SUBKEY;..;..;id2 SUBKEY;..;..;id2 SUBKEY;..;..;id2 

Any idea how this can be done in bash?