So I have this sed command below. The content of the tmp.txt file is
Which produces
dv01,at02:at04
and I'm trying to use sed to get me
Stripping out the parts leading with ":". My sed is pretty basic, can anyone see at a glance how to fix the above command?
A bit of explanation as to why yours wasn't behaving as desired. First, you don't need to escape the colon or comma which makes it easier to read:
Regular expression matching is greedy so "colon followed by zero or more characters (.*) followed by a comma will match
(all the way through the last comma) and not the desired dv01:at01.
I prefer to avoid using back references (\1 in the previous example) as they are fairly inefficient. This will work and should be more efficient:
The code matches a colon, then all characters that are not a comma up to the next comma (just what we want to match) and replaces it with a comma. The last replacement handles the case where the pattern is at the end of the line (no trailing comma).
I do observe that the other example is removing the characters before the colon, and your example indicates that you want everything between colon and comma removed. My example removes the :yyyy from xxxx:yyyy, which is what I assumed you wanted.
Last edited by agama; 05-10-2011 at 10:16 PM..
Reason: Clarification
...for the agama's input "dv01:at01,at05:xxxx,at02:at04" ...
Oops. I hadn't realised that I had left in that :xxxx. I was using that as a test to ensure that if there were such a pattern that it would do the job in addition to the original request.
Quote:
Originally Posted by J-Man
But I also found this works pretty good.
Yep, that'll work. Your need (something quick to pass over a static set of data, or something that will be used on an ongoing basis) will likely influence what you write in the future. I prefer generic solutions especially if the script will be used on an ongoing basis. Your alternative would be very quickly implemented if you just needed it for a one time shot.
How to work x in sed command?
I know x command is swaps the contents of pattern space and hold space. But i am unable to understand it's working? (4 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)
Hey all,
so I've been experimenting with SED today, no experience before today, so if you're not patient, stop reading now! :P
I will attempt to explain this as simply as possible, without having to post massive walls of shitty code. Basically, I've created a small sed script to go through an... (9 Replies)
Hi All,
I am trying to replace a string in a oracle LDT file file extension .ldt.
I am using the following command:
sed "s/Test Number Viewer/Test Number 1/g" TEST_LIEN_FORM.ldt > TEST_LIEN_FORM.ldt.tmp
mv TEST_LIEN_FORM.ldt.tmp TEST_LIEN_FORM.ldt
But after the command, the file... (2 Replies)
for j in $(cat ${list_B})
do
to_replace_2=$(grep $j ${useralias}_2)
sed "s/^${j}/${to_replace_2}/p" ${entries} > ${entries}_2
mv ${entries}_2 ${entries}
done
Hi,
I've the above sed command running in a script. Its basically looping through a file and replacing its beginning of line... (8 Replies)
Hi, I'm trying to find the first field in a text file with the below sed command but it doesn't seem to be correct for running on Solaris.. It has no problem running on AIX. Anyone got a suggestion what the problem is?
sed 's/^\(\+\) /OK/'
The eventual goal is to separate the columns in a... (5 Replies)
Hi guys,
I'm currently trying to understand a piece of shell script and it has some sed commands. I've been looking through sed tutorials to figure out what it does but still no luck :confused:
Can any of you guys tell me what this particular command does?
sed -i '1i\.options' a/*
... (1 Reply)
Okay, title is kind of confusion, but basically, I have a lot of scripts on a server that I need to replace a ps command, however, the new ps command I'm trying to replace the current one with pipes to sed at one point. So now I am attempting to create another script that replaces that line.
... (1 Reply)
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)
Hello,
I have the following line in one of my shell scripts. It works fine when the search string($SERACH_STR) exists in the logfile($ALERTLOG) but if the search string does not exist this line errors out at run time. Is there a way to make this line return 0 if it is not able to find the... (4 Replies)