我尝试将文本文件的basename
作为新行插入到整个文件目录的同一文件中(所有文件都有不同的basenames
)。所以文件" frog.txt "内容:
asdf
wef
sdf
...变为:
frog
asdf
wef
sdf
答案 0 :(得分:0)
你可以试试这个bash one-liner:
for file in "sample1.txt" "sample2.txt"; do sed -i "1i${file%%.*}" "$file"; done
这将遍历每个文件名。 ${file%%.*}
将从$file
移除扩展程序。然后sed -i '1...'
将插入文件名(不带扩展名)作为第一行。
答案 1 :(得分:0)
在包含文件的目录中尝试:
for file in `ls`
do
# echo filename without extension to filename.tmp
# because you wrote .txt ${file:0:-4} removes 4 characters from the end
echo ${file:0:-4} >> $file".tmp"
# set IFS to split lines
IFS=$(echo -en "\n\b")
# echo line by line to filename.tmp
for line in `cat $file`
do
echo $line >> $file".tmp"
done
# rename/move filenname.tmp to filename
mv $file".tmp" $file
done
# set IFS to normal
unset IFS