This should work. But this will not print the message if the name is not found or the string doesn't match. Let me know if you need to print that msg
#!/bin/bash
read -p "first name:" nme
echo $nme
awk -F":" -v nme=$nme '$1 ~ nme && $3 ~ /two/{print}' listfile.txt | sed 's/:two:.*$/ one::/g'
devtakh:
This should work. But this will not print the message if the name is not found or the string doesn't match. Let me know if you need to print that msg
#!/bin/bash
read -p "first name:" nme
echo $nme
awk -F":" -v nme=$nme '$1 ~ nme && $3 ~ /two/{print}' listfile.txt | sed 's/:two:.*$/ one::/g'
Yes I want to make changes inside the file
Try this: This will make changes to the file.
#!/bin/bash
read -p "first name:" nme
echo $nme
if grep -q -e "$nme:.*:two:" listfile.txt
then
echo "The name $nme exists in the file and will be making changes now"
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one::/g" listfile.txt > tmp.$$
mv tmp.$$ listfile.txt
else
echo "the name $nme is not there in the file"
fi
devtakh:
Try this: This will make changes to the file.
#!/bin/bash
read -p "first name:" nme
echo $nme
if grep -q -e "$nme:.*:two:" listfile.txt
then
echo "The name $nme exists in the file and will be making changes now"
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one::/g" listfile.txt > tmp.$$
mv tmp.$$ listfile.txt
else
echo "the name $nme is not there in the file"
fi
Can you please explain the "sed" part? i mean that regular exppression
First, does the solution work for you?
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one::/g"
it matches:
$nme:
any character but not : and end with a : two: any other charaters I had grouped the reg expression with ( ) escaped by using a backslash to group them
Then I have used those patters in the replacement section.
\1 identifies the first pattern matched group
\2 the second.
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one::/g"
I found out how to do reverse..Now my question is
Is it possible to run unix commads with this or not
for example the output of sed is going to be
firstname:lastname: one::
if i want to add date commmand inside the colonss
firstnem: lastname: one: (date commad)
Yes, you just need to put
to insert the current date
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one:$(date '+%Y%m%d')/g"
devtakh:
Yes, you just need to put
to insert the current date
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one:$(date '+%Y%m%d')/g"
Yes, I figured that out, but I want the format to be like 04/12/2010
I tried this but this is not working like I want.
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one:$(date '+%m/%d/%y')/g"
so i just put $(date)
sed -e "s/\($nme:\)\([^:]*:\)two:.*/\1\2 one:$(date '+%m/%d/%Y')/g"