1

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

2 Answers 2

4

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.
0
2

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .