Manipulating sed Direct Input to Direct Output

Hi guys,
been scratching round the forums and my mountain of resources.
Maybe I havn't read deep enough

My question is not how sed edits a stream and outputs it to a file, rather something like this below:

I have a .txt with some text in it :rolleyes:

abc:123:xyz
123:abc:987
qwe:145:123

Is there a way, to use sed to get, say "123:abc:987", convert it to string "123,abc,$987" and standard output it to screen? No file overwrite allowed

I have tried things like
1) sed -i "s/:/,/g" file.txt --> this of course will change all ":" to ","
2) sed -n /123,abc,/p file.txt --> print only matching
3) sed -i "s/,/:/g" file.txt --> convert back to ":"

I have tried some piping but i think I may have the wrong idea of pipe (eg. "| echo" and wondering why no output)

Could you guys throw me some ideas?
Some of my other friends was mentioning AWK.
Note that stupid "$" :mad:

awk -F: -v d="$" '/^123/{print $1","$2","d$3}' file

123,abc,$987

If you just want to add a $ after the 2nd colon in each line, try:

sed 's/:/:$/2' input_file

If you just want to do that on a line that starts with 123:abc: and only print lines that sed modifies, try:

sed -n '/123:abc:/s/:/:$/2p' input_file
sed 's/:/:$/2; s/:/,/g' file
awk '$3="$"$3' FS=: OFS=, file
sed -n '/123:abc:/ { s/:/,/g; p; }' file.txt

or shorter

sed -n '/123:abc:/ s/:/,/gp' file.txt

With the stupid $ it can be

sed -n '/123:abc:/ { s/:/,/; s/:/,$/; p; }' file.txt

or

sed -n '/123:abc:/ { s/:/,/g; s/,/,$/2p; }' file.txt

This looks like a home work exercise...

Wow....that was quick..
Your 2nd syntax, is it something to be read as,
"find <123:abc:>" then replace 2nd occurance of ":" with ":$" ?

I just came up with another issue. the /I flag

I somehow cant seem to combine /I and /2p together.
I also need to change all my ":" to ",", just for output sake.

---------- Post updated at 01:43 AM ---------- Previous update was at 01:16 AM ----------

[quote= madeingermany

sed -n '/123:abc:/ { s/:/,/g; s/,/,$/2p; }' file.txt 

this looks like a home work exercise...[/quote]

Oh...i didn't know sed could "chain" commands together. I kept invoking new instances of sed.

Haiz...forced to pickup shell after assignment thrown to our face.
Just curious, how many flags can be set at the end?

A last requirement I have is de /I for "ignore case"...cant seem to chain that up with the main code above :confused:

EDIT: @_@ the /I flag was self-resolved. Could you tell me how it was resolved? or sed has /I on default?
EDIT: I had already changed to /123:abc:/I before the last edit

Thanks again for all the help guys. Gonna keep this on front page for studying. :slight_smile:

Yes. I would have stated how it behaves as: If a line contains the string "123:abc:" change the 2nd occurrence of ":" on that line to ":$" and then print the modified line.

The standards don't have an I flag for the sed s command. The Mac OS X system I use when testing stuff I post on this forum doesn't have an I flag for th sed s command. The Linux man sed man page in this forum doesn't mention any flags but says to look for another document that I don't have to determine how sed works on Linux.

If you can tell me what the I flag is supposed to do, I may be able to help, but I have no idea what it does nor why you would want to use it.

I missed the part about changing colons to commas. I still don't have a clear statement of what you're really trying to do. So, back to my original posting:

If you just want to add a $ after the 2nd colon and change colons to commas in each line, try:

sed 's/:/,/;s/:/,$/' input_file

If you just want to do that on a line that starts with 123:abc: and only print lines that sed modifies, try:

sed -n 's/:/,/;/123,abc:/s/:/,$/p' input_file

haha. yeah its quite messed up but thanks for helping a lot. Really helped :smiley:
The linux /I flag "ignores case-sensitiveness"
/123:abc/ will match /123:ABC/ as well...yup..