在.bash中的索引处插入换行符

时间:2017-01-19 16:24:32

标签: json bash

我正在上大学的一个入门课程,正在研究一个使用curl从API中抓取的json-object的一个MotD脚本。

我想绝对肯定你明白这是一项任务,但是我正在玩的东西是为了更多地了解如何用bash编写脚本。

我发现自己陷入了可能是一个非常简单的问题;如果我的json-object的'quote'值太长(在本例中为索引80),我想在特定索引上插入一个新行('-Dconfig.file')。

我一直在关注一堆SO线程,这是我目前的解决方案:

-Dconfig.resource=application_prod.conf

我从\n收到的当前输出是引用的第一个单词,而如果我在尝试进行字符串操作之前有一个#!/bin/bash json_object=$(curl -s 'http://quotes.stormconsultancy.co.uk/random.json') quote=$(echo ${json_object} | jq .quote | sed -e 's/^"//' -e 's/"$//') author=$(echo ${json_object} | jq .author) count=${#quote} echo $quote echo $author echo "wc: $count" if((count > 80)); then quote=${quote:0:80}\n${quote:80:(count - 80)} else echo "lower" fi printf "$quote" ,我会得到整个引用。

如果没有遵循最佳做法或任何事情,我很抱歉,但我绝对是初学者同时使用printfecho

我对任何建议都很满意。 :)

修改

示例输出:

  

$ ./json.bash

     

您应该使用与第一个孩子相同的护理来命名变量。

     

“James O. Coplien”

     

86

     更高

     

您应该使用与命名第一胎nchild相同的谨慎来命名变量。

2 个答案:

答案 0 :(得分:2)

您可以使用单行bash命令来实现此目的,

string="You should name a variable using the same care with which you name a first-born child."
(( "${#string}" > 80 )) && printf "%s\n" "${string:0:80}"$'\n'"${string:80}" || printf "%s\n" "$string"
You should name a variable using the same care with which you name a first-born 
child.

(和)小于80字符的输入行

string="You should name a variable using the same care"
(( "${#string}" > 80 )) && printf "%s\n" "${string:0:80}"$'\n'"${string:80}" || printf "%s\n" "$string"
You should name a variable using the same care

解释,

(( "${#string}" > 80 )) && printf "%s\n" "${string:0:80}"$'\n'"${string:80}" || printf "%s\n" "$string"

# The syntax is a indirect implementation of ternary operator as bash doesn't 
# directly support it.
#
# (( "${#string}" > 80 )) will return a success/fail depending upon the length
# of the string variable and if it is greater than 80, the command after && is
# executed and if it fails the command after || is executed
#
# "${string:0:80}"$'\n'"${string:80}"
# A parameter expansion syntax for sub-string extraction.
#
# ${PARAMETER:OFFSET}
#
# ${PARAMETER:OFFSET:LENGTH}
#
# This one can expand only a part of a parameter's value, given a position 
# to start and maybe a length. If LENGTH is omitted, the parameter will be 
# expanded up to the end of the string. If LENGTH is negative, it's taken as
# a second offset into the string, counting from the end of the string.
#
# So in our example we basically extract the characters from position 0 to 80
# insert a new-line and append the rest of the string 
#
# The $'\n' syntax allows to include all escape sequence characters be 
# included, in this case just the new line character.

答案 1 :(得分:0)

不是真的在原来的问题中,而是在@Inian中添加一些额外的代码,以避免在一个单词的中间打破,而是在${string:0:80}的最后一个空格处:

#!/usr/bin/env bash

string="You should really name a variable using the same care with which you name a first-born child."
if (( "${#string}" > 80 )); then
    maxstring="${string:0:80}"
    lastspace="${maxstring##*\ }"
    breakat="$((${#maxstring} - ${#lastspace}))"
    printf "%s\n" $"${string:0:${breakat}}"$'\n'"${string:${breakat}}"
else
    printf "%s\n" "$string"
fi

<强> maxstring=${string:0:80}

让我们得到报价的前80个字符。

<强> lastspace=${maxstring##*\ }

*\的前面删除$maxstring的最长匹配(空格已转义),${lastspace}将是从最后一个空格到字符串结尾的剩余字符串。

<强> breakat="$((${#maxstring} - ${#lastspace}))"

使用${lastspace}的长度减去${maxstring}的长度,以从${maxstring}获取空白的最后一个索引。这是将插入\n的索引。

示例输出&#34; hard&#34;打破角色80:

You should really name a variable using the same care with which you name a firs
t-born child.

带&#34; soft&#34;的示例输出在角色80的最近的空白处打破:

You should really name a variable using the same care with which you name a 
first-born child.