我写了以下命令
echo -en 'uno\ndue\n' | sed -E 's/^.*(uno|$)/\1/'
Run Code Online (Sandbox Code Playgroud)
期待以下输出
uno
Run Code Online (Sandbox Code Playgroud)
我的 GNU Sed 4.8 确实就是这种情况。
但是,我已经验证 BSD Sed 输出
Run Code Online (Sandbox Code Playgroud)
为什么会这样?
请查看下面的示例文件和所需的输出,以了解我在寻找什么。
可以通过shell脚本中的循环来完成,但是我在努力获得一个awk/ sed班轮。
SampleFile.txt
These are leaves.
These are branches.
These are greenery which gives
oxygen, provides control over temperature
and maintains cleans the air.
These are tigers
These are bears
and deer and squirrels and other animals.
These are something you want to kill
Which will see you killed in the end.
These are things you must to think to save your tomorrow.
Run Code Online (Sandbox Code Playgroud)
所需的输出
These are leaves.
These are branches.
These are greenery which gives oxygen, …Run Code Online (Sandbox Code Playgroud) 我有一个包含多个\ n实例的文件。
我想用实际的换行符替换它们,但是sed无法识别\ n。
我试过了
sed -r -e 's/\n/\n/'
sed -r -e 's/\\n/\n/'
sed -r -e 's/[\n]/\n/'
Run Code Online (Sandbox Code Playgroud)
以及其他多种转义方式。
sed是否能够识别文字\ n?如果是这样,怎么办?
是否有另一个程序可以读取将\ n解释为真实换行符的文件?
我是Unix shell脚本的新手,并试图获得shell脚本的一些知识.请检查我的要求和我的方法.
我有一个包含数据的输入文件
ABC = A:3 E:3 PS:6
PQR = B:5 S:5 AS:2 N:2
Run Code Online (Sandbox Code Playgroud)
我试图解析数据并得到结果
ABC
A=3
E=3
PS=6
PQR
B=5
S=5
AS=2
N=2
Run Code Online (Sandbox Code Playgroud)
可以水平和垂直添加值,因此我尝试使用数组.我正在尝试这样的事情:
myarr=(main.conf | awk -F"=" 'NR!=1 {print $1}'))
echo ${myarr[1]}
# Or loop through every element in the array
for i in "${myarr[@]}"
do
:
echo $i
done
Run Code Online (Sandbox Code Playgroud)
要么
awk -F"=" 'NR!=1 {
print $1"\n"
STR=$2
IFS=':' read -r -a array <<< "$STR"
for i in "${!array[@]}"
do
echo "$i=>${array[i]}"
done
}' main.conf
Run Code Online (Sandbox Code Playgroud)
但是,当我将此代码添加到.sh文件并尝试运行它时,我会收到语法错误 …
我有一个看起来像这样的文件(伪代码):
---
foo: bar
bar: baz
---
baz: quz
---
Some text
Some text
Some text
Run Code Online (Sandbox Code Playgroud)
我需要删除第二 ---行,只有那个.我知道sed可以做到这一点,但我从来没有能够sed找到我能找到的任何文件的头脑或尾巴......