简体   繁体   English

使用Sed或Awk替换字符串

[英]String Replacing using Sed or Awk

Cat vols.txt (This list can fluctuate depending on the vols a system might have) Cat vols.txt(此列表可能会有所波动,具体取决于系统可能具有的音量)

$Vol01
$Vol02
$Vol03
$Vol04
$Vol04
$Vol05
$Vol06
$Vol07
$Vol08

if I do: 如果我做:

cat datavols.log | sed -n 's/^\$.*/vanish \\&\.acd\*\.\* -db ! \&/gp' >> workfile.txt

I get: 我得到:

cat workfile.txt

vanish $Vol01.acd*.* -db ! &
vanish $Vol02.acd*.* -db ! &
vanish $Vol03.acd*.* -db ! &
vanish $Vol04.acd*.* -db ! &
vanish $Vol05.acd*.* -db ! &
vanish $Vol06.acd*.* -db ! &
vanish $Vol07.acd*.* -db ! &
vanish $Vol08.acd*.* -db ! &

I have got 4 CPUs and I want to distribute the work between them and the output should be something like: 我有4个CPU,我想在它们之间分配工作,输出应该是这样的:

cat workfile.txt

Run –cpu=0 vanish $Vol01.acd*.* -db ! &
Run –cpu=1 vanish $Vol02.acd*.* -db ! &
Run –cpu=2 vanish $Vol03.acd*.* -db ! &
Run –cpu=3 vanish $Vol04.acd*.* -db ! &
Run –cpu=0 vanish $Vol05.acd*.* -db ! &
Run –cpu=1 vanish $Vol06.acd*.* -db ! &
Run –cpu=2 vanish $Vol07.acd*.* -db ! &
Run –cpu=3 vanish $Vol08.acd*.* -db ! &

Kindly help me getting this. 请帮助我。 I am not sure how to have a variable iterate between 0-3 inside SED. 我不确定如何在SED中让变量在0-3之间进行迭代。 Thanks in advance. 提前致谢。

 awk 'BEGIN{i=0}{print "Run –cpu="i " vanish "$1".acd*.* -db ! &"; i=(i+1)%4}' inputfile

will produce the output as 将产生输出为

 Run –cpu=0 vanish $Vol01.acd*.* -db ! &
Run –cpu=1 vanish $Vol02.acd*.* -db ! &
Run –cpu=2 vanish $Vol03.acd*.* -db ! &
Run –cpu=3 vanish $Vol04.acd*.* -db ! &
Run –cpu=0 vanish $Vol05.acd*.* -db ! &
Run –cpu=1 vanish $Vol06.acd*.* -db ! &
Run –cpu=2 vanish $Vol07.acd*.* -db ! &
Run –cpu=3 vanish $Vol08.acd*.* -db ! &

您可以在awk使用FNR ,即文件的当前行号:

awk '{print "Run -cpu=" (FNR-1)%4 " vanish " $0 ".acd*.* -db | &"}' vols.txt

You can also use this simple bash script . 您也可以使用此简单的bash script

Total_Cpu=4
Count=0
while read line ; do
   if [ $Count  -eq  4 ] ;then
      Count=0
   fi
    echo $line | sed "s/^\$.*/Run –cpu=$Count vanish \&.acd*.* -db ! \&/g" >> Output_file
    Count=`expr $Count + 1`         
done < Input_File

This might work for you (GNU sed): 这可能对您有用(GNU sed):

sed -rn '1{x;s/^/0123/;x};G;s/(.*)\n(.)(.*)/Run -cpu=\2 vanish \1.acd*.* -db ! \&\n\3\2/;P;s/.*\n//;h file

Create a line with the sequence of the cpu's you want and store it in the hold space (HS). 用所需的CPU序列创建一行,并将其存储在保留空间(HS)中。

Append the HS to the current line and using the substitute command insert the cpu number and the required strings prepping the cpu order for the next line. 将HS附加到当前行,并使用替代命令插入cpu编号和所需的字符串,为下一行准备cpu顺序。

Print the string then replace the amended cpu order in the HS. 打印字符串,然后替换HS中修改的cpu顺序。

sed '1 {
   x;s/^/Run –cpu=3 vanish :-D.acd*.* -db ! \&/;x
   }
x;y/0123/1230/;G;s/\(.*sh \)[^.]\{1,\}\(.*\)\n\(.*\)/\1\3\2/;h' YourFile

a bit like potong bt using the last line as previous reference only changing the $vol content and cpu value 有点像potong bt,使用最后一行作为先前参考仅更改$ vol内容和cpu值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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