Assign Values to a Variable in While Loop and Update the File


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Assign Values to a Variable in While Loop and Update the File
# 1  
Old 05-14-2018
Assign Values to a Variable in While Loop and Update the File

Hello,

Could anyone please help me with Assigning a value to variable and then updating the value in the original file

Code:
IFS='|'
while read -r Serial_ID JOB_NAME STATUS 
do
if [ "$STATUS" = "" ]
then 
echo "Perform Fuctions"
???Assign STATUS to COMPLETED and Update File???    
done <File


Last edited by Scrutinizer; 05-14-2018 at 04:27 PM.. Reason: code tags
# 2  
Old 05-14-2018
Show the input you have and show the output you want.
# 3  
Old 05-14-2018
On a hunch, perhaps something like:
Code:
while IFS="|" read -r SERIAL JOB STATUS
do
        if [ condition ] 
        then
                STATUS="somethingelse"
        fi
        printf "%s|%s|%s\n" "$SERIAL" "$JOB" "$STATUS"
done < inputfile > outputfile

Note that inputfile and outputfile can't be the same.
# 4  
Old 05-14-2018
Not sure I entirely understand, but
Code:
STATUS=COMPLETED
printf "%s%s%s%s%s\n" "$Serial_ID" $IFS "$JOB_NAME" $IFS "$STATUS" >> file2

and rename file2 to FIle after the loop?
# 5  
Old 05-14-2018
Process the ones which have null in the 3rd column and then update the 3rd column in the file as COMPLETED

Input
Code:
123|ABC|
456|PQR|COMPLETED
111|XYZ|
121|ASD|COMPLETED

Output
Code:
123|ABC|COMPLETED
456|PQR|COMPLETED
111|XYZ|COMPLETED
121|ASD|COMPLETED

---------- Post updated at 03:49 PM ---------- Previous update was at 03:46 PM ----------

I need to use the same file and update the row in the file by assigning Completed in the 3rd column after i perform some functions



Moderator's Comments:
Mod Comment Please use CODE tags as required by forum rules!

Last edited by RudiC; 05-14-2018 at 04:52 PM.. Reason: Added CODE tags.
# 6  
Old 05-14-2018
You cannot.



Not with standard text files.
# 7  
Old 05-14-2018
Quote:
Originally Posted by infernalhell
I need to use the same file and update the row in the file by assigning Completed in the 3rd column after i perform some functions
Files do not work that way. Even something like sed -i really just replaces or overwrites the entire file.

So I think my method should work:

Code:
cp inputfile inputfile.bak
while IFS="|" read -r SERIAL JOB STATUS
do
        if [ -z "$STATUS" ] 
        then
                do_something
                STATUS="somethingelse"
        fi
        printf "%s|%s|%s\n" "$SERIAL" "$JOB" "$STATUS"
done < inputfile > outputfile

mv outputfile inputfile

This User Gave Thanks to Corona688 For This Post:
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 assign awk values to shell variable?

Hi Gurus, I have a script which assign awk output to shell variable. current it uses two awk command to assign value to two variables. I want to use one command to assign two values to two variables. I tried the code, but it does't work. kindly provide your suggestion. current code... (2 Replies)
Discussion started by: green_k
2 Replies

2. Shell Programming and Scripting

Do While Loop + Read From File + assign line to a variable

Hello, I am using below code for reading from a file and assigning the values to a variable , but it is loosing the value after the loop , please suggest to retain the value of the variable after the loop , while IFS=: read -r line do set $dsc=$line echo 'printing line variable ' $line... (1 Reply)
Discussion started by: ParthThakkar
1 Replies

3. Shell Programming and Scripting

Unable to read assign values to two variables in while loop

I am trying to read a input file which has two columns separated by space Input file server1 server2 server3 server4 server5 server6 When i execute the below while code it reads line by line and a and b variables are able to successfully fetch the values while read a b do echo "$a" echo... (5 Replies)
Discussion started by: chidori
5 Replies

4. Shell Programming and Scripting

Assign variable name through loop

Hi As bash does not support multidimensional arrays (?), I need some help with a problem. What I want to do is to assign variable names containing a counter in a loop . what I want to do is basically something like this: #!/bin/bash for i in {1..8}; do var$i = "some command" done... (6 Replies)
Discussion started by: Tobbev
6 Replies

5. UNIX for Dummies Questions & Answers

How to assign values to variable in given scenario

Hi i have one variable like DIR="f1 f2" in config file in my script i have one runtime variable LFILE="DIR" now i want to use $DIR in my script by using LFILE that is i dont want to use DIR dirctly i am extracting DIR by some other means. Config file : DIR="f1 f2" Script: LFILE="DIR" i... (3 Replies)
Discussion started by: sriram_gec
3 Replies

6. UNIX for Advanced & Expert Users

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the values... (12 Replies)
Discussion started by: manishab00
12 Replies

7. Fedora

How to read a text file and assign the values in the same to a variable in loop

Hi, I have a text file with multiple lines, each having data in the below format <DOB>,<ADDRESS> I have to write a script which reads each line in the text file in loop, assign the values to these variables and do some further processing in it. Using the following code prints the... (1 Reply)
Discussion started by: manishab00
1 Replies

8. Shell Programming and Scripting

Assign values to variable

Hi Masters, I want to assign the values of one variable to another variable. Here the varaible name 'var' is dynamic. I know the values of V_2 and U_3, but If the i/p of TYPE is 'U' and the NO is 3, then I want to assign the values of U_3 to var. How we can achieve it? TYPE="U"... (4 Replies)
Discussion started by: ecearund
4 Replies

9. Shell Programming and Scripting

Read the csv file and assign the values in to variable

I have a csv file with the values seperated by commas.I want to extract these values one by one and assign to a variable using shell script.Any ideas or code? (11 Replies)
Discussion started by: rajbal
11 Replies

10. Shell Programming and Scripting

Read a file and assign the values to a variable

i have a file in this format curyymm PRVYYMM CDDMmmYY bddMmmyy eddMmmyy --------- ------- ------------ ---------- ----------- 0906 0905 09Jun09 01Jun09 30Jun09 ----------- --------- ------------ ------------ ----------- i need to read the... (5 Replies)
Discussion started by: depakjan
5 Replies
Login or Register to Ask a Question