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
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 "$"
Last edited by Scrutinizer; 01-23-2013 at 12:47 PM..
Reason: extra code tags
If you just want to add a $ after the 2nd colon in each line, try:
If you just want to do that on a line that starts with 123:abc: and only print lines that sed modifies, try:
If you just want to add a $ after the 2nd colon in each line, try:
If you just want to do that on a line that starts with 123:abc: and only print lines that sed modifies, try:
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:
Originally Posted by MadeInGermany
[CODE
sed -n '/123:abc:/ { s/:/,/g; s/,/,$/2p; }' file.txt [/CODE]
This looks like a home work exercise...
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
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.
Wow....that was quick..
Your 2nd syntax, is it something to be read as,
"find <123:abc:>" then replace 2nd occurance of ":" with ":$" ?
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.
Quote:
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.
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:
If you just want to do that on a line that starts with 123:abc: and only print lines that sed modifies, try:
Hello all,
i have a code in which when doing a for loop, i need to direct the output to two files, one just a single output, the other to always append (historical reasons).
So far i managed to do the following, which is working, but am still considering it as "dirty".
... (4 Replies)
Hi,
I am working on Sun Solaris 5.10 and want to direct the output from a disk space check script to an output file;
#!/bin/bash
CURRENT=$(df -k /log/logs | grep /log/logs | awk '{ print $5}' | sed 's/%//g')
THRESHOLD=30
if ; then
echo "Remaining free space is low" > output.txt
else... (10 Replies)
Hi, i'm trying to gather details from remote hosts and want them to be written to my local linux machine from where i'm using SSH. My command looks some thing like this
ssh -q remotehost 'bash -s' <command.txt
where command.txt is a file in my local machine containing
ps -ef |grep httpd |... (1 Reply)
I want change the file when the line contains $(AA) but NOT contains $(BB), then change $(AA) to $(AA) $(BB)
eg:
$(AA) something
$(AA) $(BB) something (7 Replies)
Hello all,
I have a requirement to transfer files to mainframe usinf NDM connect:direct. So can anybody provide me a sample shell script on how to call a connect;direct script by providing filename as a parameter please?
Thanks,
Ajay (0 Replies)
Hi all,
I have a script which checks on my jobs that run on some cluster.
The script, "script.sh", takes as an input the job-id for the job to checked. Sometimes I have 100s of jobs and I want to check them. I could put these job-ids into a file, each id in its own line.
The script would ask... (2 Replies)
I can run this from the command line:
scp -i identfile /path/file_to_send remotelogin@remotebox:/path_to_put_it/file_to_send
and I get:
file_to_send 100% |***************************************************************************| 0 00:00
but if I do:
scp -i identfile... (6 Replies)
Hi,
I have a script to compare 2 files.
file1=$1
file2=$2
num_of_records_file1=`awk ' END { print NR } ' $file1`
num_of_records_file2=`awk ' END { print NR } ' $file2`
i=1
while
do
sed -n "$i"p $file1 > file1_temp
sed -n "$i"p $file2 > file2_temp
diff file1_temp... (5 Replies)
Ok so i have this script and I dont know how to have the output go to a file and then email that file to someone.
#!/bin/ksh
print "AL"
print "AM"
print "AN"
print "RL\n"
nawk '/PROD/ {print $3, $2}' /home/user/switch_listtest | sort -k1,2
print "End of Report"
Thank you in... (2 Replies)
Is there any way to combine the following two statements into one? I can't figure out how to get expr to take input from the output of the awk call - I've tried piping the output of the awk call into the expr call, and tried using a 'Here' document, nothing seems to work.
export CNT=`wc -l... (4 Replies)