LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   How can I use Shell script to edit a data at a particular location in a txt file? (http://www.linuxquestions.org/questions/programming-9/how-can-i-use-shell-script-to-edit-a-data-at-a-particular-location-in-a-txt-file-775490/)

leena_d 12-14-2009 02:37 AM

How can I use Shell script to edit a data at a particular location in a txt file?
 
I have a txt file with 500 records/rows.

I want to edit row 23, column 5-8 of the file & change it to 'wxyz'.
I have to use UNIX shell scripting for this.

Previously I used to do this activity manuly in vi editor.
I want to automate this.

Somebody told me that i need to use awk/sed programming for this.
I don't know awk programming at all.

Can anybody help me in this?

ghostdog74 12-14-2009 02:53 AM

what have you tried?

ashok.g 12-14-2009 02:57 AM

Indeed you are asking the program for an editor dude... Upto what extent you have tried....?

leena_d 12-14-2009 04:46 AM

I have zero knowledge of awk programming.

Simple shell prog.'s nt working. :(

catkin 12-14-2009 04:53 AM

Quote:

Originally Posted by leena_d (Post 3790712)
I have zero knowledge of awk programming.

Simple shell prog.'s nt working. :(

So show us the input, the desired output, the script you tried, any error messages and the output.

H_TeXMeX_H 12-14-2009 05:50 AM

You can probably easily do this with awk, but we need to see before and after example.

pixellany 12-14-2009 07:09 AM

1. You have to state the problem clearly and completely. e.g.
Quote:

column 5-8 of the file & change it to 'wxyz'
Does this mean change each column to "wxyz" or to replace all 4 columns with "wxyz"?

2. What documentation for SED and AWK have you looked at?

Quote:

I have to use UNIX shell scripting for this.
Not Linux?? And who says you have to do this?

leena_d 12-14-2009 07:36 AM

e.g.

previously, file was like
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
H D R 3 0 1 0 2 0 0 9 1 0 3 0 5 0
D T A 1 0 0 0 5 X 1 0 0 0 0 0 . 5 0
D T A 1 0 2 0 1 X 2 0 0 0 0 0 . 5 0
T R L 3 0 0 0 0 1 . 0 0 0 0 0 0 0 0 . 0 0 0 4


I say, edit at row 2, col 9-10 location & whatever be the previous data, change it to xy
User I/P would be file name, row num, column from & to, and new val.

Later (after running the script)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
H D R 3 0 1 0 2 0 0 9 1 0 3 0 5 0
D T A 1 0 0 0 x y 1 0 0 0 0 0 . 5 0
D T A 1 0 2 0 1 X 2 0 0 0 0 0 . 5 0
T R L 3 0 0 0 0 1 . 0 0 0 0 0 0 0 0 . 0 0 0 4

ghostdog74 12-14-2009 07:43 AM

so where's your script that is not working?

pixellany 12-14-2009 07:46 AM

OK---but please answer ALL of my questions.....

For example, if you are required to use "UNIX shell scripting" (because it is homework??), then maybe you are not allowed to use SED or AWK.......

H_TeXMeX_H 12-14-2009 12:48 PM

Well, the only way I can think of doing this in one easy step is with awk:

Code:

awk '{ if (NR == 3) { $8="x"; $9="y" } print }' input
The output is:

Code:

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
H D R 3 0 1 0 2 0 0 9 1 0 3 0 5 0
D T A 1 0 0 0 x y 1 0 0 0 0 0 . 5 0
D T A 1 0 2 0 1 X 2 0 0 0 0 0 . 5 0
T R L 3 0 0 0 0 1 . 0 0 0 0 0 0 0 0 . 0 0 0 4

That's what you wanted right ?

ghostdog74 12-14-2009 07:48 PM

and you have let the cat out of the bag. anyway, since its out, shorter version
Code:

awk 'NR==3{$8="x";$9="y"}1' input

leena_d 01-25-2010 04:22 AM

the file is not space separated.
the above file is a flat file with nothing called as field.
Though it looks like, bt assume that there are no spaces in the above example, & the data is continuous.

I tried
awk -v r=3 -v c=8 -v val=x 'BEGIN {}; NR != r {print}; NR == r {$c = val; print}' Leena_New

But this treats the file as a space separated file & edits the 8th Field insted of 8th column.

jschiwal 01-25-2010 04:34 AM

I'm still not sure from your description whether the line you are editing is:
D T A 1 0 0 0 5 X 1 0 0 0 0 0 . 5 0
or
DTA10005x100000.50

When talking about rows you are starting with record 0. When you talk about columns, you start counting with 1.

For "DTA10005x100000.50", you can use sed. It will be easier if you use extended regular expressions. (less escaping)
Use storage registers to save what is before and after the characters to replace.

You could also look at reading each line into a bash array variable and editing the elements.
awk has functions that deal with manipulating substrings.

So there are a number of ways of doing what you want.

konsolebox 01-25-2010 05:30 AM

using sed:
Code:

sed -i '23s@\(....\)....@\1wxyz@' FILE
Please try it on a test file before actually using it.


All times are GMT -5. The time now is 05:07 PM.