Sponsored Content
Homework and Emergencies Emergency UNIX and Linux Support Read file and change a 0 to a 1 in output Post 302358085 by otheus on Thursday 1st of October 2009 10:13:52 AM
Old 10-01-2009
Quote:
I want it to write this info to the file, not to create a new one.
This doesn't happen. A new file is ALWAYS written and the old file replaced. This is simply the method used by operating systems for "text" files of any sort.

The second problem is that you want to check "if it doesn't exist" and then place it there. Well, you can't really do that in one pass. The best solution is to Put it there and remove any existing ones.

So:
Code:
awk '
   /<key>ExcludeSimpleHostnames</ { s=1; next; } 
   s && /<integer>[0-9]*</integer>/ { s=0; next;} 
   /<key>Proxies</ { p=1 } 
   p && /<\/array>/ {
                print; 
                print "<key>ExcludeSimpleHostnames</key>";
                print "<integer>1</integer>"; 
                p=0; next; } 
    { print; }'

If you want to use the integer value found in this proxy section, you're kind of screwed -- you have to either do backtracking or two passes or create a metastructure of all the data and write it out again to XML. That's because you want the ExcludeSimpleHostnames key to follow the array in the output, but the input, it might occur later.

Last edited by otheus; 10-01-2009 at 11:15 AM.. Reason: formatted code
 

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

read file and output message

hi, I have a baby.txt file with two type of message: xxxxxxxx is missing xxxxxxxxxxx is not missing xxxx is missing xxxxxxxx is not missing xxxxxxxx is not missing For the above, I need to read file and get all "xxxx is missing" and write into baby_missing.txt. If no message "xxxxx is... (12 Replies)
Discussion started by: happyv
12 Replies

2. Shell Programming and Scripting

Change output if file is empty

I'm very new to writing scripts, so here is my problem...I have the following code already written (in perl) system "rm file2"; open(FILE2, ">file2"); open(MYINPUTFILE, "file"); while(<MYINPUTFILE>) { my($line) = $_; chomp($line); print file2 "$line\n"; print... (2 Replies)
Discussion started by: ddrew78
2 Replies

3. Shell Programming and Scripting

Change file output format

I have a file which has following contents usmtnz-dinfsi19 62 61 18400 18800 99.7 usmtnz-dinfsi19 62 61 18400 18800 99.7 i want the o/p to be like date (7 Replies)
Discussion started by: fugitive
7 Replies

4. Shell Programming and Scripting

How to read file and only output certain content

Hi - I have a file containing data like :- cn=tommy,cn=users,c=uk passwordexpirydate=20100530130623z cn=jane,cn=users,c=uk passwordexpirydate=20100423140734z cn=michael,cn=users,c=uk passwordexpirydate=20100331020044z I want to end up with a file that looks like:-... (6 Replies)
Discussion started by: sniper57
6 Replies

5. UNIX for Dummies Questions & Answers

How to read entire output from a file?

Hello- I am trying to view a file which is quite large. However, whenever I do 'cat (file name)' it shows me just the half.. I am using Putty to access my server. Also, is it possible to edit a file from a unix system on a 'Gedit for Windows" text editor? Thanks (7 Replies)
Discussion started by: DallasT
7 Replies

6. Shell Programming and Scripting

Read file and for each line replace two variables, add strings and save output in another file

Hi All, I have a file, let's call it "info.tmp" that contains data like this .. ABC123456 PCX333445 BCD789833 I need to read "info.tmp" and for each line add strings in a way that the final output is put /logs/ua/dummy.trigger 'AAA00001.FTP.XXX.BLA03A01.xxxxxx(+1)' where XXX... (5 Replies)
Discussion started by: Andy_ARG
5 Replies

7. Shell Programming and Scripting

Bash Script Help...search then read from file: change text and pipe back...

