writing in a file's particular column number of every line during runtime


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting writing in a file's particular column number of every line during runtime
# 1  
Old 09-03-2011
writing in a file's particular column number of every line during runtime

Given a particular line number and a corresponding column number, can i write something in the file during run time?

For example x=1 and during runtime i want to write the value of x in column 100 of every line of a given file, then how shud that be done?

Thanks

Last edited by arindamlive; 09-03-2011 at 12:56 PM.. Reason: spelling mistake
# 2  
Old 09-03-2011
assuming the file is delimited with spaces:
Code:
# Process 'file' and save to temp file /tmp/$$
# This example replaces column 100, line 32, with the string "x".
awk -v L=32 -v C=100 -v V="x" '{ if(NR == LINE) $COL=VAL } 1 < file > /tmp/$$
# Overwrite 'file' with contents of /tmp/$$
cat /tmp/$$ > file
# delete temp file
rm /tmp/$$

# 3  
Old 09-03-2011
Quote:
Originally Posted by Corona688
assuming the file is delimited with spaces:
Code:
# Process 'file' and save to temp file /tmp/$$
# This example replaces column 100, line 32, with the string "x".
awk -v L=32 -v C=100 -v V="x" '{ if(NR == LINE) $COL=VAL } 1 < file > /tmp/$$
# Overwrite 'file' with contents of /tmp/$$
cat /tmp/$$ > file
# delete temp file
rm /tmp/$$

@Corona688, shouldn't it be
Code:
awk -v LINE=32 -v COL=100 -v VAL="x" '{ if(NR == LINE) $COL=VAL } 1' < file > /tmp/$$

Smilie
This User Gave Thanks to Franklin52 For This Post:
# 4  
Old 09-03-2011
i dont find any change in the file after i execute this command

Last edited by arindamlive; 09-03-2011 at 03:21 PM..
# 5  
Old 09-03-2011
Give examples of lines in your file.
# 6  
Old 09-03-2011
i have this following file named test.txt whose content is

Hello
World

Whats Up?


and i execute the following command
x="Hey"
awk -v LINE=3 -v COL=1 -v VAL="x" '{ if(NR == LINE) $COL=VAL } 1' test.txt /tmp/$$


Now the output should be
Hello
World
Hey
Whats Up?

BUT thats not happening
# 7  
Old 09-03-2011
Code:
cat INPUTFILE
Hello 
World

Whats Up?
 
x=Hey; awk -v LINE=3 -v COL=1 -v VAL="$x" '{ if(NR == LINE) $COL=VAL } 1' INPUTFILE 
Hello 
World
Hey
Whats Up?

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Printing the line number of first column found

Hello, I have a question on how to find the line number of the first column that contains specific data. I know how to print all the line numbers of those columns, but haven't been able to figure out how to print only the first one that is found. For example, if my data has four columns: 115... (3 Replies)
Discussion started by: user553
3 Replies

2. Shell Programming and Scripting

search a string in a particular column of file and return the line number of the line

Hi All, Can you please guide me to search a string in a particular column of file and return the line number of the line where it was found using awk. As an example : abc.txt 7000,john,2,1,0,1,6 7001,elen,2,2,0,1,7 7002,sami,2,3,0,1,6 7003,mike,1,4,0,2,1 8001,nike,1,5,0,1,8... (3 Replies)
Discussion started by: arunshankar.c
3 Replies

3. Shell Programming and Scripting

Replace 2nd column for each line in a csv file with fixed string+random number

Hi experts, My csv file looks like this U;cake;michael;temp;;;; U;bread;john;temp;;;; U;cocktails;sarah;temp;;;; I'd like to change the value fo 2nd column to cf+random number , which will look maybe something like this U;cf20187;michael;temp;;;; U;cf8926;john;temp;;;;... (7 Replies)
Discussion started by: tententen
7 Replies

4. UNIX for Dummies Questions & Answers

How to read contents of a file from a given line number upto line number again specified by user

Hello Everyone. I am trying to display contains of a file from a specific line to a specific line(let say, from line number 3 to line number 5). For this I got the shell script as shown below: if ; then if ; then tail +$1 $3 | head -n $2 else ... (5 Replies)
Discussion started by: grc
5 Replies

5. Shell Programming and Scripting

Insert new line based on numerical number of column

My input file: Class Number Position Range 1 Initial 50 1 Initial 50 2 Terminal 150 2 Terminal 20 2 Single 10 3 Single 20 4 Double 50 5 Initial 50 5 Initial 60 Class Number... (11 Replies)
Discussion started by: patrick87
11 Replies

6. Shell Programming and Scripting

Writing out 2nd column into one file from multiple files

I have several files that are being generated every 20 minutes. Each file contains 2 columns. The 1st column is Text, 2nd column is Data. I would like to generate one single file from all these files as follows: One instance of 1st column Text, followed by 2nd column Data separated by... (5 Replies)
Discussion started by: subhap
5 Replies

7. Shell Programming and Scripting

Writing line to a file

Hi, I'm reading and writing a line from one file to another. Input line is like this: My name is David. Working on a shell script now. When I use "echo" or "print" output comes like this, My name is David. Working on a shell script now. Sample... (7 Replies)
Discussion started by: dateez
7 Replies

8. Shell Programming and Scripting

Writing given value into a file in particular record and column

Hi All, Could you ppl plz help me in writing into a file , plz find the example below. Ex: Name1|092387|Address1 Name2||Address2 After executing command/script the file should look like Name1|092387|Address1 Name2|+91900236|Address2 plz let me know of you have some solution... (4 Replies)
Discussion started by: shreekrishnagd
4 Replies

9. Shell Programming and Scripting

how to get the data from line number 1 to line number 100 of a file

Hi Everybody, I am trying to write a script that will get some perticuler data from a file and redirect to a file. My Question is, I have a Very huge file,In that file I have my required data is started from 25th line and it will ends in 100th line. I know the line numbers, I need to get all... (9 Replies)
Discussion started by: Anji
9 Replies

10. Shell Programming and Scripting

awk to select a column from particular line number

The awk command awk -F: '{print $1}' test1 gives the first columns of all the lines in file ,is there some command to get a particular column from particular line . Any help is appreciated. thanks arif (4 Replies)
Discussion started by: mab_arif16
4 Replies
Login or Register to Ask a Question