-2

UNIX: How to add a "DIRECTORY" each line of a text file using sed? At this beginning of the line if possible? Thanks... (Using Unix / Linux Command tool)

I have been using foreach loop and sed together, and what is now missing is just a directory each line which I am still trying to figure out... Thanks for any kind of help.

1 Answers1

2

If you are looking just to add the word "DIRECTORY" then the following should be fine:

sed 's/^/DIRECTORY /' inputfile

If you are looking to add a path then I would recommend to change the delimiter.

sed 's,^,'$PWD' ,' inputfile
jaypal singh
  • 74,723
  • 23
  • 102
  • 147
  • Thank you so much Sir Jaypal. I will apply this right away. I was looking for that added path.. Thanks. I'll update this if its ok. :) – Digimon Digitizer 2 Jun 03 '13 at 02:04
  • Sir Jay, the output was great, it printed the directory, now I have a new problm, it actually printed the folder which logically is correct but how if I am using foreach i (./*/filedump.txt.gz*) foreach? zcat $i | sed 's,^,'$PWD',' It outputs still the folder where you are, how if I wanted to print the folder of the output content of the file? If I confuse you its like this: I am @ /dummy/ and inside that folder I am file1 & file2 folders with diffrent txt files. So while foreach is providing line outputs of a file I wish I could print the directory of the file its printing. – Digimon Digitizer 2 Jun 03 '13 at 02:20
  • $PWD was just an example. For your loop use `${i%.*}` and you should get path of your file prefixed. – jaypal singh Jun 03 '13 at 02:32
  • Thank you very much Sir Jaypal! :) I appreciated it so much, you are a life saver thanks! :) – Digimon Digitizer 2 Jun 03 '13 at 02:41
  • You're welcome @MegaManX. Just Jaypal is fine. :) This site is excellent when you post your question in detail along with what have you tried to to solve the problem. Good luck. – jaypal singh Jun 03 '13 at 02:46
  • (MASTER JAYPAL). I tried **zcat $i | sed '/s/^/'${i%.*}', '** and the output is **missing }**... :( Have I done something wrong? And thanks for the advice. :) I'll keep that in mind. – Digimon Digitizer 2 Jun 03 '13 at 03:17
  • Post your complete one-line with the for loop. – jaypal singh Jun 03 '13 at 03:20
  • **foreach i (./*/*dumpfiles*)** foreach? **zcat $i | sed '/s/^/'${i%.*}', '** foreach? **end** – Digimon Digitizer 2 Jun 03 '13 at 03:23
  • Jaypal, @Jaypal This is the code, sorry I cant use next line on this page maybe because of the **bold** – Digimon Digitizer 2 Jun 03 '13 at 03:32
  • Yeah its really weird I even tried without the bracket **zcat $i | sed '/s/^/'$i%.*','** and it say no sed: no match many times... Master, can I see your sample code? Maybe I just have a typo stuff... :( – Digimon Digitizer 2 Jun 03 '13 at 05:30