1

I have a TSV file with the following format:

HAPPY    today I feel good
SAD    this is a bad day
UPSET     Hey please leave me alone!

I have to replace the first column value with a prefix like __label__ plus my value to lower, so that to have as output

__label__happy     today I feel good
__label__sad     this is a bad day
__label__upset     Hey please leave me alone!

in the shell (using awk, sed) etc.

loretoparisi
  • 15,724
  • 11
  • 102
  • 146
  • 1
    instead of `` you could have mentioned it is TSV (tab separated values) and do add the code you tried yourself – Sundeep Oct 27 '17 at 14:27

3 Answers3

4
awk 'BEGIN{FS=OFS="\t"}{ $1 = "__label__" tolower($1) }1' infile
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
1

another awk

$ awk 'sub($1,"__label__"tolower($1))' file

with GNU sed

$ sed -r 's/[^t]+/__label__\L&/' file
karakfa
  • 66,216
  • 7
  • 41
  • 56
  • It seems it loses that `` in this way. Possibile? – loretoparisi Oct 27 '17 at 14:17
  • @loretoparisi it shouldn't lose the tab since the line is not rewritten, it might be version specific though, this works with `gawk`. – karakfa Oct 27 '17 at 14:20
  • 2
    Should set FS to tab, since fields can contain spaces.. Also sed can be written more simplu as `sed -r 's/[^t]+/__label__\L&/'` – 123 Oct 27 '17 at 14:22
  • @123 golfing further - `sed 's/[^\t]*/__label__\L&/'` taking advantage of greediness – Sundeep Oct 27 '17 at 14:28
1

Following awk may also help you in same too.

awk -F"\t" '{$1=tolower($1);printf("_label_%s\n",$0)}' OFS="\t"   Input_file
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93