LinuxQuestions.org
Visit Jeremy's Blog.
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 01-25-2010, 07:40 AM   #16
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743

leena*;
What books or tutorials are you using? e.g.: Have you found the excellent tutorials here?:
http://www.grymoire.com/Unix/
 
Old 01-27-2010, 07:21 AM   #17
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
Hi,

I've no clue about the sed command...
can someone help in this?
 
Old 01-27-2010, 07:28 AM   #18
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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?
 
Old 01-27-2010, 07:39 AM   #19
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 01-27-2010, 07:48 AM   #20
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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)
 
Old 01-27-2010, 08:00 AM   #21
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
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.
 
Old 01-27-2010, 08:16 AM   #22
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
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
 
Old 01-28-2010, 01:01 AM   #23
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
Referring to the code I posted before:
Quote:
Originally Posted by konsolebox View Post
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.
 
Old 01-28-2010, 03:10 AM   #24
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by pixellany View Post

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.

Last edited by ghostdog74; 01-28-2010 at 03:24 AM.
 
Old 01-28-2010, 03:14 AM   #25
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by leena_d View Post
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
 
Old 01-28-2010, 08:59 AM   #26
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Mint
Posts: 17,809

Rep: Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743Reputation: 743
Quote:
Originally Posted by ghostdog74 View Post
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
 
Old 01-29-2010, 01:17 AM   #27
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
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...
 
Old 01-29-2010, 01:27 AM   #28
konsolebox
Senior Member
 
Registered: Oct 2005
Distribution: Gentoo, Slackware, LFS
Posts: 2,248
Blog Entries: 8

Rep: Reputation: 235Reputation: 235Reputation: 235
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
 
Old 01-29-2010, 03:28 AM   #29
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

Original Poster
Rep: Reputation: 15
Thanks a lot!
I used

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

Its working fine.
 
Old 02-08-2010, 12:20 AM   #30
leena_d
LQ Newbie
 
Registered: Dec 2009
Posts: 26

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


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 03:07 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