Don you are correct in that " fields that are separated by a colon and each line is terminated by a semicolon (followed by a <newline>)" but it may also be the case that some files are not terminated by a semicolon plus some files have in place of "field" (as above) have "field,value". So in this case the following is not true: "when you're searching for FRED6 , FRED61 should not be modified" i.e. it should be modified or rather removed as FRED61 is just another value. I don't like the way this is implemented, it would seem cleaner to me to have a file containing a list of fields to be removed and a script that operates on that file though this would be a harder script to create.
Don, sorry I don't get the code
printf '%s\n' "FRED6;"...etc
appears to print out everything regardless of whether sed is in place or not
You may notice that the last line of the printf statement ends with a |. The printf command is feeding sample data through a pipe into the sed command. It is just there to show that all occurrences of FRED6 and FRED6,string will be removed from the ouput if it appears at the start of a line followed by a colon or semicolon; from the end of a line followed by a semicolon (changing the preceding colon to a semicolon in this case) and from the middle of a line following and followed by other "names". It will NOT modify FRED61, or ALFRED6; only FRED6 and FRED6 immediately followed by a comma.
Changing my script to add an intermediate file and having sed read from that file involves the following huge changes: Take my original script:
and change it to:
Either way, the output produced is:
All occurrences of FRED61 are untouched; all occurrences of FRED6 (as a name by itself and as a name followed by a comma and a string of non-colon characters following the comma have been removed from the output.
The sample data produced by the printf command is:
All of the data in red is removed by the sed script.
PS This script still assumes that lines are terminated by a semicolon as was shown in the earlier sample lines of input you provided, which were:
If it is important to handle lines that don't end in a semicolon, I need to know whether (missing) semicolons should be added to the ends of lines that don't have them.
Last edited by Don Cragun; 07-02-2013 at 05:17 AM..
Reason: Add postscript about trailing semicolons.
There is nothing in the sed command I provided that shouldn't work on any system that provides a sed that meets POSIX requirements. The output I showed you was from tests I ran on OS X on a MacBook Pro laptop. While digging around this morning, I found a note in the Linux sed(1) man page:
Quote:
POSIX.2 BREs should be supported, but they aren't completely because of performance problems.
So, if you are using a Linux system, that might be the problem.
What does the command:
print on your system?
What shell are you using? If you don't know, show us the output from the command:
You didn't answer my question about trailing semicolons in your input: If a line in your input does not end with a semicolon, do you want this script to add one?
In your last post, you said:
Quote:
As for the files, format is either
Does that mean that ,value only appears on lines that end with a semicolon and that every field in a line that ends with a semicolon will have a ,value on every field in that line???
Using metanotation where everything between [ and ] is optional and where [stuff]... means that the optional stuff can appear zero or more times, is the following an accurate representation of your input file format requirements?:
where each occurrence of name is a string of one or more characters that are not colon, comma, or semicolon; and each occurrence of value is a string of zero or more characters that are neither colon nor semicolon?
Last edited by Don Cragun; 07-02-2013 at 04:05 PM..
Reason: Remove extra end CODE tag
No not Linux, using /bin/zsh on a Sun box uname -irs = SunOS 5.10 SUNW,Netra-T5220. Really do not understand why there should be a problem with this box:
printf '%s\n' "FRED6;" | sed '\!^FRED6\(,[^:]*\)\{0,1\};!d'
echo "FRED6;" | sed '\!^FRED6\(,[^:]*\)\{0,1\};!d'
all print FRED6;
Yes to = "Does that mean that ,value only appears on lines that end with a semicolon and that every field in a line that ends with a semicolon will have a ,value on every field in that line"
Nothing is to be added to the files - it's a straightforward removal of a variable, and its value if it has one. It looks like - thanks to your help - the script should work but I really don't like the use of wildcards and think using a file with a list of all the variables to be removed would be safer.
Do you get the same results if you specify /usr/xpg4/bin/sed instead of just sed in the script?
Many Solaris systems have the GNU utilities loaded into a directory and on those systems some users set their command search path to pick up the GNU utilities in preference to the "standard" utilities. What output do you get from the command line:
Absolutely amazing I'd no idea of this, you are absolutely correct, using /usr/xpg4/bin/sed in place of sed (which gives /bin/sed) works (btw also interesting there is no sed -version so seems you id what you are using...comes with the os maybe) :-
Moderator's Comments:
Please use CODE tags.
Last edited by Don Cragun; 07-04-2013 at 04:56 PM..
Reason: Add CODE tags
If your "default" sed had been a GNU version of sed, sed --version would have printed version information for that implementation of sed instead of a diagnostic saying it didn't know what --version meant. Since you are using Solaris 10 sed utilities, the command:
will probably show you SCCS version information for those two sed utilities. (Some sys admins strip this information when installing Solaris systems to save a little disk space.)
I am learning SED and just following the shell scripting book, i have trouble understanding the grep and sed statement,
Question : 1
__________
/opt/oracle/work/antony>cat teledir.txt
jai sharma 25853670
chanchal singhvi 9831545629
anil aggarwal 9830263298
shyam saksena 23217847
lalit... (7 Replies)
Hi,
I am running a script sample.sh in bash environment .In the script i am using sed and awk commands which when executed individually from terminal they are getting executed normally but when i give these sed and awk commands in the script it is giving the below errors :-
./sample.sh: line... (12 Replies)
My need is :
Want to change
docBase="/something/something/something"
to
docBase="/only/this/path/for/all/files"
I have some (about 250 files)xml files.
In FileOne it contains
<Context path="/PPP" displayName="PPP" docBase="/home/me/documents" reloadable="true" crossContext="true">... (1 Reply)
I'm not sure if the problem I'm seeing is an artifact of sed or simply a beginner's mistake. Here's the problem: I want to add a zero-width space following each underscore between XML tags. For example, if I had the following xml:
<MY_BIG_TAG>This_is_a_test</MY_BIG_TAG>
It should look like... (8 Replies)
Hi all,
First off, Thank you all for the knowledge I have gleaned from this site!
Deleting Records from a text file... sed paragraphs
The following code works nearly perfect, however each time it is run on the log file it adds a newline at the head of the file, run it 5 times, it'll have 5... (1 Reply)
i made a script to update a lot of xml files. to save me some time. Ran it and it replaced all the the files with a 0kb file. The problem i was having is that I am using sed to change xml node <doc_root>. The problem with this is it has a / in the closing xml tag and the stuff inside will also have... (4 Replies)
Hello and thx for reading this
I'm using sed to remove only the leading spaces in a file
bash-280R# cat foofile
some text
some text
some text
some text
some text
bash-280R#
bash-280R# sed 's/^ *//' foofile > foofile.use
bash-280R# cat foofile.use
some text
some text
some text... (6 Replies)
Write a sed script to extract the year, rank, and stock for the most recent 10 years available in the file top10_mktval.csv, and output in the following format:
------------------------------
YEAR |RANK| STOCK
------------------------------
2007 | 1 | Exxon... (1 Reply)