- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- File content checking and replacing it!
Operating System - HP-UX
1753501
Members
4882
Online
108794
Solutions
Forums
Categories
Company
Local Language
юдл
back
Forums
Discussions
Forums
- Data Protection and Retention
- Entry Storage Systems
- Legacy
- Midrange and Enterprise Storage
- Storage Networking
- HPE Nimble Storage
Discussions
Discussions
Discussions
Forums
Forums
Discussions
юдл
back
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
Discussion Boards
Discussion Boards
Blogs
Information
Community
Resources
Community Language
Language
Forums
Blogs
Go to solution
Topic Options
- 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
тАО11-21-2004 05:57 PM
тАО11-21-2004 05:57 PM
I have a file which contain the line
...
...
"-"
...
...
I need to get the "-" part to be passed to a variable ad subsequently replaced it by "9999". How can i get it in a single command?
Maximum points for correct reply!
...
...
...
...
I need to get the "-" part to be passed to a variable ad subsequently replaced it by "9999". How can i get it in a single command?
Maximum points for correct reply!
Solved! Go to Solution.
3 REPLIES 3
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2004 06:10 PM
тАО11-21-2004 06:10 PM
Solution
A variable to the changing process, or to the calling shell?
# perl -pi -e's{"(.*?)" }{"9999" } and $perl_var = $1' file
would change the content of file and have the variable available just after the change in perl as $perl_var
If it is only one line that is changed, and you want to promote the variable to the shell, you need to catch it and set it
# MY_VAR=`(perl -pi -e's{<(SUBID)>"(.*?)"}{<$1>"9999"} and print STDERR "$2\n"' file) 2>&1`
As a proof of concept:
--8<---
sh-2.05b$ MY_VAR=2
sh-2.05b$ echo $MY_VAR
2
sh-2.05b$ cat file
Line 1
Line 3
"1234"
Line 6
sh-2.05b$ MY_VAR=`(perl -pi -e's{<(SUBID)>"(.*?)"}{<$1>"9999"} and print STDERR "$2\n"' file) 2>&1`
sh-2.05b$ echo $MY_VAR
1234
sh-2.05b$ cat file
Line 1
Line 3
"9999"
Line 6
sh-2.05b$
-->8---
Enjoy, Have FUN! H.Merijn
# perl -pi -e's{
would change the content of file and have the variable available just after the change in perl as $perl_var
If it is only one line that is changed, and you want to promote the variable to the shell, you need to catch it and set it
# MY_VAR=`(perl -pi -e's{<(SUBID)>"(.*?)"}{<$1>"9999"} and print STDERR "$2\n"' file) 2>&1`
As a proof of concept:
--8<---
sh-2.05b$ MY_VAR=2
sh-2.05b$ echo $MY_VAR
2
sh-2.05b$ cat file
Line 1
Line 3
Line 6
sh-2.05b$ MY_VAR=`(perl -pi -e's{<(SUBID)>"(.*?)"}{<$1>"9999"} and print STDERR "$2\n"' file) 2>&1`
sh-2.05b$ echo $MY_VAR
1234
sh-2.05b$ cat file
Line 1
Line 3
Line 6
sh-2.05b$
-->8---
Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2004 06:49 PM
тАО11-21-2004 06:49 PM
Re: File content checking and replacing it!
For the replacing you simply can use sed:
sed 's|".*" |"9999" '
But since you also want the old value in a variable, it gets trickier.
You could do something like this:
oldvalues=$(sed -n '/".*"<\/SUBID>/s|.*"\(.*\)" .*|\1|p' input)
sed 's|".*" |"9999" |' input > output
This means, of course, that you run twice through the input file with sed...
sed 's|
But since you also want the old value in a variable, it gets trickier.
You could do something like this:
oldvalues=$(sed -n '/
sed 's|
This means, of course, that you run twice through the input file with sed...
Every problem has at least one solution. Only some solutions are harder to find.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
тАО11-21-2004 07:40 PM
тАО11-21-2004 07:40 PM
Re: File content checking and replacing it!
Thanks for that quick reply.
The opinions expressed above are the personal opinions of the authors, not of Hewlett Packard Enterprise. By using this site, you accept the Terms of Use and Rules of Participation.
News and Events
Support
© Copyright 2024 Hewlett Packard Enterprise Development LP