1827595 Members
2792 Online
109966 Solutions
New Discussion

file manipulation

 
SOLVED
Go to solution
Nisar Ahmad
Regular Advisor

file manipulation

Hi All

I want to pick a line from one file and overwite it onto another file in similar place in a SHELL scripting. Any clue please ?

Thanks in advance

Nisar

9 REPLIES 9
Peter Godron
Honored Contributor

Re: file manipulation

Nisar,
awk/sed is probably part of the solution, but could you please provide more information or, even better, an example.
Arunvijai_4
Honored Contributor

Re: file manipulation

Hi,

You can use perl for file manipulation.

-Arun
"A ship in the harbor is safe, but that is not what ships are built for"
Marvin Strong
Honored Contributor

Re: file manipulation

not sure what you want to do.
Do you want to grab the line from one file and insert it somewhere in the middle of the second file? If so perl would be my choice for that, awk could probably be used aswell.

If you just want to grab a line and append or overwrite an existing file you can just use grep and >> or > to accomplish that.
James R. Ferguson
Acclaimed Contributor
Solution

Re: file manipulation

Hi Nisar:

Perhaps something like this(?):

# cat /tmp/data
this is line-1 of 3
this is line-2 of 3
this is line-3 of 3

# perl -pi.old -e 's/.*line-2.*/NEWSTUFF/' /tmp/data

# cat /tmp/data
this is line-1 of 3
NEWSTUFF
this is line-3 of 3

A backup copy of the original file is automatically made as '/tmp/data.old' in this case. The line matching "line-2" was targeted for replacement in this example.

If you wanted to target line-2 without regard to it's contents, this variation would target and replace it:

# perl -pi.old -e '$.==2 && s/.*/NEWSTUFF/' /tmp/data

Regards!

...JRF...
Nisar Ahmad
Regular Advisor

Re: file manipulation

Hi Fergusan

You got me.

Thanks a lot for the great help.
Nisar Ahmad
Regular Advisor

Re: file manipulation

Hi Ferguson

Just want to clear that what is difference betveen two options ? What -p means ? How about if I want no copy of the file ?

Thanks

Nisar
Nisar Ahmad
Regular Advisor

Re: file manipulation

Hi Ferguson

I am having also dificulty how to use a value from a parameter for NEWSTUFF. Like if I read a value from somewhere and assign to a variable say REP then should it work like ( perl -pi -e 's/.*line-3.*/${REP}/' file ) ?

Regards

Nisar
Dennis Handly
Acclaimed Contributor

Re: file manipulation

>assign to a variable say REP then should it work like (perl -pi -e 's/.*line-3.*/${REP}/' file) ?

Shell variables aren't expanded within single quotes. You would have to use double quotes then escape any chars that are special to the shell. I don't see any above, so using "" would work.
James R. Ferguson
Acclaimed Contributor

Re: file manipulation

Hi (again) Nisar:

If you don't want a backup copy of your file as it exists before the replacements, drop the argument that follows the '-i' switch. That is, insteadnig of creting a backup copy of '/tmp/data' as '/tmp/data.old' in:

# perl -pi.old -e 's/.*line-2.*/NEWSTUFF/' /tmp/data

...use:

# perl -pi -e 's/.*line-2.*/NEWSTUFF/' /tmp/data

THe '-p' switch creates an implicit loop to read and print file input, like:

while (<>) {
... # do whatever you want here
}
continue {
print
}

Normally, the output of the print goes to STDOUT. The addition of the '-i' switch means that the output is automatically directed to the input file (normally) specified on the command line.

You also asked how to make the substitution variable at run time. Here's one way:

# cat /tmp/data
this is line-1 of 3
this is line-2 of 3
this is line-3 of 3

# perl -pi -e 'BEGIN{$this=shift};s/.*line-2.*/$this/o' YOURSTUFF /tmp/data

# cat /tmp/data
this is line-1 of 3
YOURSTUFF
this is line-3 of 3

...Now our one-line Perl script expects two arguments passed to it. The first argument is the replacement text and the second argument is the filename on which to operate.

Regards!

...JRF...