bash - 使用 sed/awk 将 x^2 添加到每个 "nonzero"系数

标签 bash ubuntu awk sed

我必须尽可能简单地编写必须使用 awk 或/和 sed 的脚本或命令。 输入文件:

23 12 0 33
3 4 19

第一行 n=3
第二行 n=2
在文件的每一行中,我们都有一串数字。每个数字都是系数,我们必须添加 x^n,其中 n 是最高幂(每行数字之间的空格总和(每行最后一个数字后没有空格)),如果我们的字符串中有“0”,我们有跳过它。 因此,对于该输入,我们将得到如下输出:

23x^3+12x^2+33
3x^2+4x+19

请帮我写一个简短的脚本来解决这个问题。非常感谢您的宝贵时间和所有帮助 :) 我的想法:

linescount=$(cat numbers|wc -l)
linecounter=1
While[linecounter<=linescount];
do
i=0
for i in spaces=$(cat numbers|sed 1p | sed " " )
do
sed -i 's/ /x^spaces/g'
i=($(i=i-1))
done
linecounter=($(linecounter=linecounter-1))
done

最佳答案

遵循 awk 也可能对您有所帮助。

awk '{for(i=1;i<=NF;i++){if($i!="" && $i){val=(val?val "+" $i:$i)(NF-i==0?"":(NF-i==1?"x":"x^"NF-i))} else {pointer++}};if(val){print val};val=""} pointer==NF{print;} {pointer=""}'  Input_file

这里也添加了一个非线性形式的解决方案。

awk '
{
  for(i=1;i<=NF;i++){
    if($i!="" && $i){
       val=(val?val "+" $i:$i)(NF-i==0?"":(NF-i==1?"x":"x^"NF-i))}
    else            {
       pointer++}};
    if(val)         {
       print val};
  val=""
}
pointer==NF         {
  print}
{
  pointer=""
}
'  Input_file


编辑:在这里也添加解释,以便更好地理解 OP 和所有人在这里学习。

awk '
{
  for(i=1;i<=NF;i++){                                              ##Starting a for loop from variable 1 to till the value of NF here.
    if($i!="" && $i){                                              ##checking if variable i value is NOT NULL then do following.
       val=(val?val "+" $i:$i)(NF-i==0?"":(NF-i==1?"x":"x^"NF-i))} ##creating variable val here and putting conditions here if val is NULL then
                                                                   ##simply take value of that field else concatenate the value of val with its
                                                                   ##last value. Second condition is to check if last field of line is there then
                                                                   ##keep it like that else it is second last then print "x" along with it else keep
                                                                   ##that "x^" field_number-1 with it.
    else            {                                              ##If a field is NULL in current line then come here.
       pointer++}};                                                ##Increment the value of variable named pointer here with 1 each time it comes here.
    if(val)         {                                              ##checking if variable named val is NOT NULL here then do following.
       print val};                                                 ##Print the value of variable val here.
  val=""                                                           ##Nullifying the variable val here.
}
pointer==NF         {                                              ##checking condition if pointer value is same as NF then do following.
  print}                                                           ##Print the current line then, seems whole line is having zeros in it.
{
  pointer=""                                                       ##Nullifying the value of pointer here.
}
' Input_file                                                       ##Mentioning Input_file name here.

关于bash - 使用 sed/awk 将 x^2 添加到每个 "nonzero"系数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49702365/

相关文章:

linux - 在 shell 中获取文件大小(以字节为单位)的可移植方法

Bash 使用超时将输出写入文件会导致错误

ubuntu - 在 Ubuntu 上为 mingw 安装 ncurses

c++ - FFMPEG:未定义对 `avcodec_register_all' 的引用未链接

mysql - 多个主机名和多个权限?

regex - 将 bash 正则表达式模式作为 awk 中更大的正则表达式模式的一部分传递

bash - 使用 bash 的变量中的大写第一个字符

Linux:仅对目录设置权限

sorting - 使用 grep 的结果对文件进行排序

bash - 使用 join 对日志文件进行排序