LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   How can I use Shell script to edit a data at a particular location in a txt file? (https://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/)

pixellany 01-25-2010 07:40 AM

leena*;
What books or tutorials are you using? e.g.: Have you found the excellent tutorials here?:
http://www.grymoire.com/Unix/

leena_d 01-27-2010 07:21 AM

Hi,

I've no clue about the sed command... :(
can someone help in this?

pixellany 01-27-2010 07:28 AM

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?

leena_d 01-27-2010 07:39 AM

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.

pixellany 01-27-2010 07:48 AM

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)

leena_d 01-27-2010 08:00 AM

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.

pixellany 01-27-2010 08:16 AM

Quote:

Q: What books and tutorials are you using?
A: I don't have any books.

Q: Did you look at the grymoire site that I suggested?
A: 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.
The way this comes across is that you really don't want to put the effort into learning this stuff----that you would rather just have us give you the answer.

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:

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.
If there is no delimiter, then many programs (eg AWK) have no way of specifying position. You may want to look at things like "tr" or "cut".

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

konsolebox 01-28-2010 01:01 AM

Referring to the code I posted before:
Quote:

Originally Posted by konsolebox (Post 3839598)
using sed:
Code:

sed -i '23s@\(....\)....@\1wxyz@' FILE

Code:

23
That number specifies an address in the file. In this statement, it points to the 23rd row.
Code:

s
That command substitues values. Please see 'man sed' to know how the command works.
Code:

@
That one's the delimeter for the command. I use @ instead of /. If you look at the info about the s command of sed, / is the one that's always used.
Code:

\(....\)
That set matches the first four characters (. matches a single character so 4 of it should match 4. We place the dots around \( and \) so that we can use \1 to specify them back.

We can also change it to something like:
Code:

\(.\{4\}\)
to tell sed that . makes matches 4 times.

Code:

....
Now this will match the 4 characters that should be replaced.
Code:

@\1wxyz@
This should be the replacement for the first 8 characters. Remember that \1 will refer to the first 4.

ghostdog74 01-28-2010 03:10 AM

Quote:

Originally Posted by pixellany (Post 3842194)

If there is no delimiter, then many programs (eg AWK) have no way of specifying position.

that's so not true. setting FS to "" in (g)awk makes every character a field by itself. If you want position 8, then look for $8. likewise, using substr() as well.

ghostdog74 01-28-2010 03:14 AM

Quote:

Originally Posted by leena_d (Post 3842160)
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.

Code:

awk -vuserdata="x" 'BEGIN{OFS=FS=""}
NR==3{
    $8=userdata
}1' file

output

Code:

# ./shell.sh
HDR10005C100000.50ACA
DTA10005A100500.50Abc
DTA1000xC100000.50AbA
DTA10003A100700.50AbB


pixellany 01-28-2010 08:59 AM

Quote:

Originally Posted by ghostdog74 (Post 3843331)
that's so not true. setting FS to "" in (g)awk makes every character a field by itself. If you want position 8, then look for $8. likewise, using substr() as well.

My mistake---sorry

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

leena_d 01-29-2010 01:17 AM

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...

konsolebox 01-29-2010 01:27 AM

try this one:
Code:

sed "${RoWNums}s@\\(.\\{$ColNumFrom\\}\\)..@\\1wx@" Leena_New
sometimes it's better to use echo first to see if you're really getting the proper command
Code:

echo sed "${RoWNums}s@\\(.\\{$ColNumFrom\\}\\)..@\\1wx@" Leena_New

leena_d 01-29-2010 03:28 AM

Thanks a lot!
I used

sed "$RoWNum s@\(.\{$ColNumFrom\}\)..@\1wx@" Leena_New

Its working fine. :)

leena_d 02-08-2010 12:20 AM

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


All times are GMT -5. The time now is 08:36 PM.