使用awk或sed更改路径 [英] Change path using awk or sed

查看:62
本文介绍了使用awk或sed更改路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我想将符号链接路径更改为txt文件中的真实路径.首先,我找到单词"SF:"并更改路径.现在,我使用此脚本,但是它很慢且无效.我认为可以通过awk或sed命令更改它.预先谢谢你.

Hello i want to change symlink path to real path in txt file. First I find word "SF:" and change path. Now i use this script but it is slow and not effective. I think it can be changed by awk or sed command. Thank you in advance.

#!/bin/bash
FILENAME="new1.info"
    echo "" > $FILENAME
while read LINE
do
if [ "" != "$(echo $LINE | grep -e "^SF:")" ]
then
    echo "SF:""$(realpath $(echo $LINE | cut -d":" -f2))" >> $FILENAME
else
    echo $LINE >> $FILENAME
fi
done < total.info

我有一个很大的txt文件,所以我想找到"SF:"并更改类似这样的行:

I have big txt file, so I want to find "SF:" and change line from something like this:

SF:/home/ects/svn/moduleTests/ctest/tests/moduletests/base/tmp/src/base64.cpp

对此:

SF:/home/ects/svn/moduleTests/ctest/src/base/base64.cpp

推荐答案

在bash中,我会写

#!/bin/bash
while IFS= read -r line; do
    if [[ "$line" == "SF:"* ]]; then
        line="SF:$(realpath "${line#SF:}")"
    fi
    echo "$line"
done < total.info > new1.info

注意事项:

  • 引用变量,每次.
  • 不要使用ALLCAPS变量名,它会最终咬住您
  • 请注意,输出重定向已移至while循环的外部,就像输入重定向一样
  • IFS =读取-r行是从文件中精确读取行的方法.
  • quote your variables, every time.
  • don't use ALLCAPS varnames, it will bite you eventually
  • note the output redirection has been moved to the outside of the while loop, just like the input redirection
  • IFS= read -r line is the way to read lines from the file exactly.

我不知道这样做是否会更快:bash可能会非常慢,尤其是在大文件读取时, 循环.您可以尝试使用另一种语言:

I don't know if this will be any faster: bash can be quite slow, particular for while read loops over big files. You could try another language:

perl -MCwd=abs_path -pe 's/^SF:\K(.*)/ abs_path($1) /e' total.info > new1.info

这篇关于使用awk或sed更改路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