Hello, I am trying to make a bash script that can pull data from a file and then change one part of said data. I want to search by username and pull the full line. That way there is a way to replace just one part of that line then return it back to the file. My Data is stored like: ... (1 Reply)
Discussion started by: serverfull
1 Replies

8. Shell Programming and Scripting

How to read each line from input file, assign variables, and echo to output file?

I've got a file that looks like this (spaces before first entries intentional): 12345650-000005000GL140227 ANNUAL HELC FEE EN 22345650-000005000GL140227 ANNUAL HELC FEE EN 32345650-000005000GL140227 ANNUAL HELC FEE EN I want to read through the file line by line,... (6 Replies)
Discussion started by: Scottie1954
6 Replies

9. Shell Programming and Scripting

Change the naming convention of the output file

Hi Currently we have nmon running on our Red hat Linux server. The ouput file is now coming with the naming convention as "servername_160321_0010.nmon". The output file naming convention has to be changed as "nmon_servername_daily_2016.03.21_00.00.00" How can we do it ? Any suggestions... (10 Replies)
Discussion started by: newtoaixos
10 Replies

10. Shell Programming and Scripting

Read a lis, find items in a file from the list, change each item

Hello, I have some tab delimited text data, file: final_temp1 aname val NAME;r'(1,) 3.28584 r'(2,)<tab> NAME;r'(3,) 6.13003 NAME;r'(4,) 4.18037 r'(5,)<tab> You can see that the data is incomplete in some cases. There is a trailing tab after the first column for each incomplete row. I... (2 Replies)
Discussion started by: LMHmedchem
2 Replies
FASTRM(1)						    InterNetNews Documentation							 FASTRM(1)

