简体   繁体   中英

Shell script handle string with sed

I have a text file, each line is one or more file paths separated with space, all the file has suffix dl , eg

/some/path/file.dl
/some/other/path/file2.dl /some/other/path2/file3.dl
/some/other/path3/file4.dl /some/other/path4/file5.dl ...
...

Now I need to transform the above file to another text file. Only the first file of every line should be changed to /out/P{fileName}.h: , {fileName} is the original file name without directory and suffix. eg

/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path2/file5.dl ...
...

So how can I write the linux shell script?

Try this command:

$ sed -r 's@^\S*/(\S*)\.dl@/out/P\1.h:@' input 
/out/Pfile.h:
/out/Pfile2.h: /some/other/path2/file3.dl
/out/Pfile4.h: /some/other/path4/file5.dl

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM