0

I am using sed in bash shell script. I want to add some text, which is defined as shell variable, in the first place of each line using sed.

For example,

cat filename
1.cfg
2.cfg
...
100.cfg

In the script,

#!/bin/bash
currentd="/home/test/"
sed "/WHAT SHOUL I DO to add $currentd/" filename > filepath

I want to add the file path currentd in the first place of the each line in filename and get filepath file.

Jahid
  • 21,542
  • 10
  • 90
  • 108
user4914499
  • 344
  • 1
  • 3
  • 12

2 Answers2

3
sed "s|^|$currentd|" filename > filepath

This "replaces" the start of each line (^) with $currentd. This requires that $currentd not contain any pipes (|).

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
1

You can also do it using awk, as:

awk -v c=$currentd '{print c $0}' filename > filepath

-v is used to set awk variable c to bash variable currentd.

Rakholiya Jenish
  • 3,165
  • 18
  • 28