NAME
fastrm - Quickly remove a list of files SYNOPSIS
fastrm [-de] [-c|-cI] [-s|-sM] [-u|-uN] base-directory DESCRIPTION
fastrm reads a list of either file names or storage API tokens, one per line, from its standard input and removes them. Storage API tokens are removed via the SMcancel() interface. fastrm does not delete files safely or with an eye to security, but rather cuts every corner it can to delete files as fast as it can. It should therefore never be run on publically writable directories, or in any other environment where a hostile party may control the directory structure in which it is working. If a file name is not an absolute path name, it is considered to be relative to base-directory as given on the command line. The base- directory parameter must be a simple absolute pathname (it must not contain multiple consecutive slashes or references to the special directories "." or ".."). fastrm is designed to be faster than the typical "| xargs rm" pipeline when given a sorted list of file names as input. For example, fastrm will usually chdir(2) into a directory before removing files from it, meaning that if its input is sorted, most names passed to unlink(2) will be simple names. This can substantially reduce the operating system overhead from directory lookups. fastrm assumes that its input is valid and that it is safe to call unlink(2) on every file name it is given. As a safety measure, however, fastrm when running as root will check with stat(2) that a file name doesn't specify a directory before removing it. (In some operating systems, root is allowed to unlink directories, even directories which aren't empty, which can cause file system corruption.) The input to fastrm should always be sorted -- or even better be in the order file names are output by find(1) -- if speed is an issue and the input isn't solely storage API tokens. (It deals fine with unsorted input, but is unlikely to be any faster in that case than a simple "| xargs rm" command.) Sorting may even slightly speed up the removal of storage API tokens due to caching effects, since sorting will tend to keep all of the tokens from a particular storage method together. Various additional optimizations for removing files can be turned on and/or tuned with options (see below). Which options will be most effective depends heavily on the underlying structure of the file system, the way in which directories are stored and searched, and similar, often underdocumented, operating system implementation details. The more sophisticated the underlying operating system and file system, the more likely that it will already perform the equivalent of these optimizations internally. OPTIONS
-c[I] Controls when fastrm calls chdir(2). If the number of files to be unlinked from a given directory is at least I, then fastrm will change to that directory before unlinking those files. Otherwise, it will use either the absolute path names or a path name relative to the current directory (whichever is likely more efficient). The I parameter is optional; if just -c is given, -c1 is assumed, which will cause fastrm to always chdir before calling unlink(2). The default is -c3. Use -c0 to prevent fastrm from ever using chdir(2). -d Don't remove any files. Instead, print a list of the files that would be removed to standard output. Each line contains either the current directory of fastrm at the time it would do the unlink and the relative path name it would pass to unlink(2) as two fields separated by whitespace and a "/", the absolute path name (as a single field) that would be passed to unlink(2), or the string "Token" and the storage API token that would be removed. -e Treat an empty input file as an error. This is most useful when fastrm is last in a pipeline after a preceding sort(1) command, ensuring that fastrm will fail if the sort fails. -s[M] When -s is given and the number of files to remove in a directory is greater than M, rather than remove files in the order given, fastrm will open the directory and read it, unlinking files in the order that they appear in the directory. On systems with a per- process directory cache or that use a linear search to find files in a directory, this should make directory lookups faster. The M parameter is optional; if just -s is given, -s5 is assumed. When this option is in effect, fastrm won't attempt to remove files that it doesn't see in the directory, possibly significantly speeding it up if most of the files to be removed have already been deleted. However, using this option requires fastrm to do more internal work and it also assumes that the order of directory listings is stable in the presence of calls to unlink(2) between calls to readdir(3). This may be a dangerous assumption with some sophisticated file systems (and in general this option is only useful with file systems that use unindexed linear searches to find files in directories or when most of the files to be removed have already been deleted). This optimization is off by default. -u[N] Specifying this option promises that there are no symbolic links in the directory tree from which files are being removed. This allows fastrm to make an additional optimization to its calls to chdir(2), constructing a relative path using "../.." and the like to pass to chdir(2) rather than always using absolute paths. Since this reduces the number of directory lookups needed with deeply nested directory structures (such as that typically created by traditional news spool storage), it can be a significant optimization, but it breaks horribly in the presence of symbolic links to directories. When -u is given, fastrm will use at most N levels of ".." segments to construct paths. N is optional; if just -u is given, -u1 is assumed. This optimization is off by default. fastrm also accepts -a and -r options, which do nothing at all except allow you to say "fastrm -usa", "fastrm -usr", or "fastrm -user". These happen to often be convenient sets of options to use. EXIT STATUS
fastrm exits with a status of zero if there were no problems, and an exit status of 1 if something went wrong. Attempting to remove a file that does not exist is not considered a problem. EXAMPLES
fastrm is typically invoked by INN via expirerm(8) using a command like: fastrm -e <patharticles in inn.conf> < expire.list To enable all optimizations and see the affect on the order of removal caused by -s, use: fastrm -d -s -e -u <patharticles> < expire.list If your file system has indexed directory lookups, but you have a deeply nested directory structure, you may want to use a set of flags like: fastrm -e -u3 <patharticles> < expire.list to strongly prefer relative paths but not to use readdir(2) to order the calls to unlink(2). You may want to edit expirerm(8) to change the flags passed to fastrm. WARNINGS
fastrm cuts corners and does not worry about security, so it does not use chdir(2) safely and could be tricked into removing files other than those that were intended if run on a specially constructed file tree or a file tree that is being modified while it is running. It should therefore never be used with world-writable directories or any other directory that might be controlled or modified by an attacker. NOTES
fastrm defers opening the storage subsystem or attempting to parse any INN configuration files until it encounters a token in the list of files to remove. It's therefore possible to use fastrm outside of INN as a general fast file removal program. HISTORY
fastrm was originally written by <kre@munnari.oz.au>. This manual page was rewritten in POD by Russ Allbery <rra@stanford.edu> for InterNetNews. $Id: fastrm.pod 8576 2009-08-18 13:55:11Z iulius $ SEE ALSO
expirerm(8). INN 2.5.3 2009-09-11 FASTRM(1)
All times are GMT -4. The time now is 06:14 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy