Update a specific line in a file while reading sequentially


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Update a specific line in a file while reading sequentially
# 1  
Old 05-13-2010
Update a specific line in a file while reading sequentially

All,

I know this is a very naive question but I could not find a way to get this working!

I have a file with values like

input.file

Code:
Value1
Value2
server1/mylogin,mypasswd
Value3
Value4

And in my code, I am reading the file line by line and processing it.

Code:
#! /bin/ksh
line_no=1;

while read line
do
  case $line_no in
  1) var1=$line;;
  2) var2=$line;;
  3) 
    logon_var=$line
    [I need to add my code here to update this line in the file
    [with masking text "SERVER/USERNAME,PASSWORD"]
  ;;
  4) var3=$line;;
  5) var4=$line;;
  esac

  line_no=`expr $line_no + 1`
done < input.file

echo $logon_var

Once after running the code I should see the file like:

input.file

Code:
Value1
Value2
SERVER/USERNAME,PASSWORD
Value3
Value4


Any help is greatly appreciated. Thanks

Bharath
# 2  
Old 05-13-2010
Code:
sed 's#server1/mylogin,mypasswd#SERVER/USERNAME,PASSWORD#' input.file

# 3  
Old 05-13-2010
Thanks,

I tried using the variable instead of the text itself like:

Code:
mask_text="SERVER/USERNAME,PASSWORD"
sed 's#$line#$mask_text#' input.file

This does not change anything in the input file. Also as part of the output I got the whole input file in the terminal which is kind of breaks everything that I am trying to achieve. I need to mask the logon details with NO one seeing any messages of the same!!!

Also when I give the text itself, it does not update the file rather it shows the updated file contents in the terminal.

Please help!

Last edited by bharath.gct; 05-13-2010 at 10:29 PM..
# 4  
Old 05-13-2010
Code:
echo $mask_text
SERVER/USERNAME,PASSWORD

cat abc.txt
Value1
Value2
server1/mylogin,mypasswd
Value3
Value4


sed "s#server1/mylogin,mypasswd#$mask_text#" abc.txt
Value1
Value2
SERVER/USERNAME,PASSWORD
Value3
Value4

If the contents in the file need to be changed use "-i" option

Code:
sed -i "s#server1/mylogin,mypasswd#$mask_text#" abc.txt


HTH,
PL
# 5  
Old 05-14-2010
Thanks PL,

I tried executing the sed command you gave and below is the result I get:

Input File
Code:
$ cat input.file
Value1
Value2
server1/mylogin,mypasswd
Value3
Value4

Code
Code:
$ cat editfile.test
#! /bin/ksh
line_no=1;
mask_text="SERVER/USERNAME,PASSWORD"

while read line
do
  case $line_no in
  1) var1=$line;;
  2) var2=$line;;
  3) 
    logon_var=$line
    #YOUR CODE
    sed "s#server1/mylogin,mypasswd#$mask_text#" input.file [I WANT TO USE $line INSTEAD OF server1/mylogin,mypasswd]
  ;;
  4) var3=$line;;
  5) var4=$line;;
  esac

  line_no=`expr $line_no + 1`
done < input.file

echo $logon_var

Output
Code:
$ ./editfile.test
Value1
Value2
$mask_text
Value3
Value4
server1/mylogin,mypasswd [This line is the result of the echo command]

Input file after execution
Code:
$ cat input.file
Value1
Value2
server1/mylogin,mypasswd [NO CHANGE, But I want this Changed]
Value3
Value4

Now adding -i flag in the sed, this time I used the same code above except for the sed line for which I used the below:
Code:
    #YOUR CODE
    sed -i "s#server1/mylogin,mypasswd#$mask_text#" input.file

Output
Code:
$ ./editfile.test
sed: Not a recognized flag: 1 [NOT SURE WHY GOT THESE, I AM USING KSH]
Usage:  sed [-n] Script [File ...]
           sed [-n] [-e Script] ... [-f Script_file] ... [File ...]
server1/mylogin,mypasswd [This line is the result of the echo command]

Input file after execution
Code:
$ cat input.file
Value1
Value2
server1/mylogin,mypasswd [NO CHANGE, But I want this Changed]
Value3
Value4

