0

Earlier I created the below question, unfortunately it has been closed.
How to append the path in the given file list I made a mistake in that question, Now I have corrected it in this question. Due to that I have created a new post.

I'm looking for a bash/shell script approach.

My file list consists of different types of file names.

I want to append the path at the beginning of each line in the file list.

file_list.txt contents:

pkg/arc/PM/MW/aaa.ctf
bbb.lst
ccc.lst
pkg/arc/scr/mass/dim/config/hsc.c.in
pkg/arc/PM/MW/ddd.pkg
eee.lst

I want to append the specific path of each file in the file list. Following output should be updated on the same file_list.txt Example output:

d:est/build/rdb/pkg/arc/PM/MW/aaa.ctf
d:est/build/bbb.lst
d:est/build/ccc.lst
d:est/build/mass/dim/config/hsc.c.in
d:est/build/rdb/pkg/arc/PM/MW/ddd.pkg
d:est/build/eee.lst

My Code: I tried the following command to modify and update the path:

SCRDIR="d:est/build/"
PMDIR="d:est/build/rdb/pkg/arc/PM"
sed -i -e "s|^|${SCRDIR}|" file_list.txt
sed -i "s#.*scr#$SCRDIR#g" file_list.txt
sed -i "s#.*PM#$PMDIR#g" file_list.txt

By using above three command I can achieve the result. But is there any other way or approach to get the same output.

  • You should add your code and show as what you did and what error you got. If you don't add your code, this question will be close too. – Lety Jan 24 '22 at 09:34
  • Thanks for the information. I will add my code portion – Ashok Kumar Jan 24 '22 at 09:38
  • I think your question is a duplicate to https://stackoverflow.com/questions/13586349/how-can-i-prepend-a-string-to-the-beginning-of-each-line-in-a-file – user1934428 Jan 24 '22 at 11:18
  • @Lety the one which you mentioned is about adding some string at the beginning or middle by specifying number, which is not related to my question. – Ashok Kumar Jan 24 '22 at 15:54
  • @user1934428 it’s about adding prefix. My case not only adding prefix also replacing. If you check my expected output line 4 and the input line 4. – Ashok Kumar Jan 24 '22 at 15:56
  • According to which rule do you just prefix the line by `d:est/build/` or replace something? I wouldn't use `sed` here but loop over the lines and for each line decide, which transformation you want to apply. `sed` is better suitable to do the same operation to each line. – user1934428 Jan 25 '22 at 07:15
  • You can apply your rules in one sed command: sed -e "s|^|${SCRDIR}|;s|pkg/arc/PM|rdb/pkg/arc/PM|;s|pkg/arc/scr/||" this work because sed use pattern space where rule are applied and at the end the result is printed out. Hope this help – Lety Jan 25 '22 at 11:50

0 Answers0