LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
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
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
LQ Guru
 
Registered: Oct 2005
Location: $RANDOM
Distribution: slackware64
Posts: 12,928
Blog Entries: 2

Rep: Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301Reputation: 1301
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,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
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
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682Reputation: 682
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,248
Blog Entries: 8

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


Reply



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

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:31 PM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration