loop to replace txt and add command inside of colon (:)

in one of my script..I have some thing like

john:christina : one::
and i want to make it like 
john:chritina:two:(date command):jackey:

basically change 'one' to 'two' and run date :command and add other name:

Not sure what you are looking for:

echo "john:christina : one::" | awk '{sub(/ one:/,"two");print $0,time,":something"}' time=`date '+%Y%m%d'`

cheers,
Devaraj Takhellambam

thanks for reply sir
can you please explain what 'sub' is? and is print legal in unix?

Try this:

echo "john:christina : one::" | sed "s/one::/two:$(date '+%Y%m%d'):/"

First thing, I am not very sure if the above code serves your purpose.

sub means substitute. You should read man pages of awk. In the above code, awk tries to substitue the string "one" with "two" and then print the substituted text as well as the date o/p and some other text.

Print is very legal in unix. It basically prints what is instructed to print, it can either print on the console or a file.

#!/bin/bash
read -p "one name:" nme
read -p "second name" sn
file1="${nme}:${sn}:one::"
if grep -q "$file1" listfile
then
echo "$file1"| sed "s/one::/two:$(date '+%Y%m%d'):/"
else
echo "The book is checked out"
fi

I have something like this..it is not working it does not change the original format of listfile..what this is doing is just executing else statement.

same with your code...any suggestion?

Does your file "listfile" has the string contents you entered from the keyborad?

i had another script which asks the user for data and enter that data in the listfile and yes the file is .txt file. whatever user enter it goes to this txt file

Ok, can you show me a snippet of the contents of the file.

Apprarently, grep is not able to find the string in the file and hence your "else" part of the program is only executed.

to make sure , do a

echo $file1 

and see the o/p of it is there indeed in the file "listfile"

do you want to see how my file look like?

if yes this is the format of data stored in the file

jo:adam:one::
joseph ul: fernendez:one::

and so on

Can you show me the o/p of this script below:

#!/bin/bash 
read -p "one name:" nme 
read -p "second name" sn 
file1="${nme}:${sn}:one::" 
echo $file1
grep "$file1" listfile
if grep -q "$file1" listfile 
then 
echo "$file1"| sed "s/one::/two:$(date '+%Y%m%d'):/" 
else 
echo "The book is checked out" 
fi

sorry brother to ask again can you tell what is o/p ...dou mean output?

if yes

then output i get everytime is
The book is checked out

yes, o/p means output.

did you include the line i marked in RED above in the script and execute it?

yes, i did include those lines

Well, I think the problem is with your read keys from the keyboard

When you run the script, make sure that you enter the text when prompted. I dont see any other reason why it wouldn't work. Good luck

#!/bin/bash 

read -p "first name:" nme 
awk -F":" '/$nme/{print $1}' listfile.txt > otherfile
file1="${nme}"
if grep -q "$file1" otherfile 
then 
echo "$file1"| sed -i "s/two:$(date '+%Y%m%d')/one:::/" 
else 
echo "it is out" 
fi

now format of listfile is
firstname:lastname:two:date:

I want this to be change to:
firstname:lastname: one

can someone check problem in this script?

what is the value of the variable "title". where do you get that value from?

sorry it was $nme not $title.. I misprint here...

Take $nme instead of $title

As I mentioned, it will be easier for us to solve the problem, the need of the script rather than trying to fix a script without not the real background.

May be you can try something like this

#!/bin/bash

read -p "first name:" nme
echo $nme
awk -F":" -v nme=$nme '$0 ~ nme{print $1}' listfile.txt > otherfile
file1="${nme}"
if grep -q "$file1" otherfile
then
sed "s/two:$(date '+%Y%m%d')/one:::/" listfile.txt
else
echo "it is out"
fi