我有一个名为deps的变量:
deps='word1 word2'
我想为变量的每个单词添加一个前缀。
我尝试过:
echo $deps | while read word do \ echo "prefix-$word" \ done
但我明白了:
bash:意外令牌“完成”附近的语法错误
任何帮助?感谢
答案 0 :(得分:15)
使用sed:
Dim mRow As Integer = 0
Dim newpage As Boolean = True
Private Sub PrintDocument1_PrintPage(ByVal sender As System.Object, ByVal e As System.Drawing.Printing.PrintPageEventArgs) Handles PrintDocument1.PrintPage
Dim custCells As Integer() = {1, 2, 3, 4, 5}
With DataGridView1
Dim fmt As StringFormat = New StringFormat(StringFormatFlags.LineLimit)
fmt.LineAlignment = StringAlignment.Center
fmt.Trimming = StringTrimming.EllipsisCharacter
Dim y As Single = e.MarginBounds.Top
Do While mRow < .RowCount
Dim row As DataGridViewRow = .Rows(mRow)
Dim x As Single = e.MarginBounds.Left
Dim h As Single = 0
For Each cell As Integer In custCells
Dim rc As RectangleF = New RectangleF(x, y, row.Cells(cell).Size.Width, row.Cells(cell).Size.Height)
e.Graphics.DrawRectangle(Pens.Black, rc.Left, rc.Top, rc.Width, rc.Height)
If (newpage) Then
e.Graphics.DrawString(DataGridView1.Columns(cell).HeaderText, .Font, Brushes.Black, rc, fmt)
Else
e.Graphics.DrawString(DataGridView1.Rows(row.Cells(cell).RowIndex).Cells(cell).FormattedValue.ToString(), .Font, Brushes.Black, rc, fmt)
End If
x += rc.Width
h = Math.Max(h, rc.Height)
Next
newpage = False
y += h
mRow += 1
If y + h > e.MarginBounds.Bottom Then
e.HasMorePages = True
mRow -= 1
newpage = True
Exit Sub
End If
Loop
mRow = 0
End With
End Sub
答案 1 :(得分:5)
对于表现良好的字符串,最好的答案是:
templating:
engines: ['twig']
正如123对于fedorqui回答的评论所建议的那样。
说明:
printf "prefix-%s\n" $deps
$deps
(默认为$IFS
)拆分" \n\t"
的内容
printf
评估每个提供的参数的模式,并将输出写入stdout。printf
是内置的shell(至少对于bash而言)并且不会分叉另一个进程,因此这比基于sed的解决方案更快。答案 2 :(得分:2)
在another question中,我发现了单词的开头(\<
)和结尾(\>
)的标记。有了这些,您可以稍微缩短SLePort以上的解决方案。该解决方案也很好地扩展到附加后缀,我需要除了前缀之外,但是无法弄清楚如何使用上面的解决方案,因为&
还包括单词之后可能的尾随空格。
所以我的解决方案是:
$ deps='word1 word2'
# add prefix:
$ echo "$deps" | sed 's/\</prefix-/g'
prefix-word1 prefix-word2
# add suffix:
$ echo "$deps" | sed 's/\>/-suffix/g'
word1-suffix word2-suffix
说明: \<
匹配每个单词的开头,\>
匹配每个单词的结尾。您可以简单地用前缀/后缀“替换”它们,从而使它们被预先附加/附加。在替换中不再需要引用它们,因为它们不是“真正的”字符!
答案 3 :(得分:0)
您可以将字符串读入数组,然后将字符串添加到每个项目中:
$ IFS=' ' read -r -a myarray <<< "word1 word2"
$ printf "%s\n" "${myarray[@]}"
word1
word2
$ printf "prefix-%s\n" "${myarray[@]}"
prefix-word1
prefix-word2