我想寻求帮助。
我当前正在创建一个脚本,该脚本将具有一个充满格式文本的设置文件作为基础,然后根据用户输入输入该文本文件并从该文本文件中删除。我会将其放入bash脚本中。
我想出了如何使用以下方法从文件中提取所需的文本:
sed -n '/5.1.1"/,/|}/p' textfile.txt
5.1.1是服务器的机架位置,我正在抓取文件的文本,直到找到|}。
这将输出以下内容:
|+ description = "5.1.1";
|-
|1: ~servername~ [4U];
|-
|5: servername [2U];
|-
|7: servername [2U];
|-
|9: servername [2U];
|-
|11: servername [2U];
|-
|15: servername [2U];
|-
|17: servername [2U];
|-
|19: servername [2U];
|-
|21: ~servername~ [2U];
|-
|23: servername [2U];
|-
|25: servername [2U];
|-
|29: servername [2U];
|-
|31: servername [2U];
|-
|33: servername [2U];
|-
|35: ~servername~ [2U];
|-
|37: ~servername";
|-
|39: servername;
|-
|41: switch;
|}
依此类推,直到5.1.15。
sed只会选择我需要更改的文本。
我想如何做到这一点的方法是将我用sed抓取的文本,插入所需的行,然后根据第二列中的数字对其进行排序,然后将更改后的文本重新插入文件中。相同的位置,但是我不知道如何实现。
我要输入的行与其他行的格式相同,然后在其下方显示一个|-。
非常感谢您
编辑1:
因此,期望的输出将是:
我有16行:服务器名;我想在5.1.1机架中输入此文件。
从上面获得基准的最终结果将是:
|+ description = "5.1.1";
|-
|1: ~servername~ [4U];
|-
|5: servername [2U];
|-
|7: servername [2U];
|-
|9: servername [2U];
|-
|11: servername [2U];
|-
|15: servername [2U];
|-
|16: servername;
|-
|17: servername [2U];
|-
|19: servername [2U];
|-
|21: ~servername~ [2U];
|-
|23: servername [2U];
|-
|25: servername [2U];
|-
|29: servername [2U];
|-
|31: servername [2U];
|-
|33: servername [2U];
|-
|35: ~servername~ [2U];
|-
|37: ~servername";
|-
|39: servername;
|-
|41: switch;
|}
编辑2:
我认为我应该包括为格式化而编写的脚本。它是不完整的,而且很简陋,但是除了$ location变量的if之外,它还可以解决问题。我需要为此修改正则表达式。
#! /bin/bash
function serverread {
#Enter the location
function locationread {
echo 'Enter the location of the server in the data center in format (x.x.x) ex. 5.1.15'
read location
if [[ "$location" =~ [0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2} ]]
then
echo "Success"
else
echo "You have entered an incorrect format for the rack, please try again."
locationread
exit
fi
}
locationread
echo Please enter the rack space at which the server was placed
read rp
if (( $rp < 1 || $rp > 42));
then
echo You have entered an incorrect size for the server. Please enter a number between 1 and 42.
read rp
else
{
echo You entered: $rp as the server placement in the rack.
}
fi
#Enter the server name
echo Now please enter the server name "(Use ~ around the name if it is disconnected/unplugged)":
read name
if [[ $name == ~* ]] && [[ $name == *~ ]];
then
echo "You entered: $name as the server name and it is currently disconnected in some way."
else
echo "You entered: $name as the server name."
fi
#Enter the physical height of the server
echo Now please enter the size in height of the server:
function readsize {
read size
if [ $size -eq $size 2>/dev/null ]
then
if (( $size < 1 || $size > 42));
then
echo You have entered an incorrect size for the server. Please enter a number between 1 and 42.
readsize
else
echo "You entered: $size as the physical height of the server"
fi
fi
}
readsize
#if (( $size == 1))
#
# else
# echo "$size is not an integer"
# fi
#
#if (( $size < 1 || $size > 42));
#then
# echo You have entered an incorrect size for the server. Please enter a number between 1 and 42.
# read size
#else
# echo "You entered: $size as the physical height of the server"
#fi
if (( $size == 1));
then
echo Currently this is the line that will be entered: $rp": "$name";"
else
echo Currently this is the line that will be entered: $rp": "$name" ["$size"U];"
fi
echo Is this correct? '[yes/no]'
read matrix
echo Would you like to add another server? '[yes/no]'
read matrix2
if (( $matrix == "yes" ));
then
if (( $matrix2 == "yes" ));
then
serverread
else
echo This is complete
exit
fi
else
echo Please run this command again
exit
fi
}
serverread
编辑3:
这是我更改的最后一个函数以使其适合脚本:
set finale="($rp": "$name" ["$size"U];""
echo Is this correct? '[yes/no]'
read matrix
echo Would you like to add another server? '[yes/no]'
read matrix2
if (( $matrix == "yes" ));
then
{
# all text before 5.1.1
sed -n '/'"$location"'"/q;p;' racktext.txt;
# parse text in this section
sed -n '/'"$location"'"/,/|}/p' racktext.txt | input.sh
# all text below this section
sed -n '/'"$location"'"/{ :a; /|}/{ n; :b; p; n; bb; }; n; ba; };' racktext.txt
# use sponge to overwrite the file in a pipeline
} | sponge racktext.txt
if (( $matrix2 == "yes" ));
then
serverread
else
echo This is complete
exit
fi
else
echo Please run this command again
exit
fi
}
serverread
这就是我要执行的脚本,该脚本是在单独的文件中显示的:
#! /bin/bash
export $(cat rackformat | grep -v ^# | xargs)
echo $finale
while IFS= read -r l; do
if [[ "$l" =~ description ]]; then
msg="$l"$'\n';
elif [[ "$l" =~ "}" ]]; then
{
echo -n "$msg" | grep -v '|-';
echo "\|$finale";
} | sed 's/^|//' | sort -n | sed 's/^/|/' | while IFS= read -r l; do
echo "$l";
echo '|-';
done | head -n-1;
echo '|}';
else
msg+="$l"$'\n';
fi;
done
编辑4:
我没有包括上述部分,因为我认为这没有意义,但是我可能应该将其包括在内,这是我的缺点。这是输入之前的文本文件:
{| class="wikitable"
|
{| class="wikitable"
|+ description = "5.1.1";
|-
|1: ~servername~ [4U];
|-
|5: servername [2U];
|-
|7: servername [2U];
|-
|9: servername [2U];
|-
|11: servername [2U];
|-
|15: servername [2U];
|-
|17: servername [2U];
|-
|19: servername [2U];
|-
|21: ~servername~ [2U];
|-
|23: servername [2U];
|-
|25: servername [2U];
|-
|29: servername [2U];
|-
|31: servername [2U];
|-
|33: servername [2U];
|-
|35: ~servername~ [2U];
|-
|37: ~servername~;
|-
|39: servername;
|-
|41: servername;
|}
|
{| class="wikitable"
这是当前脚本的输出:
{| class="wikitable"
|
{| class="wikitable"
|\|2.: test [2U];
|-
|+ description = "5.1.1";
|-
|1: ~servername~ [4U];
|-
|5: servername [2U];
|-
|7: servername [2U];
|-
|9: servername [2U];
|-
|11: servername [2U];
|-
|15: servername [2U];
|-
|17: servername [2U];
|-
|19: servername [2U];
|-
|21: ~servername~ [2U];
|-
|23: servername [2U];
|-
|25: servername [2U];
|-
|29: servername [2U];
|-
|31: servername [2U];
|-
|33: servername [2U];
|-
|35: ~servername~ [2U];
|-
|37: ~servername~;
|-
|39: servername;
|-
|41: servername;
|}
|
{| class="wikitable"
这是当前脚本的全部:
#! /bin/bash
function serverread {
#Enter the location
function locationread {
echo 'Enter the location of the server in the data center in format (x.x.x) ex. 5.1.15'
read location
if [[ "$location" =~ [0-9]{1,2}.[0-9]{1,2}.[0-9]{1,2} ]]
then
echo "Success"
else
echo "You have entered an incorrect format for the rack, please try again."
locationread
exit
fi
}
locationread
function parser {
while IFS= read -r l; do
if [[ "$l" =~ description ]]; then
msg="$l"$'\n';
elif [[ "$l" =~ "}" ]]; then
{
echo -n "$msg" | grep -v '|-';
echo "\|$finale";
} | sed 's/^|//' | sort -n | sed 's/^/|/' | while IFS= read -r l; do
echo "$l";
echo '|-';
done | head -n-1;
echo '|}';
else
msg+="$l"$'\n';
fi;
done
}
echo Please enter the rack space at which the server was placed
read rp
if (( $rp < 1 || $rp > 42));
then
echo You have entered an incorrect size for the server. Please enter a number between 1 and 42.
read rp
else
{
echo You entered: $rp as the server placement in the rack.
}
fi
#Enter the server name
echo Now please enter the server name "(Use ~ around the name if it is disconnected/unplugged)":
read name
if [[ $name == ~* ]] && [[ $name == *~ ]];
then
echo "You entered: $name as the server name and it is currently disconnected in some way."
else
echo "You entered: $name as the server name."
fi
#Enter the physical height of the server
echo Now please enter the size in height of the server:
function readsize {
read size
if [ $size -eq $size 2>/dev/null ]
then
if (( $size < 1 || $size > 42));
then
echo You have entered an incorrect size for the server. Please enter a number between 1 and 42.
readsize
else
echo "You entered: $size as the physical height of the server"
fi
fi
}
readsize
#if (( $size == 1))
#
# else
# echo "$size is not an integer"
# fi
#
#if (( $size < 1 || $size > 42));
#then
# echo You have entered an incorrect size for the server. Please enter a number between 1 and 42.
# read size
#else
# echo "You entered: $size as the physical height of the server"
#fi
if (( $size == 1));
then
echo Currently this is the line that will be entered: $rp": "$name";"
else
echo Currently this is the line that will be entered: $rp": "$name" ["$size"U];"
fi
echo Is this correct? '[yes/no]'
read matrix
echo Would you like to add another server? '[yes/no]'
read matrix2
finale=$rp": "$name" ["$size"U];"
echo $finale
if (( $matrix == "yes" ));
then
function b {
# all text before 5.1.1
sed -n '/'"$location"'"/q;p;' racktext.txt;
# parse text in this section
sed -n '/'"$location"'"/,/|}/p' racktext.txt | parser
# all text below this section
sed -n '/'"$location"'"/{ :a; /|}/{ n; :b; p; n; bb; }; n; ba; };' racktext.txt
# use sponge to overwrite the file in a pipeline
}
b | sponge racktext.txt
echo $
if (( $matrix2 == "yes" ));
then
serverread
else
echo This is complete
fi
else
echo Please run this command again
exit
fi
}
serverread
答案 0 :(得分:0)
以下script.sh
适用于在您提供的stdin上传递的示例:
#!/bin/bash
while IFS= read -r l; do
if [[ "$l" =~ description ]]; then
msg="$l"$'\n';
elif [[ "$l" =~ "}" ]]; then
{
echo -n "$msg" | grep -v '|-';
echo '|16: servername;';
} | sed 's/^|//' | sort -n | sed 's/^/|/' | while IFS= read -r l; do
echo "$l";
echo '|-';
done | head -n-1;
echo '|}';
else
msg+="$l"$'\n';
fi;
done
对于以下输入:
|+ description = "5.1.1";
|-
|1: ~servername~ [4U];
|-
|5: servername [2U];
|-
|7: servername [2U];
|-
|9: servername [2U];
|-
|11: servername [2U];
|-
|15: servername [2U];
|-
|17: servername [2U];
|-
|19: servername [2U];
|-
|21: ~servername~ [2U];
|-
|23: servername [2U];
|-
|25: servername [2U];
|-
|29: servername [2U];
|-
|31: servername [2U];
|-
|33: servername [2U];
|-
|35: ~servername~ [2U];
|-
|37: ~servername";
|-
|39: servername;
|-
|41: switch;
|}
它输出以下内容:
|+ description = "5.1.1";
|-
|1: ~servername~ [4U];
|-
|5: servername [2U];
|-
|7: servername [2U];
|-
|9: servername [2U];
|-
|11: servername [2U];
|-
|15: servername [2U];
|-
|16: servername;
|-
|17: servername [2U];
|-
|19: servername [2U];
|-
|21: ~servername~ [2U];
|-
|23: servername [2U];
|-
|25: servername [2U];
|-
|29: servername [2U];
|-
|31: servername [2U];
|-
|33: servername [2U];
|-
|35: ~servername~ [2U];
|-
|37: ~servername";
|-
|39: servername;
|-
|41: switch;
|}
由于您没有提供此文件格式的描述,也没有提供其他示例,所以我假设:
|-
行可以忽略不计,不会消失(起初我以为那是空的破布,但后来在15到16到17之间,所以不是)|}
结尾要将其插入到脚本中,您可以:
{
# all text before 5.1.1
sed -n '/5.1.1"/q;p;' textfile.txt;
# parse text in this section
sed -n '/5.1.1"/,/|}/p' textfile.txt | script.sh
# all text below this section
sed -n '/5.1.1"/{ :a; /|}/{ n; :b; p; n; bb; }; n; ba; };' textfile.txt
# use sponge to overwrite the file in a pipeline
} | sponge textfile.txt