A Shell Script Problem

A Shell Script Problem

am 02.12.2007 19:19:28 von amerar

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!

Re: A Shell Script Problem

am 02.12.2007 19:50:52 von Janis Papanagnou

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!

Re: A Shell Script Problem

am 03.12.2007 12:49:56 von Richard W

Janis Papanagnou wrote:
> 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!

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

Re: A Shell Script Problem

am 04.12.2007 00:11:25 von cfajohnson

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

Re: A Shell Script Problem

am 04.12.2007 02:01:50 von Edward Rosten

On Dec 3, 4:11 pm, "Chris F.A. Johnson" wrote:
> On 2007-12-03, Richard W wrote:
> ...
>
> > sed -i 's#12/01/2007#xx/xx/xxxx#' file
>
> 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

Re: A Shell Script Problem

am 04.12.2007 20:28:39 von cfajohnson

On 2007-12-04, Edward Rosten wrote:
> On Dec 3, 4:11 pm, "Chris F.A. Johnson" wrote:
>> On 2007-12-03, Richard W wrote:
>> ...
>>
>> > sed -i 's#12/01/2007#xx/xx/xxxx#' file
>>
>> The -i option is non-standard:
>
> 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
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