Read line by line and replace string.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Read line by line and replace string.
# 1  
Old 10-02-2014
Read line by line and replace string.

Hi,

I currently have a problem that I need to read a file line by line.

After I read it line by line there are some commands in which I have to change a specific string.(In my case, I have to make a script that changes all the passwords into hash value)

Here is a sample input
Code:
hostName=TEST123
hostIpAddr=TEST123
hostUserName=test112
hostPassword=test146

HostName=11.111.111
HostIpAddr=12.12.121
UserName=UseR1
Password=UsEr1

I only tried to change the second occurence of Password with this code.

Code:
passNew=`grep 'Password' test.file | awk -F= '{print $2}' | openssl enc -aes-128-cbc -a -salt -pass pass:wtf`

passStr=`grep 'Password' test_dev.param | awk -F= '{print $2}'`

sed 13,/$passStr/{s@$passStr@$passNew@} test.file > test1.file

Any help I cant get so I can loop up? and convert every occurence of passwords to hash?

The sample output must be:
Code:
hostName=TEST123
hostIpAddr=TEST123
hostUserName=test112
hostPassword=JfsjeoOf3001=

HostName=11.111.111
HostIpAddr=12.12.121
UserName=UseR1
Password=Fkjdslkjqksnlkc491/+FDSJLK=

# 2  
Old 10-02-2014
This will change all the password that are the same in one execution:
Code:
OIFS="${IFS}"
IFS="="
awk '/^Password|^hostPassword/ {print $0}' test.file | sort | uniq | \
while read key value
do
        echo "Changing -> Key[${key}] Value[${value}]"
        newPassword=$(echo "${value}" | openssl enc -aes-128-cbc -a -salt -pass pass:wtf)
        searchValue="${key}=${value}"
        sed -i 's#^'"${searchValue}"'#'"${key}"'='"${newPassword}"'#g' test.file
done
IFS="${OIFS}"

This uses linux "sed -i", which changes the file "in place".

I hope it helps!
# 3  
Old 10-02-2014
Looks like you solved your issue from here bash - Read line by line and replace string - Stack Overflow
# 4  
Old 10-02-2014
SmilieSmilie
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

How to read file line by line and compare subset of 1st line with 2nd?

Hi all, I have a log file say Test.log that gets updated continuously and it has data in pipe separated format. A sample log file would look like: <date1>|<data1>|<url1>|<result1> <date2>|<data2>|<url2>|<result2> <date3>|<data3>|<url3>|<result3> <date4>|<data4>|<url4>|<result4> What I... (3 Replies)
Discussion started by: pat_pramod
3 Replies

2. Shell Programming and Scripting

Need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line...

Hello, I need a program that read a file line by line and prints out lines 1, 2 & 3 after an empty line... An example of entries in the file would be: SRVXPAPI001 ERRO JUN24 07:28:34 1775 REASON= 0000, PROCID= #E506 #1065: TPCIPPR, INDEX= 003F ... (8 Replies)
Discussion started by: Ferocci
8 Replies

3. Shell Programming and Scripting

Replace line in file with line in another file based on matching string

HI Can any one guide me how to achieve this task. I have 2 files env.txt #Configuration.Properties values identity_server_url = http://identity.test-hit.com:9783/identity/service/user/register randon_password_length = 6 attachment_file_path = /pass/temp/attachments/... (1 Reply)
Discussion started by: nikilbr86
1 Replies

4. Shell Programming and Scripting

Replace and add line in file with line in another file based on matching string

Hi, I want to achieve something similar to what described in another post: The difference is I want to add the line if the pattern is not found. File 1: A123, valueA, valueB B234, valueA, valueB C345, valueA, valueB D456, valueA, valueB E567, valueA, valueB F678, valueA, valueB ... (11 Replies)
Discussion started by: jyu3
11 Replies

5. Shell Programming and Scripting

how to read the contents of two files line by line and compare the line by line?

Hi All, I'm trying to figure out which are the trusted-ips and which are not using a script file.. I have a file named 'ip-list.txt' which contains some ip addresses and another file named 'trusted-ip-list.txt' which also contains some ip addresses. I want to read a line from... (4 Replies)
Discussion started by: mjavalkar
4 Replies

6. Emergency UNIX and Linux Support

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (3 Replies)
Discussion started by: nithins007
3 Replies

7. Shell Programming and Scripting

Find a line using a condition and replace a string in that line

Hello, I have a 100 line code. I have given a sample of it below: ABC*654654*1*54.54*21.2*87*1*654654654654 CCC*FS*FS*SFD*DSF GGG*FGH*CGB*FBDFG*FGDG ABC*654654*1*57.84*45.4*88*2*6546546545 CCC*WSF*SG*FGH*GHJ ADA*AF*SFG*DFGH*FGH*FGTH I need to select the line starting with "ABC" its... (6 Replies)
Discussion started by: nithins007
6 Replies

8. Solaris

Line too long error Replace string with new line line character

I get a file which has all its content in a single row. The file contains xml data containing 3000 records, but all in a single row, making it difficult for Unix to Process the file. I decided to insert a new line character at all occurrences of a particular string in this file (say replacing... (4 Replies)
Discussion started by: ducati
4 Replies

9. Shell Programming and Scripting

bash: read file line by line (lines have '\0') - not full line has read???

I am using the while-loop to read a file. The file has lines with null-terminated strings (words, actually.) What I have by that reading - just a first word up to '\0'! I need to have whole string up to 'new line' - (LF, 10#10, 16#A) What I am doing wrong? #make file 'grb' with... (6 Replies)
Discussion started by: alex_5161
6 Replies

10. Shell Programming and Scripting

search for a string ,replace the whole line with new line

hai i am very new to unix. i am having two files like this. first.properties cache.ZA.TL_CCY=SELECT trim(CCY_CODE)||trim(COUNTRY_CODE)||trim(CITY_CODE) AS... (4 Replies)
Discussion started by: kkraja
4 Replies
Login or Register to Ask a Question