如何在unix中分别打印字符串的每个单词并获取位置
How to print each word of string in unix separately and get the position
我如何比较给定字符串的每个单词的值
示例字符串
i am over here
i am programmer
(这一行必须在第一行 i 之后开始)
要求:
需要检查第一个字符串从哪个列(位置)开始,并且必须确保第二行必须从同一列(位置)开始。
像这样检查第一个非空白:
lead=$(head -n1 file | grep -o '^ *')
col=$(echo $lead | wc -c)
((col--))
echo "column: $col"
现在删除第二行的第一个空格并添加适当数量的空格。
sed -i "2s/^ */$lead/" file
示例:
$ cat file
haha
some test
$ bash script.sh
colummn: 6
$ cat file
haha
some test
此处script.sh包含以上几行代码。
我如何比较给定字符串的每个单词的值
示例字符串
i am over here
i am programmer
(这一行必须在第一行 i 之后开始)
要求: 需要检查第一个字符串从哪个列(位置)开始,并且必须确保第二行必须从同一列(位置)开始。
像这样检查第一个非空白:
lead=$(head -n1 file | grep -o '^ *')
col=$(echo $lead | wc -c)
((col--))
echo "column: $col"
现在删除第二行的第一个空格并添加适当数量的空格。
sed -i "2s/^ */$lead/" file
示例:
$ cat file
haha
some test
$ bash script.sh
colummn: 6
$ cat file
haha
some test
此处script.sh包含以上几行代码。