在解压缩文件,输出线的特定字符串与修改另一个文件 [英] Extract specific strings from line in file and output to another file with modifications

查看:138
本文介绍了在解压缩文件,输出线的特定字符串与修改另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

新的Linux,并试图逃离这个做着艰辛的道路。我有一个包含查找命令的结果的文件(output.txt的)。例如首先从output.txt的三行:

  /home/user/temp/LT50150292009260GNC01/L5015029_02920090917_MTL.txt
/home/user/temp/LT50150292009276GNC01/L5015029_02920091003_MTL.txt
/home/user/temp/LT50150292009292GNC01/L5015029_02920091019_MTL.txt

我想用awk或sed的(或类似)来提取每行列出的路径和输出两个部分到一个新文件(run.txt),每行添加像这样的额外信息

  CD /家庭/用户/温度/ LT50150292009260GNC01; $ RUNLD L5015029_02920090917_MTL.txt
CD /家庭/用户/温度/ LT50150292009276GNC01; $ RUNLD L5015029_02920091003_MTL.txt
CD /家庭/用户/温度/ LT50150292009292GNC01; $ RUNLD L5015029_02920091019_MTL.txt

我猜这可能也涉及类似腰斩,但我不能让我的头缠着如何核算更改文件夹和文件名。

任何帮助将是非常美联社preciated。


解决方案

  SED的| ^ | CD |; S | / \\([^ /] * \\)$ |; $ RUNLD \\ 1 |' inputfile中>跑

它说:


  • 插入CD在该行的开头

  • 在过去的斜线和后会发生什么,替代品; $ RUNLD,而最后一部分(用括号捕获)

New to linux and trying to escape doing this the hard way. I have a file ("output.txt") that contains the results of a 'find' command. Example first three lines from "output.txt":

/home/user/temp/LT50150292009260GNC01/L5015029_02920090917_MTL.txt
/home/user/temp/LT50150292009276GNC01/L5015029_02920091003_MTL.txt
/home/user/temp/LT50150292009292GNC01/L5015029_02920091019_MTL.txt

I'd like to use awk or sed (or similar) to extract two parts from the path listed for each line, and output to a new file ("run.txt") with extra information added on each line like so:

cd /home/user/temp/LT50150292009260GNC01; $RUNLD L5015029_02920090917_MTL.txt
cd /home/user/temp/LT50150292009276GNC01; $RUNLD L5015029_02920091003_MTL.txt
cd /home/user/temp/LT50150292009292GNC01; $RUNLD L5015029_02920091019_MTL.txt

I'm guessing this might also involve something like "cut", but I can't get my head wrapped around how to account for changing folder and file names.

Any help would be much appreciated.

解决方案

sed 's|^|cd |; s|/\([^/]*\)$|; $RUNLD \1|' inputfile > run

It says:

  • insert "cd " at the beginning of the line
  • for the last slash and what comes after, substitute "; $RUNLD " and the final part (captured by the parentheses)

这篇关于在解压缩文件,输出线的特定字符串与修改另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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