sed - How to confirm substitution


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sed - How to confirm substitution
# 1  
Old 03-21-2011
sed - How to confirm substitution

How to confirm whether a particular substitution taken place or not ?

Find the code below :-

Code:
#!/bin/ksh
COUNT=0
#######  Create backup of the original file  ######
cp passwd passwd_old
#######  Read the csv file for unixid and project ######
while IFS=, read unix_id project_id
do
  UNIX_ID=${unix_id}
  PROJECT_ID=${project_id}
############ replace UNIX_ID with PROJECT_ID ##############
  print "converting $UNIX_ID to $X_ID .........................." >>log
  sed "s/$UNIX_ID/$PROJECT_ID/g" passwd >> passwd-new
  cp passwd-new passwd
  rm passwd-new
done < ids.csv

Now what I need is to detect whether a particular unix_id exists or better to confirm whether a particular substitution from unix_di to project_id taken place or not so that error logs and other logs can be generated efficiently

Last edited by hiten.r.chauhan; 03-21-2011 at 08:11 AM.. Reason: error in typing
# 2  
Old 03-21-2011
In this script you define lower case variables in the read statement and then define the same in upper case with the lower case's value. You could just write those in the read statement in upper case and save two redundant lines.

awk's sub() function can be assigned to a variable. It will be 1 on a succesful substitution, 0 on a fail.

If I got the logic right, you could just tell the script straight forward to replace the 1st field (UNIX_ID) with the second. It will always work so it would not help to know if the substitution worked or not, as the way you do it, it is forcing to overwrite it anyway, no matter if the UNIX_ID matches or not. It will always match as you wrote it. I am not sure if that is, what you wanted.

If your ids.csv looks like...
Code:
john,sales
martha,sales
ron,control
herbert,management

... your output would be always:
Code:
sales,sales
sales,sales
control,control
management,management

So either you want this and this can be done much easier with much less lines or there is something different logic behind it.

If there is really passwords involved in this, saving them in plain text to a file is really a security issue. You might at least want to use something like md5 to store passwords encrypted.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed substitution

Hi everyone, I need very simple sed command to change a parameter in a text file. I have a line in this text which is like set xx 0.5 A program reads this file and does some algebraic calculations. So to make a parameter scan I need to change the value of xx. I thought I can do... (7 Replies)
Discussion started by: hayreter
7 Replies

2. UNIX for Dummies Questions & Answers

sed substitution

How can you use sed with a line of code that reads: 67899:Bill:Williams:Maple Dr.:45908600 Let us say we want to replace Maple Dr. with Oak St. (1 Reply)
Discussion started by: yonkers062986
1 Replies

3. Shell Programming and Scripting

sed substitution

Hello, I have two files. File1 is normal txt file and File2 contains list of line numbers. e.g. File2: 3 6 9 ..... I need to replace a character in File1 in lines (taken from File2). For that I am using a "for" loop: for i in $(cat File2) do sed "$i s/Y/N/" File1 done but my... (3 Replies)
Discussion started by: shekhar2010us
3 Replies

4. Shell Programming and Scripting

Substitution with sed

I have a file with some numbers having single quotes around them which I want to remove. i.e. '923930' -> 23930 If it can be done without using sed thats fine. I have tried with sed but can't think how to replace this pattern on only the numbers (13 Replies)
Discussion started by: user_invalid
13 Replies

5. Shell Programming and Scripting

sed substitution

Using sed I'm trying to replace 'string' with ']' while retaining case and ignoring words with 'string' in it along with additional characters like 'strings' and those which already contain the ] wrapper. I'm hoping to do it with sed and the right expression, if possible. Example: Apple... (2 Replies)
Discussion started by: tom.lee
2 Replies

6. Shell Programming and Scripting

Help with sed/substitution!

I have file.txt 1 4 7 9 3 I want to replace the tabs with a space, but my code doesn't work. cat file.txt | sed 's/"\t"/ /g' > t.txt But file is still the same. Numbers seperated by tabs instead of spaces. Help? (2 Replies)
Discussion started by: Bandit390
2 Replies

7. Shell Programming and Scripting

SED Substitution

Hi guys, Can u please help me to replace (-) with (/) in a file containing no of records using "sed " command in unix. thanks in advance. subhendu (5 Replies)
Discussion started by: subhendu81
5 Replies

8. Shell Programming and Scripting

SED Substitution

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as well to... (1 Reply)
Discussion started by: shubhranshu
1 Replies

9. Shell Programming and Scripting

Substitution using SED

Hi , I am stuck up in the below scenario:- I need to read a file name (eg A.txt) name frm another file (eg B.txt) and then I need to search for a particular expression in A.txt and substitute it with another expression. How can I use SED inside SHELL Scripting and command prompt as... (2 Replies)
Discussion started by: shubhranshu
2 Replies

10. UNIX for Dummies Questions & Answers

sed substitution

Hi, I have a set of files containing strings like I.TEST1_TEST2 or B.ESSA_ESSB for example. Does somebody know how to substitute these strings whith the same name and an extension "_V1" (ie. I.TEST1_TEST2_V1) using sed command or else ? (3 Replies)
Discussion started by: jo_aze
3 Replies
Login or Register to Ask a Question