HPE Community read-only access December 15, 2018
This is a maintenance upgrade. You will be able to read articles and posts, but not post or reply.
Hours:
Dec 15, 4:00 am to 10:00 am UTC
Dec 14, 10:00 pm CST to Dec 15, 4:00 am CST
Dec 14, 8:00 pm PST to Dec 15, 2:00 am PST
System Administration
cancel
Showing results for 
Search instead for 
Did you mean: 

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
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.