Find and replace

my input file

tan23
cos87
sin32
tat1&

I'm using the below command for find and replace on beginning and end of each line but due to ' symbol its throwing error.

Any awk singe liner to replace at beginning and end of each line?
And first line of the first field should be "root" and the following lines are "bin"
Using ksh.

awk '{sub(/^/, "root a.directory from '")};1' input.txt
sed -e 's/$/'/' input.txt 

Output:

root a.directory from 'tan23'
bin a.directory from 'cos87'
bin a.directory from 'sin32'
bin a.directory from 'tat1&'

Hello,

Could you please try this script.
Where find_replace.ksh is the script name. Also lets say we have all entries in the file named test_file121.

 
$ cat find_replace.ksh
 
zero=0
i=0
while read line
do
if [[ $i -eq ${zero} ]]
then
echo "root a.directory from '"$line"'"
else
echo "bin a.directory from " $line"'"
fi
let "i = i +1"
done < "test_file121"
 

Out put will be as follows.

$ ksh find_replace.ksh
root a.directory from 'tan23 '
bin a.directory from 'cos87'
bin a.directory from 'sin32'
bin a.directory from 'tat1& '
$

Thanks,
R. Singh

1 Like
awk 'NR==1 {print s1$0e} NR!=1 {print s2$0e}' s1="root a.directory from '" s2="bin a.directory from '" e="'" input.txt
root a.directory from 'tan23'
bin a.directory from 'cos87'
bin a.directory from 'sin32'
bin a.directory from 'tat1&'

Edit: some shorter variation

awk '{s=(NR==1)?s1:s2;print s" a.directory from "e$0e}' s1="root" s2="bin" e="'" input.txt
2 Likes

Jotna..Thanks :slight_smile:

awk '{print s1,"a.directory from",e $0 e; s1=s2}' s1="root" s2="bin" e="'" input.txt
1 Like