0

Now I have a file format like

ACTG,CD1,234
BGTY,CD2,561
CFRT,CD3,27
DGTY,CD4,45
EYTG,CD5,23
FJUI,CD1,78
GYHJ,CD2,89
HYHG,CD3,107
IUHJHU,CD4,55
JMJGT,CD5,77

I want my output file like:

CD1,ACTG,234
CD1,FJUI,78

Can anybody tell me what the linux command will be for doing this?

  • Welcome to U/L! I noticed that you cross-posted here. Unfortunately, cross-posting on multiple sites is discouraged, and will result in the question being closed. – Sparhawk May 29 '19 at 2:26
  • The cross-post has since been deleted. – Jeff Schaller May 29 '19 at 20:23
  • Wendy, can you use some words to explain the transformation you're expecting, just so that Answerers don't have to guess themselves? – Jeff Schaller May 29 '19 at 20:23
1
awk 'BEGIN { FS=","; OFS="," } $2 == "CD1" { print $2, $1, $3 }' inputfile
| improve this answer | |
  • The second column is not "1" exactly, I just use 1 to represent the data, the exact data is "CD127", I replace "1" to CD127 in the command you gave but did not work – SherlockLTS May 28 '19 at 20:31
  • And my input file actually not tab separate it's separate by "," and I updated the file format at this answer. – SherlockLTS May 28 '19 at 20:59
  • 1
    @WendyHuang With the field separator changed and quotes, try awk -F, 'BEGIN{OFS=","} $2 == "CD1" { print $2, $1, $3 }' inputfile. I'm an awk noob, but it seems to do the job. – Freddy May 28 '19 at 21:38
  • @Freddy I tried your command, it did extract the row that second column is "CD1" but it didn't print out the third column in the result. – SherlockLTS May 29 '19 at 0:43
  • @WendyHuang Hmm, I can't reproduce it. Any difference if you use awk 'BEGIN{FS=","; OFS=","} $2 == "CD1" { print $2, $1, $3 }' inputfile? This is the same command, just different syntax. And if you remove $2 == "CD1" it should just swap column one and two of your data. If that doesn't help we'll have to wait for an expert... – Freddy May 29 '19 at 1:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.