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? |
what have you tried?
|
Indeed you are asking the program for an editor dude... Upto what extent you have tried....?
|
I have zero knowledge of awk programming.
Simple shell prog.'s nt working. :( |
Quote:
|
You can probably easily do this with awk, but we need to see before and after example.
|
1. You have to state the problem clearly and completely. e.g.
Quote:
2. What documentation for SED and AWK have you looked at? Quote:
|
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 |
so where's your script that is not working?
|
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....... |
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 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 |
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 |
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. |
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. |
using sed:
Code:
sed -i '23s@\(....\)....@\1wxyz@' FILE |
leena*;
What books or tutorials are you using? e.g.: Have you found the excellent tutorials here?: http://www.grymoire.com/Unix/ |
Hi,
I've no clue about the sed command... :( can someone help in this? |
leena*;
So far, a lot of the advice has been about AWK---but there was one suggestion using SED---Did you try it? Did you try any of the AWK suggestions? Did you look at the tutorials I recommended? What exactly is it that you do not understand? |
As i've already mentioned in my last post (01-25-10, 03:52 PM ), I've created an awk command & tried it.
Suppose following is a flat file: HDR10005C100000.50ACA DTA10005A100500.50Abc DTA10003C100000.50AbA DTA10003A100700.50AbB There are no field separators (no comma or spaces are there). We have a fixed location say 3rd row, 8th column which we need to temper every time. So I wanted to create a shell script which will edit the 3rd row, 8th column of that particular file, with user provided data. So after editing, the above file will look like: HDR10005C100000.50ACA DTA10005A100500.50Abc DTA1000xC100000.50AbA DTA10003A100700.50AbB I used the following awk command: awk -v r=3 -v c=8 -v val=x 'BEGIN {}; NR != r {print}; NR==r{$c=val; print}' Leena_New where Leena_New is the name of the file. But this is trying to edit the 8th field instead of 8th column. It will be great if you could provide any help. |
leena*;
It is hard to help when you do not directly respond to our questions..... What books and tutorials are you using? Did you look at the grymoire site that I suggested? If you are trying to do this with AWK (as people have suggested), then what it the relevance of your question about SED? Did you try the suggested SED solution? Did it work? Finally, in your post above, what is the distinction between "field" and "column"? (In most contexts, these terms would be synonymous) |
What books and tutorials are you using?
I don't have any books. Did you look at the grymoire site that I suggested? I'm looking into the tutorial. But it'll take time. I posted this to see if I could get any clue in the mean time. If you are trying to do this with AWK (as people have suggested), then what it the relevance of your question about SED? I've tried this with awk, bt it doesn't seems working. I asked about sed 'coz jschiwal (Moderator) & konsolebox (Senior Member) have suggested so. Did you try the suggested SED solution? Did it work? The suggestion is nothing particular. As I've no knowledge of sed I could nt modify it to my requirement. Finally, in your post above, what is the distinction between "field" and "column"? (In most contexts, these terms would be synonymous) By Field I mean --> there are many fields(columns) in a file that are comma/space separated. By 8th Column I mean the 8th character of that row. |
Quote:
I'm not good with AWK, but I will tell you that trying to adapt someone else's suggestions without reading the man pages, tutorials, etc. is not likely to be very rewarding. Quote:
In your example, you say it is trying to edit the 8th field---but there is only ONE field In addition to the Grymoire tutorials, please go to http://tldp.org and get a copy of the Bash Guide for Beginners |
Referring to the code I posted before:
Quote:
Code:
23 Code:
s Code:
@ Code:
\(....\) We can also change it to something like: Code:
\(.\{4\}\) Code:
.... Code:
@\1wxyz@ |
Quote:
|
Quote:
Code:
awk -vuserdata="x" 'BEGIN{OFS=FS=""} Code:
# ./shell.sh |
Quote:
It's just that---at least for my pea-brain---it is not intuitive to think of every letter in a string as a field |
Thanks for your suggestions.
I wrote the following code. "!/bin/ksh echo "Enter the Row Num" read RowNum echo "Enter Column No From" read ColNumFrom echo "Enter Column No To" read ColNumTo echo "Enter File Name" read FileName #\033[$RowNum;$ColNumFrom H #awk -v r=2 -v c=3 -v val=x 'BEGIN {NR!=r; NR==r {$c=val}}' Leena.csv #r=2 #c=3 sed ''"$RoWNum"'s@\(.\{'"$ColNumFrom"'\}\)..@\1wx@' Leena_New But its' throwing an error Syntax error at line 11 : `"' is not matched. I changed the sed command to sed '"$RoWNum"s@\(.\{"$ColNumFrom"\}\)..@\1wx@' Leena_New still the same error occured... I again changed the sed to sed ''$RoWNum's@\(.\{'$ColNumFrom'\}\)..@\1wx@' Leena_New Error persisted. :( I've removed "' from the script but still the error talks about it. I'am stuck at this error... |
try this one:
Code:
sed "${RoWNums}s@\\(.\\{$ColNumFrom\\}\\)..@\\1wx@" Leena_New Code:
echo sed "${RoWNums}s@\\(.\\{$ColNumFrom\\}\\)..@\\1wx@" Leena_New |
Thanks a lot!
I used sed "$RoWNum s@\(.\{$ColNumFrom\}\)..@\1wx@" Leena_New Its working fine. :) |
Hi,
I need your help again. I've one more requirment. We have to copy a the file into a directory called My_Dir But we don't have rights to copy a file in this particular dir (no rights of cp command on this dir). So we ftp it. File_Name = "My_File" ftp host.name login: Username password: PASSWORD cd /home/abc/My_Dir mput $File_Name But I want the shell script to do this task. I tried the following: 1) REM_USER=Username REM_PASS=PASSWORD ftp host.name quote USER $REM_USER quote PASS $REM_PASS 2) ftp host.name quote Username quote PASSWORD They bring me into ftp mode. But I'm unable to do anything in FTP mode through the shell. :( Please Help |
I've got the answer :)
Following script worked... #!/bin/ksh REM_USER="Username" REM_PASS="PASSWORD" REM_HOST="host.name" echo "Please Enter the File Name" read FILE_NAME ftp -nv <<EOF open $REM_HOST user $REM_USER $REM_PASS cd /abc/My_Dir mput $FILE_NAME EOF |
All times are GMT -5. The time now is 02:30 PM. |