Unix Shell - A Shell Script Problem

This is Interesting: Free IT Magazines  
Home > Archive > Unix Shell > December 2007 > A Shell Script Problem





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author A Shell Script Problem
amerar@iwc.net

2007-12-02, 1:29 pm

Hi Everyone,

I just had a question, can I, in a shell script, write certain lines
into the file? So, let's say I have a file that looks like this:

STATUS:F
LAST DATE: 12/01/2007
DATA PATH:/u01/data/indata
RUN DATE: 11/27/2007

And, let's say I use a grep/awk to retrieve the line I am interested
in. Can I also write that file back to the file? I want to write the
new value to the line I selected.

I know Perl, but I'd rather not write a seperate script just to write
a value to the file.......

Thanks!
Janis Papanagnou

2007-12-02, 1:29 pm

amerar@iwc.net wrote:
> Hi Everyone,
>
> I just had a question, can I, in a shell script, write certain lines
> into the file?


You want to modify/update your data? Typically you would create the
new data and overwrite the original file if everything went okay.

somecmd oldfile >newfile && mv newfile oldfile

....is a pattern for some command/script "somecmd" you might use.

> So, let's say I have a file that looks like this:
>
> STATUS:F
> LAST DATE: 12/01/2007
> DATA PATH:/u01/data/indata
> RUN DATE: 11/27/2007
>
> And, let's say I use a grep/awk to retrieve the line I am interested
> in. Can I also write that file back to the file? I want to write the
> new value to the line I selected.


For example...

awk '/^LAST DATE:/ { $NF = 30/01/2007 } { print }' oldfile >newfile &&
mv newfile oldfile

....would replace the date value of the "LAST DATE" records while keeping
other lines as they have been.

If that's not what you want give us some more precise description what
you want to achieve, and provide examples of origial data and expected
output for that data.

Janis

>
> I know Perl, but I'd rather not write a seperate script just to write
> a value to the file.......
>
> Thanks!

Richard W

2007-12-03, 7:33 am

Janis Papanagnou wrote:[vbcol=seagreen]
> amerar@iwc.net wrote:
>
> You want to modify/update your data? Typically you would create the
> new data and overwrite the original file if everything went okay.
>
> somecmd oldfile >newfile && mv newfile oldfile
>
> ...is a pattern for some command/script "somecmd" you might use.
>
>
> For example...
>
> awk '/^LAST DATE:/ { $NF = 30/01/2007 } { print }' oldfile >newfile &&
> mv newfile oldfile
>
> ...would replace the date value of the "LAST DATE" records while keeping
> other lines as they have been.
>
> If that's not what you want give us some more precise description what
> you want to achieve, and provide examples of origial data and expected
> output for that data.
>
> Janis
>

Janis has already shown the common way:

somecmd oldfile >newfile && mv newfile oldfile

But if you just want to modify the data slightly, you can use "sed -i".

For example,

sed -i 's#12/01/2007#xx/xx/xxxx#' file
Chris F.A. Johnson

2007-12-03, 7:25 pm

On 2007-12-03, Richard W wrote:
....
> sed -i 's#12/01/2007#xx/xx/xxxx#' file


The -i option is non-standard:

$ sed -i 's#12/01/2007#xx/xx/xxxx#' file
sed: unknown option -- i
usage: sed [-aEn] script [file ...]
sed [-aEn] [-e script] ... [-f script_file] ... [file ...]

--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Edward Rosten

2007-12-03, 7:25 pm

On Dec 3, 4:11 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
> On 2007-12-03, Richard W wrote:
> ...
>
>
> The -i option is non-standard:


It's a gnu sed option. If you want to do sed like things on a file in
place, ed or ex are suitable and similar. I think they historically
share some significant roots.


-Ed
Chris F.A. Johnson

2007-12-04, 7:25 pm

On 2007-12-04, Edward Rosten wrote:
> On Dec 3, 4:11 pm, "Chris F.A. Johnson" <cfajohn...@gmail.com> wrote:
>
> It's a gnu sed option.


It's also in FreeBSD sed, which, unlike GNU sed, requires a backup
suffix if you use the -i option.

> If you want to do sed like things on a file in place, ed or ex are
> suitable and similar.


...but don't have the safety valve that's available with sed -i.

> I think they historically share some significant roots.



--
Chris F.A. Johnson, author <http://cfaj.freeshell.org/shell/>
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence
Sponsored Links






Free braindumps | Software forum | Database administration forum

Copyright 2003 - 2008 webservertalk.com