LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 12-14-2009, 02:37 AM   #1
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Rep: Reputation: 15
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?

Last edited by leena_d; 12-14-2009 at 04:30 AM.
 
Old 12-14-2009, 02:53 AM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
what have you tried?
 
Old 12-14-2009, 02:57 AM   #3
ashok.g
Member
 
Registered: Dec 2009
Location: Hyderabad,India
Distribution: RHEl AS 4
Posts: 215

Rep: Reputation: 32
Indeed you are asking the program for an editor dude... Upto what extent you have tried....?
 
Old 12-14-2009, 04:46 AM   #4
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
I have zero knowledge of awk programming.

Simple shell prog.'s nt working.
 
Old 12-14-2009, 04:53 AM   #5
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by leena_d View Post
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.
 
Old 12-14-2009, 05:50 AM   #6
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
You can probably easily do this with awk, but we need to see before and after example.
 
Old 12-14-2009, 07:09 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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?
 
Old 12-14-2009, 07:36 AM   #8
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
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

Last edited by leena_d; 12-14-2009 at 07:39 AM.
 
Old 12-14-2009, 07:43 AM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
so where's your script that is not working?
 
Old 12-14-2009, 07:46 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.......
 
Old 12-14-2009, 12:48 PM   #11
H_TeXMeX_H
Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269Reputation: 1269
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 ?
 
Old 12-14-2009, 07:48 PM   #12
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,695
Blog Entries: 5

Rep: Reputation: 240Reputation: 240Reputation: 240
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
 
Old 01-25-2010, 04:22 AM   #13
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 01-25-2010, 04:34 AM   #14
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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.

Last edited by jschiwal; 01-25-2010 at 04:46 AM.
 
Old 01-25-2010, 05:30 AM   #15
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,245
Blog Entries: 15

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


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I use Shell script to edit row 23 column 5-8 in a txt file? leena_d Linux - Newbie 4 12-14-2009 03:43 AM
Shell script to edit a file kaash_m31 Linux - Newbie 2 06-02-2008 05:43 AM
shell script to move files from one system to another sytem with file names in a txt coral_km Linux - Newbie 3 02-13-2008 10:23 PM
shell script for changing a txt file content Flobsi Linux - Newbie 3 10-06-2006 03:10 AM
shell script for changig txt-file content Flobsi Programming 1 10-05-2006 05:46 PM


All times are GMT -5. The time now is 12:20 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration