Manipulating sed Direct Input to Direct Output


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Manipulating sed Direct Input to Direct Output
# 1  
Old 01-23-2013
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 Smilie

Code:
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 "$" Smilie

Last edited by Scrutinizer; 01-23-2013 at 12:47 PM.. Reason: extra code tags
# 2  
Old 01-23-2013
Code:
awk -F: -v d="$" '/^123/{print $1","$2","d$3}' file

123,abc,$987

# 3  
Old 01-23-2013
If you just want to add a $ after the 2nd colon in each line, try:
Code:
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:
Code:
sed -n '/123:abc:/s/:/:$/2p' input_file

# 4  
Old 01-23-2013
Code:
sed 's/:/:$/2; s/:/,/g' file

Code:
awk '$3="$"$3' FS=: OFS=, file

# 5  
Old 01-23-2013
Code:
sed -n '/123:abc:/ { s/:/,/g; p; }' file.txt

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

With the stupid $ it can be
Code:
sed -n '/123:abc:/ { s/:/,/; s/:/,$/; p; }' file.txt

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

This looks like a home work exercise...

Last edited by MadeInGermany; 01-23-2013 at 01:26 PM.. Reason: Oh, missed the stupid $
# 6  
Old 01-23-2013
Quote:
Originally Posted by Don Cragun
If you just want to add a $ after the 2nd colon in each line, try:
Code:
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:
Code:
sed -n '/123:abc:/s/:/:$/2p' input_file

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 Smilie

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. Smilie

Last edited by the0nion; 01-23-2013 at 01:49 PM..
# 7  
Old 01-23-2013
Quote:
Originally Posted by the0nion
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:
Code:
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:
Code:
sed -n 's/:/,/;/123,abc:/s/:/,$/p' input_file

Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

For loop in bash - Direct output to two files

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)
Discussion started by: nms
4 Replies

2. Shell Programming and Scripting

Disk Space Script to direct output

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)
Discussion started by: SSKAAB
10 Replies

3. UNIX for Dummies Questions & Answers

Output of ssh command from localhost - direct to local file.

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)
Discussion started by: poga
1 Replies

4. Shell Programming and Scripting

sed substitution or awk, need to direct change the file

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)
Discussion started by: yanglei_fage
7 Replies

5. Shell Programming and Scripting

Connect:Direct

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)
Discussion started by: ajaykumar4534
0 Replies

6. Shell Programming and Scripting

Direct input to a script from a file

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)
Discussion started by: faizlo
2 Replies

7. Shell Programming and Scripting

how to direct scp output to a file in bash shell or script

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)
Discussion started by: NewSolarisAdmin
6 Replies

8. Shell Programming and Scripting

Direct the output of a script to a log file

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)
Discussion started by: autosys_nm
5 Replies

9. UNIX for Dummies Questions & Answers

direct output to a file then email it

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)
Discussion started by: llsmr777
2 Replies

10. Shell Programming and Scripting

How to direct awk output to expr?

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)
Discussion started by: jvander
4 Replies
Login or Register to Ask a Question