Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a tab-separated file in Linux that looks like this

~$ head list.tab
"x"
"1" "FUHGF.jnf.m22-1"
"2" "HDFHFEY.gfs.d2-1"
"3" "KJFGJF.fr.md2-1"
"4" "SDFSDIB.gfd.rtl2-1"
"5" "FGJFGOB.hgd.k2-5"
"6" "HJLHEH.fc.po2-1"
"7" "GFHFGV.gfn.col2-1"
"8" "KLAA.ghn.xil2-3"
"9" "KJGGFG.hgr.col2-2"

I would like to keep the first part of the name of the second column only. I know I will have to use cut and sed, but I cannot think of the structure of the command. My desired output is

~$ head list.tab

"FUHGF"
"HDFHFEY
"KJFGJF"
"SDFSDIB"
"FGJFGOB"
"HJLHEH"
"GFHFGV"
"KLAA"
"KJGGFG"

I've been trying with awk and gsub but I haven't used Linux for quite some time, help would be appreciated.

Many thanks

share|improve this question

2 Answers 2

up vote 4 down vote accepted

cut -sf2 -d' ' list.tab|sed -e 's/\..*/\"/'

meaning

  1. cut only the second field in lines with separators (space)
  2. replace the dot and anything that follows with quotation marks.
share|improve this answer
    
Thanks for the detailed answer :) –  Error404 Aug 12 '13 at 17:19

awk '$2 {oFS=FS; FS="."; $0=$2; print $1"\""; FS=oFS}' input

For all lines that have a second field:

  • save the current FS value
  • set FS to .
  • force awk to re-split the second field on the new FS
  • print the first field (and a trailing quotation mark)
  • set FS back to its original value
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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