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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting loop to replace txt and add command inside of colon (:)
# 22  
Old 04-21-2010
Quote:
Originally Posted by devtakh
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


Code:
#!/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


Last edited by Learnerabc; 04-21-2010 at 07:01 AM..
# 23  
Old 04-21-2010
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

Code:
#!/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'

# 24  
Old 04-21-2010
Quote:
Originally Posted by 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

Code:
#!/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
# 25  
Old 04-21-2010
Try this: This will make changes to the file.

Code:
#!/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

# 26  
Old 04-21-2010
Quote:
Originally Posted by devtakh
Try this: This will make changes to the file.

Code:
#!/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
# 27  
Old 04-21-2010
First, does the solution work for you?
Code:
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.
# 28  
Old 04-21-2010
Code:
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)

Last edited by Learnerabc; 04-21-2010 at 07:38 PM.. Reason: it was showing smilies
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

CUT command not giving correct result inside loop

Hi, i have a source file and have 3 columns and separated by "|" .i want to split this 3 columns in different variable.When i am executing this values indivisually giving correct result but when the same execute inside a for loop,it's giving issues. Src file(jjj.txt) -------... (8 Replies)
Discussion started by: raju2016
8 Replies

2. Shell Programming and Scripting

How to replace colon in HH:MI:SS to hypen?

I have a file that contains timestamp in some of the rows in the file and the field separator is colon and some of the rows have numerical values, timestamp and characters with colon as the field separator We are looking to change colon in HH:MI:SS for the timestamp to hyphen “-“ leaving the field... (1 Reply)
Discussion started by: cumeh1624
1 Replies

3. Windows & DOS: Issues & Discussions

2 Questions: replace text in txt file, add text to end of txt file

so... Lets assume I have a text file. The text file contains multiple "#" symbols. I want to replace all thos "#"s with a STRING using DOS/Batch I want to add a certain TEXT to the end of each line. How can I do this WITHOUT aid of sed, grep or anything linux related ? (1 Reply)
Discussion started by: pasc
1 Replies

4. Shell Programming and Scripting

How to execute a command inside a while loop?

How do we execute a command inside a while loop? (7 Replies)
Discussion started by: Little
7 Replies

5. Shell Programming and Scripting

If else condition inside for loop of awk command in UNIX shell scripting

Hi , Please excuse me for opening a new thread i am unable to find out the syntax error in my if else condition inside for loop in awk command , my actual aim is to print formatted html td tag when if condition (True) having string as "failed", could anyone please advise what is the right... (2 Replies)
Discussion started by: karthikram
2 Replies

6. UNIX for Dummies Questions & Answers

Replace colon with blank space

Dear Gurus, I have a unix file with multiple colons on each row, and I would like to replace each colon with a blank space using the awk command. For example, I have the following data: Data: --------- A~000000000000518000~SLP:~99991231~20090701~00102.00~USD:~CS:~... (2 Replies)
Discussion started by: chumsky
2 Replies

7. Shell Programming and Scripting

how to use sqlplus command inside for loop

I tried this: for region in 'raj' 'kt' 'kol' 'krl' 'chn' 'dl' 'hr' 'bih' 'ap' do sqlplus -s huw$region/`echo $region`huw#321@huw$region<<! set serveroutput on begin select count(*) from tcd_preferred_cust_201109 end; / exit; done but the error shows like: Syntax error at line 4 :... (1 Reply)
Discussion started by: deepakprasad29@
1 Replies

8. Programming

PERL, search and replace inside foreach loop

Hello All, Im a Hardware engineer, I have written this script to automate my job. I got stuck in the following location. CODE: .. .. ... foreach $key(keys %arr_hash) { my ($loc,$ind,$add) = split /,/, $arr_hash{$key}; &create_verilog($key, $loc, $ind ,$add); } sub create_verilog{... (2 Replies)
Discussion started by: riyasnr007
2 Replies

9. Shell Programming and Scripting

C Shell - Command Inside a Loop

I have a command nested in some while loops to parse some data that looks something like this. while ($condition) while ($condition) ... gzcat /dir/$fileName.gz | grep $searchString > out_file end end On the first loop, the command is executed properly (and takes maybe 10... (3 Replies)
Discussion started by: hobbers
3 Replies

10. Shell Programming and Scripting

read command (input) inside the while loop

Hi, 'read' command is not working inside the while loop, How can I solve this? Rgds, Sharif. (2 Replies)
Discussion started by: sharif
2 Replies
Login or Register to Ask a Question