- Community Home
- >
- Servers and Operating Systems
- >
- Operating Systems
- >
- Operating System - HP-UX
- >
- Using sed
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
12-04-2003 03:44 AM
12-04-2003 03:44 AM
VAR1=VALUE1
VAR1=VALUE1 #Some comment with a tab
VAR1=VALUE1 #Some comment with spaces
VAR11=VALUE11
VAR11=VALUE11 #Some comment with a tab
VAR11=VALUE11 #Some comment with spaces
I want to substitute all the VALUE1's with some other value say VALUE2, but without changing VALUE11.
Ideally the output file should be as follows
VAR1=VALUE2
VAR1=VALUE2 #Some comment with a tab
VAR1=VALUE2 #Some comment with spaces
VAR11=VALUE11
VAR11=VALUE11 #Some comment with a tab
VAR11=VALUE11 #Some comment with spaces
I tried using 'sed' with regular expressions but it doesnt seem to be working.
It would be great if somebody can enlighten me on this.
Cheers
Deepak
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 03:50 AM
12-04-2003 03:50 AM
Re: Using sed
Also, changing a variable for one doesnt affect the other variable.
I am really confused as to your logic, can you post again and explain further?
Changing a variable is simple, just change it from value1 to value2.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 03:51 AM
12-04-2003 03:51 AM
Re: Using sed
Thanks
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 04:04 AM
12-04-2003 04:04 AM
Re: Using sed
OK, assume there are 2 different name value pairs.
CONFIG_FILE=$CFG_FILE
CONFIG_FILE_INPUT=$CFG_FILE_INPUT
Now, through a script I want to change CFG_FILE to CFG_FILE_TEMP. That is, I am _not_ doing it manually :)
So, if I use 'sed' to replace CFG_FILE to CFG_FILE_TEMP, then both these lines will get affected, which is what I do _not_ want.
After replacing, I want the file to be:
CONFIG_FILE=$CFG_FILE_TEMP
CONFIG_FILE_INPUT=$CFG_FILE_INPUT
and _not_
CONFIG_FILE=$CFG_FILE_TEMP
CONFIG_FILE_INPUT=$CFG_FILE_TEMP_INPUT
I hope somebody understood my simple yet unique problem :)
Cheers
Deepak
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 04:09 AM
12-04-2003 04:09 AM
Re: Using sed
This will do. I think the gnu versions of sed support this.
HTH,
Umapathy
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 04:49 AM
12-04-2003 04:49 AM
Re: Using sed
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 04:53 AM
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 05:39 AM
12-04-2003 05:39 AM
Re: Using sed
sed 's/CFG_FILE/CFG_FILE_TEMP/g' < file.in > file.out
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 06:02 AM
12-04-2003 06:02 AM
Re: Using sed
Using sed, it would be something like this:
sed -e 's|CFG_FILE *$|CFG_FILE_TEMP|" -e "s|CFG_FILE\( *#.*\)$|CFG_FILE_TEMP\1|"
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 06:28 AM
12-04-2003 06:28 AM
Re: Using sed
\< is begin of word in vi/elvis/vim/some-versions-of-grep and \> is end of word
perl only has \b which drills down to a zero width asserion between \w (a word character) and \W|^|$ (a non-word character, begin of line, end of line)
Enjoy, Have FUN! H.Merijn
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 07:15 AM
12-04-2003 07:15 AM
Re: Using sed
I can not get an regexpr to match "end-of-line" or whitepace. The $ end-of-line anchor does not seem to cut it, The (|) alternate match does not seem to cut it.
Todd: Your suggestion will replace VALUE11 by VALUE21 which was explicitly described as to be avoided, otherwise Deepak would not have posted the question!
Elmar: No, \b is a boundary, not a character which is just as well otherwise it would be 'eaten'. COmpare to ^ and $ somewhat.
Deepak: for a general solution it would seem to me you need regular subexpression to anchor your target string and be able to get the anchors back in the output.
For example, is you'd use a simple:
sed 's/LUE1[[:blank:]]/LUE2/g' xxx
The result is: VAR1=VALUE2#Some
Instead of: VAR1=VALUE2 #Some
It was easy in Perl, but with sed to best I can come up with so far is not good enough:
sed -e 's/\([^[:alnum:]]\)VALUE1\([^[:alnum:]]\)/\1VALUE2\2/g' -e 's/VALUE1$/VALUE2/' xxx
Here the last part deals with the end-of-line occurence, the other part finds the target surrounded with non-alphanumerics, remembers those surroundings and uses them with the replace string. Yuck.
In grep you can use:
grep -E 'LUE1($|[ \t])' xxx
or:
grep -E 'LUE1($|[^[:alnum:]])' xxx
So how does one match on EOL or SPACE in sed?
The $ want to start a shell variable, and a simple escape (\) does not fix that I believe.
fwiw,
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 08:21 AM
12-04-2003 08:21 AM
Re: Using sed
This will work if you are explicit.
sed 's/CFG_FILE /CFG_FILE_TEMP/g' < file.in > file.out
Ensure there is a space after the CFG_FILE, this will only change the ones that are explicitly CFG_FILE and not the CFG_FILE_INPUT variable.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-04-2003 09:16 AM
12-04-2003 09:16 AM
Re: Using sed
nitpicking... nothing personal... just trying to help and all that!
Deepak was very explicit about the target potentially being followed by a space, tab or nothing.
It was implied, but not explicit, that he would not want to touch a line with 'VAR1=OTHERVALUE1 #differnt variable'
Cheers!
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2003 07:27 AM
12-05-2003 07:27 AM
Re: Using sed
-e 's/CFG_FILE$/CFG_FILE_TEMP/g' \
-e 's/CFG_FILE[ ]/CFG_FILE_TEMP/g'
where
. the first expression (-e) gets the end-of-line case
. the second expression (-e) gets the white-space (one or more tabs and/or blanks) and the [..] is entered in the script as a blank followed by a tab. On the command line, the tab can be entered as a ^V^I (ctl-V ctl-I) when in Posix/ksh/bash. In the Bourne shell, you can simply hit the tab key. I also noticed that you can simply hit the tab in HP-UX Posix/ksh shell. (I generally use ^V^I because it also works in Linux bash).
bv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-05-2003 03:24 PM
12-05-2003 03:24 PM
Re: Using sed
Fine... but does that not stripe a tab or space from the text once replaced? How do you propose to return that exact character?
Hein.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-07-2003 11:49 AM
12-07-2003 11:49 AM
Re: Using sed
# cat sed.dat
VAR1=VALUE1
VAR1=VALUE1 #Some comment with a tab
VAR1=VALUE1 #Some comment with a space
VAR11=VALUE11
VAR11=VALUE11 # Some comment with a tab
VAR11=VALUE11 # Some comment with a space
# sed \
-e 's/VALUE1\([ ]\)/VALUE2\1/g' \
-e 's/VALUE1$/VALUE2/' sed.dat
VAR1=VALUE2
VAR1=VALUE2 #Some comment with a tab
VAR1=VALUE2 #Some comment with a space
VAR11=VALUE11
VAR11=VALUE11 # Some comment with a tab
VAR11=VALUE11 # Some comment with a space
#
In HP posix sh you type in the actual tab char in the [...] char class.
Cheers,
JW.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-08-2003 12:20 AM
12-08-2003 12:20 AM
Re: Using sed
I missed that.
2 possibilities:
1. since we are finding whitespace, we *could* just replace it with a single space
sed \
-e 's/CFG_FILE$/CFG_FILE_TEMP/g' \
-e 's/CFG_FILE[ ]/CFG_FILE_TEMP /g'
2. better, we could just save the matching character
sed \
-e 's/CFG_FILE$/CFG_FILE_TEMP/g' \
-e 's/CFG_FILE\([ ]\)/CFG_FILE_TEMP\1/g'
The \(...\) saves the characters ... and they are "recalled" via \1.
If you have more than one \(...\) in the match string, they are recalled by occurrence in the replacement string:
\1 then \2 then \3 , etc.
bv
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
12-22-2003 01:10 AM
12-22-2003 01:10 AM
Re: Using sed
perl -p -i -e s/VAR1/VAR2/g
I first set the variables in my script
for example
export dist=elk
export train=train
export fin=fin
perl -p -i -e s/$dist$train/DBNAME/g dropit
where dropit is is an sql script..
#dropit
drop database DBNAME
Works great..
happy holidays all..