简体   繁体   中英

Awk Match a TSV column and replace all rows with a prefix in bash

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.

awk 'BEGIN{FS=OFS="\t"}{ $1 = "__label__" tolower($1) }1' infile

another awk

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

with GNU sed

$ sed -r 's/[^t]+/__label__\L&/' file

跟随awk也可能会帮助您。

awk -F"\t" '{$1=tolower($1);printf("_label_%s\n",$0)}' OFS="\t"   Input_file

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM