Operating System - HP-UX
1753767 Members
5506 Online
108799 Solutions
New Discussion юеВ

Re: script to add an inputted variable in a line

 
Scott_14
Regular Advisor

script to add an inputted variable in a line

Hello:

I know there is a simple answer but I cant put my finger on it,
I simply want to take a file with the fields

NAME1:NAME2:NAME3::::NAME1

Ask the user for input and drop that input before the SECOND NAME1
to have
NAME1:NAME2:NAM3::USERINPUT:NAME1

I was thinking of sed тАУe but I need to drop it between the 2 colons
and not sure if I can?

Thanks

4 REPLIES 4
James R. Ferguson
Acclaimed Contributor

Re: script to add an inputted variable in a line

Hi Scott:

# echo "NAME1:NAME2:NAME3::::NAME1" | sed -e 's/::::/::USERINPUT:/'
NAME1:NAME2:NAME3::USERINPUT:NAME1

...matches your apparent requirement.

...but I suspect you want:

# USERINPUT=Scott
# echo "NAME1:NAME2:NAME3::::NAME1" | sed -e "s/::::/::$USERINPUT:/"
NAME1:NAME2:NAME3::Scott:NAME1

Regards!

...JRF...
Laurent Menase
Honored Contributor

Re: script to add an inputted variable in a line

#!/usr/bin/ksh

inputfile=$1
outputfile=$2
exec 3<$inputfile

while read -u3 a
do
echo $a : input?
read b
echo "${a%:*}$b${a##*:}"
done >$outputfile
Scott_14
Regular Advisor

Re: script to add an inputted variable in a line

THANKS...
I got it working, and I am "fine tuning" it more but your help got me over the : i was over thinking it...

I have it working with

read A
read B
grep -i $A filename | sed -e "s/:::/::$B:/"

gets me to to put the file as I need, and I am sure I can simply reverse it to back it out as needed....

Scott_14
Regular Advisor

Re: script to add an inputted variable in a line

Thanks as I have it working how I need.