1

I have some engineering data in a CSV file with the following format:

A1, B1
C2, D2, E2
F3, G3, H3

I need to transform this file into the following CSV format:

B1
E2
H3

In other words, retain rows 2 and above of column 3 only; but field B1 needs to be shifted/aligned to column 3.

In other words, C2, D2, F3 and G3 are discarded.

There are a lot of awk, sed, cut and paste solutions for shifting, copying and pasting whole columns or rows. But I don't find any solutions for this sort of custom manipulation though.

peterh
  • 11,875
  • 18
  • 85
  • 108
DwayneB
  • 19
  • 2
  • 2
    Could you please clarify "retain rows 2 and above of column 3 only". Seems like you just want the last value of each row, is that right? If so, "awk '{print $NF}' filename" will do it. Which would be a dup of https://stackoverflow.com/questions/17921544/get-last-field-using-awk-substr – flu Jul 18 '17 at 19:31
  • Little grammar: in English sentences, the personal pronoun is (nearly) always there. It is because they practically don't have suffixes at the end of the verbs, so they have to use the personal pronouns for the same task. If you say "Have some engineering data", it doesn't mean "*I* have some...", but it means "*You should have* some...". Because the sentence will look as if it would an imperativ-form. In exchange, it is not so bad style if there is the "I" in your all sentences. – peterh Jul 18 '17 at 23:52
  • `field B1 needs to be shifted/aligned to column 3. In other words, C2, D2, F3 and G3 are discarded.` What an extremely strange way of saying `print the last column of each line`. If that's NOT what you meant then edit your question to clarify. – Ed Morton Jul 19 '17 at 02:59

2 Answers2

2

Assuming your input file is jnk

awk -F"," '{print $NF}' jnk
JimR
  • 473
  • 2
  • 11
  • 1
    Get rid of the `-F` setting (or make it `-F', '`) to avoid getting a leading blank char in the output. – Ed Morton Jul 19 '17 at 03:00
0

Thanks for all the helpful advice. I finally got the following approach to work, maybe not the most efficient but works.

\ Output first 4 rows of column 2 to appendme.csv

awk -F, 'NR>=1 && NR<=4 { print $2 }' input.csv >> appendme.csv

\ Grab the useful column to data1.csv

awk -F, '{print $3}' input.csv >> data1.csv

\Delete empty rows 1-4 output data2.csv

sed -e '1,4d' < data1.csv > data2.csv

\ Output data2 contents into appendme.csv (single column so implied pending appending to $1)

awk -F, '{print $1}' data2.csv >> appendme.csv

DwayneB
  • 19
  • 2