- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - Linux
- >
- file manipulation
Categories
Company
Local Language
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Forums
Discussions
Discussions
Discussions
Forums
Discussions
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
- BladeSystem Infrastructure and Application Solutions
- Appliance Servers
- Alpha Servers
- BackOffice Products
- Internet Products
- HPE 9000 and HPE e3000 Servers
- Networking
- Netservers
- Secure OS Software for Linux
- Server Management (Insight Manager 7)
- Windows Server 2003
- Operating System - Tru64 Unix
- ProLiant Deployment and Provisioning
- Linux-Based Community / Regional
- Microsoft System Center Integration
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Discussion Boards
Community
Resources
Forums
Blogs
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2006 08:01 PM
08-08-2006 08:01 PM
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
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2006 08:17 PM
08-08-2006 08:17 PM
Re: file manipulation
awk/sed is probably part of the solution, but could you please provide more information or, even better, an example.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-08-2006 08:37 PM
08-08-2006 08:37 PM
Re: file manipulation
You can use perl for file manipulation.
-Arun
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 12:27 AM
08-09-2006 12:27 AM
Re: file manipulation
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.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 01:14 AM
08-09-2006 01:14 AM
SolutionPerhaps 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...
- Tags:
- Perl
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 12:18 PM
08-09-2006 12:18 PM
Re: file manipulation
You got me.
Thanks a lot for the great help.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 12:37 PM
08-09-2006 12:37 PM
Re: file manipulation
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 12:51 PM
08-09-2006 12:51 PM
Re: file manipulation
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 01:25 PM
08-09-2006 01:25 PM
Re: file manipulation
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.
- Tags:
- quoting
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
08-09-2006 11:54 PM
08-09-2006 11:54 PM
Re: file manipulation
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 {
}
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...