add filename to beginning of file using find and sed

后端 未结 5 647
死守一世寂寞
死守一世寂寞 2021-01-04 23:51

using the following I add the file name to the front of each line and send the output to a single file.

ls | while read file; do sed -e \"s/^/$file/g\" $fil         


        
5条回答
  •  迷失自我
    2021-01-05 00:23

    this one works fine for me and it is simpler to use than Kent's answer
    NOTE: than the full pathname is inserted for that one

    find . -type f | xargs -r -t -i sed -r 's|^|'{}' |g' {}
    

    use this one instead to keep only the bare filename part

    find . -type f | xargs -r -t -i sed -r -e 's|^|'{}' |g' -e 's|^.+/||g' {}
    

    then if your are happy with stdout results you might add -i switch to the sed command to overwrite the files

    find . -type f | xargs -r -t -i sed -i -r -e 's|^|'{}' |g' -e 's|^.+/||g' {}
    

提交回复
热议问题