组合行时终端命令等效于PHP内爆

时间:2016-10-28 02:48:23

标签: bash awk terminal

我有几行,想要将该行分组为5,然后将其内嵌到MySQL $row = $wpdb->get_row( "SELECT * FROM my_table WHERE service = $package_id AND url LIKE '%$url%'", ARRAY_A );查询中。

我已经做到了这个

header("Content-type: image/jpeg");

//$add=$img_link;
$add="https://pixabay.com/static/uploads/photo/2015/10/01/21/39/background-image-967820_960_720.jpg";

//$string = $verse_ref;
$string = "There are glimpses of heaven to us in every act, or thought, or word, that raises us above ourselves.";
$fontSize = 20;

$border=10; // Change the value to adjust width
$im=ImageCreateFromJpeg($add);

$width=ImageSx($im);
$height=ImageSy($im);

$img_adj_width=$width+(2*$border);
$img_adj_height=$height+(15*$border);

$newimage=imagecreatetruecolor($img_adj_width,$img_adj_height);

$border_color = imagecolorallocate($newimage, 0, 0, 0);

imagefilledrectangle($newimage,0,0,$img_adj_width,$img_adj_height,$border_color);

imageCopyResized($newimage,$im,$border,$border,0,0,$width,$height,$width,$height);


$font_width = ImageFontWidth($font);
$font_height = ImageFontHeight($font);

$text_width = $font_width * strlen($string);
$position_center = ceil(($width - $text_width) / 2);

$text_height = $font_height;
$position_middle = ceil(($height - $text_height) / 2);

$color = imagecolorallocate($newimage, 255, 255, 255);

imagestring($newimage, $fontSize, $position_center, $position_middle, $string, $color);
ImageJpeg($newimage);

例如,我想要下面的这些行

IN ()

awk '{ printf "%s", $0; if (NR % 5 == 0) print ""; else printf " " }

如果我使用这个

1
2
3
4
5
6

如果所有行都不能被5整除,那么输出将在尾随行中有1,2,3,4,5 6

更新

以前的标题是awk而不是bash,但事实证明有比awk更简单的解决方案。我的目标是做这样的事情

awk '{ printf "%s", $0; if (NR % 5 == 0) print ""; else printf "," }

5 个答案:

答案 0 :(得分:5)

pr是此

的工具
$ seq 6 | pr -5ats,
1,2,3,4,5
6

$ seq 18 | pr -5ats, 
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16,17,18

答案 1 :(得分:2)

#!/usr/bin/awk -f
{
  a[NR] = $0
}
END {
  for (b in a) {
    printf a[b] (b % 5 && b != NR ? "," : RS)
  }
}

或者一个班轮:

awk '{a[NR]=$0} END {for (b in a) printf a[b] (b%5 && b!=NR ? "," : RS)}'

答案 2 :(得分:1)

你可以这样做:

awk 'NR%5==0{print s","$0; s=""; next} {if (length(s)>0){ s=s","$0 } else s=$0} END {print s}'

测试它:

$ seq 1 6 | awk 'NR%5==0{print s","$0; s=""; next} {if (length(s)>0){ s=s","$0 } else s=$0} END {print s}'
1,2,3,4,5
6
$ seq 1 12 | awk 'NR%5==0{print s","$0; s=""; next} {if (length(s)>0){ s=s","$0 } else s=$0} END {print s}'
1,2,3,4,5
6,7,8,9,10
11,12

答案 3 :(得分:0)

虽然不是单个进程awk命令,但您甚至可以使用pastesed组合:

$ cat file 
1
2
3
4
5
6
$ paste - - - - - -d , < file | sed 's/,\+$//'
1,2,3,4,5
6
$ seq 1 12 > file 
$ cat file 
1
2
3
4
5
6
7
8
9
10
11
12
$ paste - - - - - -d , < file | sed 's/,\+$//'
1,2,3,4,5
6,7,8,9,10
11,12

答案 4 :(得分:0)

seq 16 |awk '{printf NR%5 ? (NR % 5 ==1 ? $0 : ","$0) : ","$0"\n";}'
#output:
1,2,3,4,5
6,7,8,9,10
11,12,13,14,15
16