使用shell脚本替换第二列 [英] Replacing second column using shell script
本文介绍了使用shell脚本替换第二列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想将第二列更改为大写,我只想在shell脚本中执行此操作。 (没有内衬!)
I want to change the second column to upper case and I want to do it in shell script only. (no one liners!)
#!/bin/sh
# read file line by line
file="/pdump/country.000000.txt"
while read line
do
mycol=`echo $line | awk -F"," '{print $2}'`
mycol_new=`echo $mycol | tr "[:lower:]" [:upper:]`
echo $line | awk -F"," '{print $1 "," $mycol_new "," $3 "," $4 "," $5 "," $6 "," $7 "," $8}'
done < $file
我无法用$ mycol_new取代$ 2。
任何建议?
I am not able to replace the $2 with $mycol_new. Any suggestion?
推荐答案
awk
看不到 $ mycol_new
,因为它是一个shell变量。下面是使用 -v
标志将shell变量传递到awk的一种方法:
awk
cannot see $mycol_new
because it is a shell variable. Here is one way of passing a shell variable into awk using the -v
flag:
echo $line | awk -v var="$mycol_new" -F"," '{print $1 "," var "," $3 "," $4 "," $5 "," $6 "," $7 "," $8}'
这是另一种方法,它让shell扩展 $ mycol_new
:
Here is an alternative method which lets the shell expand $mycol_new
:
echo $line | awk -F"," '{print $1 ",'"$mycol_new"'," $3 "," $4 "," $5 "," $6 "," $7 "," $8}'
这篇关于使用shell脚本替换第二列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文