Please help!!

---------- Post updated 05-14-10 at 12:03 AM ---------- Previous update was 05-13-10 at 11:49 PM ----------

Got it to work by using the following code:

Code:
perl -p -i -e "s!$line!$mask_text!g" input.file

Not sure if there is any other effective way!

Anyway thanks all for your inputs! its always learning with UNIX.COM SmilieSmilie
# 6  
Old 05-14-2010
Note that the -i option for sed is specific to GNU sed and may not be available on non-GNU/Linux platforms.
# 7  
Old 05-14-2010
No need of external command

Code:
# 3) logon_var=$line
3) logon_var=${line/server1\/mylogin,mypasswd/SERVER\/USERNAME,PASSWORD}

Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

awk to update specific value in file with match and add +1 to specific digit

I am trying to use awk to match the NM_ in file with $1 of id which is tab-delimited. The NM_ will always be in the line of file that starts with > and be after the second _. When there is a match between each NM_ and id, then the value of $2 in id is substituted or used to update the NM_. Each NM_... (3 Replies)
Discussion started by: cmccabe
3 Replies

2. Shell Programming and Scripting

Extract specific line in an html file starting and ending with specific pattern to a text file

Hi This is my first post and I'm just a beginner. So please be nice to me. I have a couple of html files where a pattern beginning with "http://www.site.com" and ending with "/resource.dat" is present on every 241st line. How do I extract this to a new text file? I have tried sed -n 241,241p... (13 Replies)
Discussion started by: dejavo
13 Replies

3. Shell Programming and Scripting

Update specific field in a line of text file

I have a text file like this: subject1:LecturerA:10 subject2:LecturerA:40 if I was given string in column 1 and 2 (which are subject 1 and LecturerA) , i need to update 3rd field of that line containing that given string , which is, number 10 need to be updated to 100 ,for example. The... (6 Replies)
Discussion started by: bmtoan
6 Replies

4. UNIX for Dummies Questions & Answers

Reading a specific line from a file

Hi All, I am having 100 lines a text file say a.txt. I want read the 'nth' line from that file inside a script. Kindly tell us how to that. (2 Replies)
Discussion started by: boopathyvasagam
2 Replies

5. Shell Programming and Scripting

[Solved] Problem in reading a file line by line till it reaches a white line

So, I want to read line-by-line a text file with unknown number of files.... So: a=1 b=1 while ; do b=`sed -n '$ap' test` a=`expr $a + 1` $here do something with b etc done the problem is that sed does not seem to recognise the $a, even when trying sed -n ' $a p' So, I cannot read... (3 Replies)
Discussion started by: hakermania
3 Replies

6. Shell Programming and Scripting

Reading data from a specific line in a text file

Hello, I have a problem which is giving me headache for days, can some please help. Please see code and text fiel below. Please see text in red for the problem I am facing # Program gets an input x from user while read line ; do echo... (4 Replies)
Discussion started by: jermaine4ever
4 Replies

7. Shell Programming and Scripting

Reading data from a specific line in a text file

hello, I have got the following problem that I am hoping someone can help with please. 1. I have got the following text file (below) , the columns data are 'Test Day', 'Board', 'Betting Number'. TEXT FILE ============================================ 1 3 02-01-27-28-29-30 0 1... (1 Reply)
Discussion started by: jermaine4ever
1 Replies

8. Shell Programming and Scripting

Reading from a specific line in a loop

Hello All, Request you to let me know how to do the below urgently.. Requirement File A Contains: for i in file A DEV1 DEV5 STG1 STG5 File B Contains: for j in file B DEV1 DEV5 STG1 STG5 (3 Replies)
Discussion started by: kaushikraman
3 Replies

9. Shell Programming and Scripting

reading specific line from file

Hi all... I not a expert unix script programmer, Kindly adjust. My requirement is that, i have a file which contains the about 10 lines - say 1 2 3 ... 8 war of the worlds: => text in this line 9 9000,80,78,77,334,445 => this line contains some numbers separted by commas 10 ... (10 Replies)
Discussion started by: cool_boss2121
10 Replies
Login or Register to Ask a Question