文本文件有sample.txt
a/b/1
b/f/2
y/z/3
a/y/4
t/q/5
想要这样的输出
a/1
a/2
a/3
a/4
a/5
尝试过的代码
dir="a/"
num=$(wc -l < sample.txt)
for ((i=1; i<=$num; i++));
do
echo "$dir""`awk -F "/" '{print $NF}' sample.txt`"
echo "$i"
break
done
但这给了我输出
a/1
2
3
4
5
问题是使用 awk 它只更新第一行。请帮忙
最佳答案
将 IFS
设置为包含 '/'
的简单 while 循环同样有效,例如
while IFS="$IFS/" read -r a b c; do
[ -z "$pfx" ] && pfx=$a ## if prefix not set, set it with 1st read
echo "$pfx/$c"
done <tripfile.txt
示例输入
$ cat tripfile.txt
a/b/1
b/f/2
y/z/3
a/y/4
t/q/5
示例使用/输出
$ while IFS="$IFS/" read -r a b c; do [ -z "$pfx" ] && pfx=$a; \
echo "$pfx/$c"; done <tripfile.txt)
a/1
a/2
a/3
a/4
a/5
关于linux - 在 linux 文件中的每一行之前添加文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45790884/