Visit Our UNIX and Linux User Community

Updating variables using sed or awk

Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Updating variables using sed or awk
# 1  
Old 08-25-2015
Updating variables using sed or awk

I have a file(testfile.txt) that contains list of variables as shown below. T


……and so on there are 50 different variables.

I am writing a script( that will update the above three variable one by one with the values passed as a parameter to the script.

So Ron Donnely 1960-01-31 passing the three parameters. take these three parameter in $1, $2 and $3.

I am trying to see if there is a SED or AWK that I can use in to update the testfile.txt to update the variable values with the values passed to the script as a parameter. So the values in testfile.txt should look like something below:


Please note: Variable Filename cannot be changed.

Will appreciate any help.

Last edited by Corona688; 08-25-2015 at 03:30 PM..
# 2  
Old 08-25-2015
Perhaps something like this:

# Setting IFS="%" causes "${*}" to become "arg1%arg2%arg3%arg4".

# arg1%arg2%arg3%arg4 gets split in awk later to A[1]=arg1, A[2]=arg2, A[3]=arg3, A[4]=arg4.
# Line 1 matches A[1] gets arg1, line 2 matches A[2] gets arg2, etc.
awk -F= -v OFS="=" -v S="${*}" 'BEGIN { split(S,A,"%") } ; NR in A { $2=A[NR] } 1' filename > newfilename

# 3  
Old 08-25-2015
Hello Saanvi,

Following may help you in same too but here considering the values will not have spaces in themselves.
 cat test78.ksh
for j in $@
        let "i = i + 1"
        VAL1=`head -$i test78`
        VAL=`echo ${VAL1%%=*}`
        echo $VAL"="$j

Output will be as follows.
./test78.ksh test singh 1960-01-31

R. Singh
# 4  
Old 08-25-2015
Thanks Corona for the response. Will the above fix will work if the variable are not one below the other. I am sorry if it was not clear in my post. The file contains more than 50 variables. I want to update few of them based on the variable name and assign them new values.
I just gave an example in my post that happened to be one below the other. The update of the variable value needs to happen based on the variable name that I choose in my script. It can be $$POBOX variable on 45 line.The point is the variables, I am trying to update are not below one below the other. One variable can be on line 10 the other one on 40. Hence looking to update the variable value by picking up the variable name and assigning new value to it.

---------- Post updated at 02:47 PM ---------- Previous update was at 02:45 PM ----------

Thank you R Singh. Let me try your suggested code.

Thanks again
# 5  
Old 08-25-2015
The file contains more than 50 variables. I want to update few of them based on the variable name and assign them new values. I just gave an example in my post that happened to be one below the other. The update of the variable value needs to happen based on the variable name that I choose in my script.
The arguments don't contain any variable names at all. How should the script decide what names to update?

Last edited by Corona688; 08-25-2015 at 05:00 PM..
# 6  
Old 08-25-2015
If the list of names and order of arguments is fixed:


NAMES='$$FirstName $$LastName $$Dateofbirth'


awk -F= -v OFS="=" -v VS="${NAMES}" -v S="${*}" 'BEGIN { split(S,A,"%") ; split(VS,VY," ") ; for(X in VY) V[VY[X]]=A[X] } $1 in V { $2=V[$1] } 1' < inputfile > outputfile

# 7  
Old 08-26-2015
The latter "" deserves an example/instruction:
 cat inputfile

 ./ "Ron%Donnely%1960-01-31"

 cat outputfile

Previous Thread | Next Thread
Test Your Knowledge in Computers #576
Difficulty: Medium
All programming languages have automatic garbage collection that monitors the dynamically allocated pieces of memory and determine if any variable in the program still references it. If the memory is no longer referenced, it is 'garbage' and becomes eligible to be 'collected'.
True or False?

10 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

Use of Variables in a sed/awk script

Hi, After looking at the differents post on this forum, I am convinced that I will benefit from the experience of advanced Unix user on some script I have already done for an aeronautical study. Here is one of them : Step 1 : sed -e "s/??/00/g" Base_Awk.txt > Awk_Cut_00.txt4; sed... (11 Replies)
Discussion started by: Marc_Camoc
11 Replies

2. Shell Programming and Scripting

Variables into SED or AWK and multiple commands

Hello I am hoping you may help. I am not sure how to go about this exactly, I know the tools but not sure how to make them work together. I have two SED commands that I would like to run in a shell script. I would like to take the manual input of a user (types in when prompted) to be used... (4 Replies)
Discussion started by: lostincashe
4 Replies

3. Shell Programming and Scripting

print pattern between two variables awk sed

I am trying to print text between two variables in a file I have tried the following things but none seem to work: awk ' /'$a'/ {flag=1;next} /'$b'/{flag=0} flag { print }' file and also sed "/$a/,/$b/p" file But none seem to work Any Ideas? Thanks in Advance (5 Replies)
Discussion started by: forumbaba
5 Replies

4. UNIX for Dummies Questions & Answers

Updating environment variables

ok, this definitely falls in the n00b category... I'm trying to upgrade Java on my server and just need to update the PATH, CLASSPATH, and JAVA_HOME environment variables. This is what they currently are:... (4 Replies)
Discussion started by: jtennent
4 Replies

5. Shell Programming and Scripting

Updating a line in a large csv file, with sed/awk?

I have an extremely large csv file that I need to search the second field, and upon matches update the last field... I can pull the line with awk.. but apparently you cant use awk to directly update the file? So im curious if I can use sed to do this... The good news is the field I want to... (5 Replies)
Discussion started by: trey85stang
5 Replies

6. Shell Programming and Scripting

Using variables within awk/sed commands

Can I use my own variables within awk and sed for example: I've written a while loop with a counter $i and I want to use the value of $i within sed and awk to edit certain lines of text within a data file. I want to use : sed '1s/$/texthere/g' data.csv Like this: sed '$is/$/$age/g' data.csv... (5 Replies)
Discussion started by: mustaine85
5 Replies

7. Shell Programming and Scripting

awk updating one file with another, comparing, updating

Hello, I read and search through this wonderful forum and tried different approaches but it seems I lack some knowledge and neurones ^^ Here is what I'm trying to achieve : file1: test filea 3495; test fileb 4578; test filec 7689; test filey 9978; test filez 12300; file2: test filea... (11 Replies)
Discussion started by: mecano
11 Replies

8. Shell Programming and Scripting

Accessing Shell Variables in awk or sed

Hello, I wonder if it is possible to pass and use variables from shell environment into sed or awk. I am trying to achieve something similar to the following using sed or awk: var=some_regular_expression grep "$var" filename # Will extract lines from filename The following code,... (3 Replies)
Discussion started by: nasersh
3 Replies

9. Shell Programming and Scripting

using sed on bash variables (or maybe awk?)

Hi all- I've been fooling with this for a few days, but I'm rather new at this... I have a bash variable containing a long string of various characters, for instance: JUNK=this that the other xyz 1234 56 789 I don't know what "xyz" actually is, but I know that: START=he other and ... (2 Replies)
Discussion started by: rev66
2 Replies

10. Shell Programming and Scripting

Manipulating awk $variables using sed?

I have been searching around the forums here trying to find a solution to my problem but not getting anywhere but closer to baldness. I have a 20 column pipe "|" seperated text file. The 14th variable doesnt always exist, but will have the format of YYYYMM or YYYY if it does. I need to take... (2 Replies)
Discussion started by: r0sc0
2 Replies

Featured Tech Videos