Sponsored Content
Top Forums Shell Programming and Scripting How to replace a range of text with sed or awk? Post 302307222 by TXTad on Wednesday 15th of April 2009 01:38:32 AM
Old 04-15-2009
I have the following file:

Code:
# Some comments
# S'more comments

[daemon]
TimedLoginEnable=true
TimedLoginDelay=10
TimedLogin=user1
# and maybe some other, unpredictable lines that I don't need or want

[security]

[xdmcp]

[gui]
...

I wish for it to become something like
Code:
# Some comments
# S'more comments

[daemon]
TimedLoginEnable=true
TimedLoginDelay=5
TimedLogin=user2

[security]

[xdmcp]

[gui]
...

So far, I have managed to come up with this awk script:
Code:
BEGIN {
    searching = 1; 
    done = 0
}
searching == 1 {
    print;
    if ($1 == "[daemon]") {
        searching = 0;
        print "TimedLoginEnable=true";
        print "TimedLoginDelay=5";
        print "TimedLogin=user2";
        print "";
    }
}
searching == 0 {
    if (done) {
        print;
    } else {
        getline;
        if (match($0, /^\[/)) {
            print;
            done = 1;
        }
    }
}

But I feel like the awk script should be more concise, and I also suspect that this can be done in sed, but I don't even know where to start on that one.

Thanks,
Tad

Last edited by TXTad; 04-15-2009 at 02:41 AM.. Reason: fixed code typo
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

sed/awk date range?

Hi, I am trying to grep out a date range in an access log file. I defined the date like so; DATE1=$(date --date '1 hour ago' '+%m/%d/%y:%H:%M:%S') DATE2=$(date '+%m/%d/%y:%H:%M:%S') Then I just used cat to get the hits to the url into a results.txt; touch /tmp/results.txt cat... (7 Replies)
Discussion started by: Epx998
7 Replies

2. Shell Programming and Scripting

using sed/awk to replace a block of text in a file?

My apologies if this has been answered in a previous post. I've been doing a lot of searching, but I haven't been able to find what I was looking for. Specifically, I am wondering if I can utilize sed and/or awk to locate two strings in a file, and replace everything between those two strings... (12 Replies)
Discussion started by: kiddsupreme
12 Replies

3. Shell Programming and Scripting

sed replace range of characters in each line

Hi, I'm trying to replace a range of characters by their position in each line by spaces. I need to replace characters 95 to 145 by spaces in each line. i tried below but it doesn't work sed -r "s/^(.{94})(.{51})/\ /" inputfile.txt > outputfile.txt can someone please help me... (3 Replies)
Discussion started by: Kevin Tivoli
3 Replies

4. UNIX for Dummies Questions & Answers

Using sed to replace a range of number

Trying to use SED to replace numbers that fall into a range but can't seem to get the logic to work and am wondering if SED will do this. I have a file with the following numbers 3 26 20 5. For the numbers that are greater than zero and less than 25, SED would add the word range after the... (7 Replies)
Discussion started by: jimmyf
7 Replies

5. Debian

Using awk and sed to replace text

Good Day Every one I have a problem finding and replacing text in some large files that will take a long time to manually edit. Example text file looks like this #Example Large Text File unix linux dos squid bind dance bike car plane What im trying to do is to edit all the... (4 Replies)
Discussion started by: linuxjunkie
4 Replies

6. Shell Programming and Scripting

Get range out using sed or awk, only if given pattern match

Input: START OS:: UNIX Release: xxx Version: xxx END START OS:: LINUX Release: xxx Version: xxx END START OS:: Windows Release: xxx Version: xxx ENDHere i am trying to get all the information between START and END, only if i could match OS Type. I can get all the data between the... (3 Replies)
Discussion started by: Dharmaraja
3 Replies

7. Shell Programming and Scripting

awk to print text in field if match and range is met

In the awk below I am trying to match the value in $4 of file1 with the split value from $4 in file2. I store the value of $4 in file1 in A and the split value (using the _ for the split) in array. I then strore the value in $2 as min, the value in $3 as max, and the value in $1 as chr. If A is... (6 Replies)
Discussion started by: cmccabe
6 Replies

8. Shell Programming and Scripting

sed or awk to remove specific column to one range

I need to remove specific column to one range source file 3 1 000123456 2 2 000123569 3 3 000123564 12 000123156 15 000125648 128 000125648 Output required 3 000123456 2 000123569 3 000123564 12 000123156 15 000125648 128 000125648 (6 Replies)
Discussion started by: ranjancom2000
6 Replies

9. UNIX for Beginners Questions & Answers

Sed/awk to delete a regex between range of lines

Hi Guys I am looking for a solution to one problem to remove parentheses in a range of lines. Input file module bist_logic_inst(a, ab , dhd, dhdh , djdj, hdh, djjd, jdj, dhd, dhp, dk ); input a; input ab; input dhd; input djdj; input dhd; output hdh; output djjd; output jdj;... (5 Replies)
Discussion started by: kshitij
5 Replies

10. UNIX for Beginners Questions & Answers

Help with awk or sed Command to Replace Text in Files

Hello Everyone, I have many files like so: file1.txt file2.txt file3.txt Within each file I have many lines of random text separated by commas like so: abcAAA,123,defAA,456777,ghiA,789 jklB,101,mnoBBB,11211,pqrB,13111 stuCC,415,vwxCCCC,161,yzaC,718 I am trying to use SED or AWK to... (4 Replies)
Discussion started by: D3U5X
4 Replies
gdmflexiserver(1)                                                  User Commands                                                 gdmflexiserver(1)

NAME
gdmflexiserver - runs flexible (on demand) X servers SYNOPSIS
gdmflexiserver [--authenticate] [--command=command] [--debug] [--monte-carlo-pi] [--no-lock] [--xnest] [gnome-std-options] DESCRIPTION
gdmflexiserver runs flexible (on demand) X servers. gdmflexiserver allows a user to log in once and then quits. This is useful if you are logged in as user A, and user B wants to log in quickly but user A does not wish to log out. The X server ensures that the virtual terminal switching works transparently. You can use gdmflexiserver to run a flexi-server as an Xnest, that is, an X server in a window. If more than one server is defined with flexible=true, a dialog is displayed that lists all available flexible servers.. OPTIONS
The following options are supported: --authenticate Authenticate before running --command. You can also use -a to specify this option. --command=commanSend the specified protocol command to GDM. You can also use -c to specify this option. --debug Debugging output. You can also use -d to specify this option. --monte-carlo-pi --no-lock Do not lock current screen. You can also use -l to specify this option. --xnest Xnest mode. You can also use -n to specify this option. gnome-std-optionStandard options available for use with most GNOME applications. See gnome-std-options(5) for more information. EXTENDED DESCRIPTION
gdmflexiserver accepts the following commands with the --command option: o ALL_SERVERS o AUTH_LOCAL o CLOSE o CONSOLE_SERVERS o FLEXI_XNEST o FLEXI_XSERVER o GREETERPIDS o QUERY_LOGOUT_ACTION o QUERY_VT o SET_LOGOUT_ACTION o SET_SAFE_LOGOUT_ACTION o SET_VT o UPDATE_CONFIG o VERSION Each command is described in detail in the following sections. ALL_SERVERS Description: List all servers, including console, remote, Xnest. This can be useful to figure out if the server you are on is managed by the gdm daemon, by seeing whether it is in the list. It is also somewhat like the 'w' command but for graphical sessions. Supported since: 2.4.2.96 Arguments: None Answers: o OK server;server... o server is display,logged-in-user o logged-in-user can be empty if no user is logged in. o ERROR error-number error-description 0 Not implemented 200 Too many messages 999 Unknown error AUTH_LOCAL Description: Set up this connection as authenticated for FLEXI_SERVER. All full-blown (non-Xnest) servers can be started only from users logged in locally. Here, GDM assumes only users logged in from GDM. Users must pass the xauth MIT-MAGIC- COOKIE-1 that they were passed before the connection is authenticated. Note: The AUTH LOCAL command requires the --authenticate option, although only FLEXI_XSERVER uses this currently. Supported since: 2.2.4.0 Arguments: xauth-cookie, in hex form with no 0x prefix Answers: o OK o ERROR error-number error-description 0 Not implemented 100 Not authenticated 200 Too many messages 999 Unknown error CLOSE Supported since: 2.2.4.0 Arguments: None Answers: None CONSOLE_SERVERS Description: List all console servers, useful for Linux mostly. Does not list XDMCP and Xnest non-console servers. Supported since: 2.2.4.0 Arguments: None Answers: o OK server;server... o server is display,logged-in-user,vt-or-xnest-display o logged-in-user can be empty if no user is logged in. o vt can be -1 if not known or not supported. For example, on non-Linux. o If the display is an Xnest display and is a console display (that is, an Xnest inside another console display), the display is listed. Instead of vt, lists the parent display in standard form. o ERROR error-number error-description 0 Not implemented 200 Too many messages 999 Unknown error FLEXI_XNEST Description: Start a new flexible Xnest server. Supported since: 2.3.90.4 Note: Supported an older version from 2.2.4.0, later 2.2.4.2, but since 2.3.90.4 you must supply four arguments or ERROR 100 will be returned. This will start Xnest using the XAUTHORITY file supplied and with the same uid as the owner of that file (and the same as you supply). You must also supply the cookie as the third argument for this display, to prove that you indeed are this user. Also, this file must be readable ONLY by this user, that is, have a mode of 0600. If all of these conditions are not met, ERROR 100 is returned. Note: The cookie should be the MIT-MAGIC-COOKIE-1, the first one that GDM can find in the XAUTHORITY file for this display. If that is not what you use, you should generate one first. The cookie should be in hex form. Arguments: display-to-run-on uid-of-requesting-user xauth-cookie-for-the-display xauth-file Answers: o OK display o ERROR error-number error-description 0 Not implemented 1 No more flexi servers 2 Startup errors 3 X failed 4 X too busy 5 Xnest can't connect 6 No server binary 100 Not authenticated 200 Too many messages 999 Unknown error FLEXI_XSERVER Description: Start a new X flexible server. Only supported on connection that passed AUTH_LOCAL. Supported since: 2.2.4.0 Arguments: xserver-type If no argument specified, starts the standard x server. Answers: o OK display o ERROR error-number error-description 0 Not implemented 1 No more flexi servers 2 Startup errors 3 X failed 4 X too busy 6 No server binary 100 Not authenticated 200 Too many messages 999 Unknown error GREETERPIDS Description: List all greeter pids so that one can send HUP to them to re-read config. Only the root user can do that. Supported since: 2.3.90.2 Arguments: None Answers: o OK pid;pid... o ERROR error-number error-description 0 Not implemented 200 Too many messages 999 Unknown error QUERY_LOGOUT_ACTION Description: Query which logout actions are possible. Only supported on connection that passed AUTH_LOCAL. Supported since: 2.5.90.0 Arguments: None Answers: o OK action;action... action is HALT, REBOOT, or SUSPEND. An empty list can also be returned if no action is possible. A '!' is appended to an action if it was already set with SET_LOGOUT_ACTION or SET_SAFE_LOGOUT_ACTION. Note that SET_LOGOUT_ACTION has precedence over SET_SAFE_LOGOUT_ACTION. o ERROR error-number error-description 0 Not implemented 100 Not authenticated 200 Too many messages 999 Unknown error QUERY_VT Description: Ask the daemon about the current virtual terminal. This is useful for logins that do not own /dev/console but are still console logins. Only supported on Linux currently, otherwise will get ERROR 8. This is also the way to query whether VT support is available in the daemon. Only supported on connection that passed AUTH_LOCAL. Supported since: 2.5.90.0 Arguments: None Answers: o OK vt-number o ERROR error-number error-description 0 Not implemented 8 Virtual terminals not supported 100 Not authenticated 200 Too many messages 999 Unknown error SET_LOGOUT_ACTION Description: Tell the daemon to halt, reboot, or suspend after slave process exits. Only supported on connection that passed AUTH_LOCAL. Supported since: 2.5.90.0 Arguments: action action is one of the following: o NONE: Set exit action to 'none' o HALT: Set exit action to 'halt' o REBOOT: Set exit action to 'reboot' o SUSPEND: Set exit action to 'suspend' Answers: o OK o ERROR error-number error-description 0 Not implemented 7 Unknown logout action, or not available 100 Not authenticated 200 Too many messages 999 Unknown error SET_SAFE_LOGOUT_ACTION Description: Tell the daemon to halt, reboot, or suspend after all users log out. If only one person logs out, this is the same as SET_LOGOUT_ACTION. Note that SET_LOGOUT_ACTION has precendence over SET_SAFE_LOGOUT_ACTION if set to something other than NONE. If no user is logged in, the action takes effect immediately. Only supported on connection that passed AUTH_LOCAL. Supported since: 2.5.90.0 Arguments: action action is one of the following: o NONE: Set exit action to 'none' o HALT: Set exit action to 'halt' o REBOOT: Set exit action to 'reboot' o SUSPEND: Set exit action to 'suspend' Answers: o OK o ERROR error-number error-description 0 Not implemented 7 Unknown logout action, or not available 100 Not authenticated 200 Too many messages 999 Unknown error SET_VT Description: Change to the specified virtual terminal. This is useful for logins that do not own /dev/console but are still con- sole logins. Only supported on Linux currently, otherwise will get ERROR 8. Only supported on connection that passed AUTH_LOCAL. Supported since: 2.5.90.0 Arguments: vt Answers: o OK o ERROR error-number error-description 0 Not implemented 8 Virtual terminals not supported 9 Invalid virtual terminal number 100 Not authenticated 200 Too many messages 999 Unknown error UPDATE_CONFIG Description: Tell the daemon to update the configuration of the specified key. Any user can request that values are re-read but the daemon caches the last date of the config file so a user cannot actually change any values unless they can write the config file. The following keys are currently supported: o security/AllowRoot (2.3.90.2) o security/AllowRemoteRoot (2.3.90.2) o security/AllowRemoteAutoLogin (2.3.90.2) o security/RetryDelay (2.3.90.2) o security/DisallowTCP (2.4.2.0) o daemon/Greeter (2.3.90.2) o daemon/RemoteGreeter (2.3.90.2) o xdmcp/Enable (2.3.90.2) o xdmcp/Port (2.3.90.2) o xdmcp/PARAMETERS (2.3.90.2) (pseudokey, all the parameters) o xdmcp/MaxPending o xdmcp/MaxSessions o xdmcp/MaxWait o xdmcp/DisplaysPerHost o xdmcp/HonorIndirect o xdmcp/MaxPendingIndirect o xdmcp/MaxWaitIndirect o xdmcp/PingIntervalSeconds (only affects new connections) o daemon/TimedLogin (2.3.90.3) o daemon/TimedLoginEnable (2.3.90.3) o daemon/TimedLoginDelay (2.3.90.3) o greeter/SystemMenu (2.3.90.3) o greeter/ConfigAvailable (2.3.90.3) o greeter/ChooserButton (2.4.2.0) o greeter/SoundOnLoginFile (2.5.90.0) o daemon/AddGtkModules (2.5.90.0) o daemon/GtkModulesList (2.5.90.0) Supported since: 2.3.90.2 Arguments: key, the base part of the key such as "security/AllowRemoteRoot" Answers: o OK o ERROR error-number error-description 0 Not implemented 50 Unsupported key 200 Too many messages 999 Unknown error VERSION Description: Query version Supported since: 2.2.4.0 Arguments: None Answers: o GDM gdm-version o ERROR error-number error-description 200 Too many messages 999 Unknown error EXAMPLES
Example 1: To See Console GDM Version example% gdmflexiserver --command=VERSION GDM 2.6.0.2 Example 2: To See All Servers example% gdmflexiserver --command=ALL_SERVERS OK :0,username Example 3: To See Console Servers example% gdmflexiserver --command=CONSOLE_SERVERS OK :0,username,-1 FILES
The following files are used by this application: /usr/bin/gdmflexiserver Executable for flexible (on demand) X servers ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWgnome-display-mgr | +-----------------------------+-----------------------------+ |Interface stability |External | +-----------------------------+-----------------------------+ SEE ALSO
Latest version of the GNOME Desktop User Guide for your platform. gdm(1), gdmXnestchooser(1), gdmphotosetup(1), gdmsetup(1), gdmthemetester(1), gdm-restart(1m), gdmconfig(1m), gnome-std-options(5) NOTES
Original man page written by Martin K. Petersen <mkp@mkp.net>, George Lebl <jirka@5z.com>. Copyright (c) 1998, 1999 by Martin K. Petersen. Copyright (c) 2001, 2003, 2004 by George Lebl. Copyright (c) 2003 by Red Hat, Inc. Updated by Brian Cameron, Sun Microsystems Inc., 2004. SunOS 5.10 1 Sep 2004 gdmflexiserver(1)
All times are GMT -4. The time now is 11:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy