Sponsored Content
Top Forums Shell Programming and Scripting Update a specific line in a file while reading sequentially Post 302421193 by bharath.gct on Thursday 13th of May 2010 05:55:25 PM
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
 

9 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

4. 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

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. 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

7. 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

8. 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

9. 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
orddict(3erl)						     Erlang Module Definition						     orddict(3erl)

NAME
orddict - Key-Value Dictionary as Ordered List DESCRIPTION
Orddict implements a Key - Value dictionary. An orddict is a representation of a dictionary, where a list of pairs is used to store the keys and values. The list is ordered after the keys. This module provides exactly the same interface as the module dict but with a defined representation. One difference is that while dict considers two keys as different if they do not match ( =:= ), this module considers two keys as different if and only if they do not com- pare equal ( == ). DATA TYPES
ordered_dictionary() as returned by new/0 EXPORTS
append(Key, Value, Orddict1) -> Orddict2 Types Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() This function appends a new Value to the current list of values associated with Key . An exception is generated if the initial value associated with Key is not a list of values. append_list(Key, ValList, Orddict1) -> Orddict2 Types ValList = [Value] Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() This function appends a list of values ValList to the current list of values associated with Key . An exception is generated if the initial value associated with Key is not a list of values. erase(Key, Orddict1) -> Orddict2 Types Key = term() Orddict1 = Orddict2 = ordered_dictionary() This function erases all items with a given key from a dictionary. fetch(Key, Orddict) -> Value Types Key = Value = term() Orddict = ordered_dictionary() This function returns the value associated with Key in the dictionary Orddict . fetch assumes that the Key is present in the dictio- nary and an exception is generated if Key is not in the dictionary. fetch_keys(Orddict) -> Keys Types Orddict = ordered_dictionary() Keys = [term()] This function returns a list of all keys in the dictionary. filter(Pred, Orddict1) -> Orddict2 Types Pred = fun(Key, Value) -> bool() Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() Orddict2 is a dictionary of all keys and values in Orddict1 for which Pred(Key, Value) is true . find(Key, Orddict) -> {ok, Value} | error Types Key = Value = term() Orddict = ordered_dictionary() This function searches for a key in a dictionary. Returns {ok, Value} where Value is the value associated with Key , or error if the key is not present in the dictionary. fold(Fun, Acc0, Orddict) -> Acc1 Types Fun = fun(Key, Value, AccIn) -> AccOut Key = Value = term() Acc0 = Acc1 = AccIn = AccOut = term() Orddict = ordered_dictionary() Calls Fun on successive keys and values of Orddict together with an extra argument Acc (short for accumulator). Fun must return a new accumulator which is passed to the next call. Acc0 is returned if the list is empty. The evaluation order is undefined. from_list(List) -> Orddict Types List = [{Key, Value}] Orddict = ordered_dictionary() This function converts the Key - Value list List to a dictionary. is_key(Key, Orddict) -> bool() Types Key = term() Orddict = ordered_dictionary() This function tests if Key is contained in the dictionary Orddict . map(Fun, Orddict1) -> Orddict2 Types Fun = fun(Key, Value1) -> Value2 Key = Value1 = Value2 = term() Orddict1 = Orddict2 = ordered_dictionary() map calls Func on successive keys and values of Orddict to return a new value for each key. The evaluation order is undefined. merge(Fun, Orddict1, Orddict2) -> Orddict3 Types Fun = fun(Key, Value1, Value2) -> Value Key = Value1 = Value2 = Value3 = term() Orddict1 = Orddict2 = Orddict3 = ordered_dictionary() merge merges two dictionaries, Orddict1 and Orddict2 , to create a new dictionary. All the Key - Value pairs from both dictionaries are included in the new dictionary. If a key occurs in both dictionaries then Fun is called with the key and both values to return a new value. merge could be defined as: merge(Fun, D1, D2) -> fold(fun (K, V1, D) -> update(K, fun (V2) -> Fun(K, V1, V2) end, V1, D) end, D2, D1). but is faster. new() -> ordered_dictionary() This function creates a new dictionary. size(Orddict) -> int() Types Orddict = ordered_dictionary() Returns the number of elements in an Orddict . store(Key, Value, Orddict1) -> Orddict2 Types Key = Value = term() Orddict1 = Orddict2 = ordered_dictionary() This function stores a Key - Value pair in a dictionary. If the Key already exists in Orddict1 , the associated value is replaced by Value . to_list(Orddict) -> List Types Orddict = ordered_dictionary() List = [{Key, Value}] This function converts the dictionary to a list representation. update(Key, Fun, Orddict1) -> Orddict2 Types Key = term() Fun = fun(Value1) -> Value2 Value1 = Value2 = term() Orddict1 = Orddict2 = ordered_dictionary() Update a value in a dictionary by calling Fun on the value to get a new value. An exception is generated if Key is not present in the dictionary. update(Key, Fun, Initial, Orddict1) -> Orddict2 Types Key = Initial = term() Fun = fun(Value1) -> Value2 Value1 = Value2 = term() Orddict1 = Orddict2 = ordered_dictionary() Update a value in a dictionary by calling Fun on the value to get a new value. If Key is not present in the dictionary then Initial will be stored as the first value. For example append/3 could be defined as: append(Key, Val, D) -> update(Key, fun (Old) -> Old ++ [Val] end, [Val], D). update_counter(Key, Increment, Orddict1) -> Orddict2 Types Key = term() Increment = number() Orddict1 = Orddict2 = ordered_dictionary() Add Increment to the value associated with Key and store this value. If Key is not present in the dictionary then Increment will be stored as the first value. This could be defined as: update_counter(Key, Incr, D) -> update(Key, fun (Old) -> Old + Incr end, Incr, D). but is faster. NOTES
The functions append and append_list are included so we can store keyed values in a list accumulator . For example: > D0 = orddict:new(), D1 = orddict:store(files, [], D0), D2 = orddict:append(files, f1, D1), D3 = orddict:append(files, f2, D2), D4 = orddict:append(files, f3, D3), orddict:fetch(files, D4). [f1,f2,f3] This saves the trouble of first fetching a keyed value, appending a new value to the list of stored values, and storing the result. The function fetch should be used if the key is known to be in the dictionary, otherwise find . SEE ALSO
dict(3erl) , gb_trees(3erl) Ericsson AB stdlib 1.17.3 orddict(3erl)
All times are GMT -4. The time now is 07:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy