Input a variable and write to a file using awk

 
Thread Tools Search this Thread
Special Forums UNIX and Linux Applications Input a variable and write to a file using awk
# 1  
Old 04-06-2010
Input a variable and write to a file using awk

Hi

I am trying to edit a csv file. Bacically I need to input a search variable and the value that must be changed in one of the fields corresponding to that searched variable.

My csv file looks like so:

Code:
1,1A,5
1,1B,2
1,1C,3
2,2A,7
2,2B,4
2,2C,0
3,3A,1
3,3B,6
3,3C,4

I want to input a value for the second field (these are unique) and input a new value for the third field.

Example, If I input 1A and 2, then the new value of 2 must be written to the file and replace 5.

This is my awk file:

Code:
BEGIN {
FS=","
}
{
if (cellid == $2)
print "old parameter value was: " $3 " " $2 " new value is now " newvalue;
} print $3=newvalue>ds_pl_cell1.csv;

And from my command line, I call this:
Code:
 awk -v "cellid=1A" -v "newvalue=0" -f changefile.awk ds_pl_cell.csv >ds_pl_cell1.csv

Please can you help as I am very new to this.

Thank you.

Last edited by radoulov; 04-06-2010 at 05:30 AM.. Reason: Please use code tags!
# 2  
Old 04-06-2010
Try this:
Code:
awk -F, -v cellid="1A" -v newvalue="0" '$2==cellid{$3=newvalue}1' OFS="," file > newfile

# 3  
Old 04-06-2010
Thank you so much, this really helps.
A quick question though, I would like to use an awk file, so I can build up on it as I learn how to alter the csv file. Can you tell me how this can be done.
Thank you
# 4  
Old 04-06-2010
You could place this in a shell script:

Code:
awk -F, -v cellid="$1" -v newvalue="$2" '$2==cellid{$3=newvalue}1' OFS="," file > newfile

and call it as:

Code:
./scriptname 1A 0

# 5  
Old 04-06-2010
thank u very much. this works like a charm. I appreciate your help.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Variable input to awk script

Hi guys, I wrote the following function to compare two csv files column by column. However, sometimes the input needs to be sorted before parsing it to awk. I can do this by changing the awk arguments, but I would like to make this variable if possible. The below doesn't work since the... (3 Replies)
Discussion started by: Subbeh
3 Replies

2. Shell Programming and Scripting

Read input files and merge them in given order and write them to input one param or one file

Dear Friends, I am looking for a shell script to merge input files into one file .. here is my idea: 1st paramter would be outfile file (all input files content) read all input files and merge them to input param 1 ex: if I pass 6 file names to the script then 1st file name as output file... (4 Replies)
Discussion started by: hyd1234
4 Replies

3. Shell Programming and Scripting

XML variable for input in same input file

Dear All , i stuck in one problem executing xml .. i have input xml as <COMMAND name="ARRANGEMENT.WRITE" timestamp="0" so="initial"> <SVLOBJECT> <LONG name="CSP_PMNT_ID" val="-1"/> <MONEY name="CSP_CEILING" amount="0.0" currency="AUD"/> ... (6 Replies)
Discussion started by: arvindng
6 Replies

4. Shell Programming and Scripting

Passing variable as an input file to AWK comand

Hi, I would like to compare 2 files using awk, which I can do by using: awk 'NR==FNR{a;next} (NR > 32 && $2 in a) {print $0}' File1 and File2. If the name of the File1 is in another file (for example, column 4 in File 3) then how can I pass this column 4 to the awk command. Thanks in... (1 Reply)
Discussion started by: ezhil01
1 Replies

5. Programming

take input from a variable as pattern to awk

Hi everyone, Can anyone tell me how to take contents of a variable as a pattern for awk command. Am doing as below, but doesnt get any output: $c = "Tue Dec"; $log = ` awk '/ \$c /' in.txt`; print $log; (7 Replies)
Discussion started by: anandrec
7 Replies

6. Shell Programming and Scripting

Variable as input to awk command

Hi Gurus, I need a suggestion, please help. I have a input file as below : abc.txt : * xxxx: 00000 xxxxx: 00000 xxxx: RANDOM xxx: RANDOM **************************xxxxxxx*** * abc ****************************** abc: abc: ... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

7. Shell Programming and Scripting

Unfold the data from a input file and write to a file

I am working on a script to unfold data for each column from a specific line of data and write output in a single line. Input data looks like this. 2011-09-26 INF UM_10 UserMana Starting synchronization for security domain 14:37:31 080 gementSe . rvice I... (2 Replies)
Discussion started by: svajhala
2 Replies

8. Shell Programming and Scripting

awk built-in variable for input file

Hi guys, Does awk have a built-in variable which I can use to display the input file it's currently reading? I'm currently concatenating multiple files using awk and later on do some parsing. But for now, I want to add an extra column in the main output data file - basically putting in the... (3 Replies)
Discussion started by: Det7
3 Replies

9. Shell Programming and Scripting

How to write shell script for input file name format checking?

Hello, I had written a shell script that accepts input file as cmd line argument and process this file. if ; then if ; then . $1 LOGFILE="$LOG_FILE/MIG_BIOS.log"; get_input_file else ERROR_CODE=MSCRM0005_003 error "$ERROR_CODE : Input file $1 is not available"; exit... (3 Replies)
Discussion started by: Poonamol
3 Replies

10. Shell Programming and Scripting

Write a new file from 2 files as input to the script

Hi- I am hoping someone can give me some pointers to get me started. I have a file which contains some dn's .e.g file 1 cn=bob,cn=user,dc=com cn=kev,cn=user,dc=com cn=john,cn=user,dc=com I have a second file e.g. file.template which looks something like :- dn: <dn> objectclass:... (5 Replies)
Discussion started by: sniper57
5 Replies
Login or Register to Ask a Question