Issue piping TR to SED


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Issue piping TR to SED
# 1  
Old 12-09-2009
Question Issue piping TR to SED

Trying to get oracle column headings to CSV format without success.

Code:
{myprompt}$ sqlplus -S user/pass <<EOF | tr '\n' ','
SET echo off heading off feedback off pages 0
SELECT column_name
FROM cols
WHERE table_name = 'MY_TABLE'
ORDER BY column_id;
QUIT
EOF
COLUMN1,COLUMN2,COLUMN3,{myprompt}$

Note the expected yet unwanted trailing comma. Adding a simple sed command unexpectedly produces no output. Any workarounds?

Code:
{myprompt}$ sqlplus -S user/pass <<EOF | tr '\n' ',' | sed 's/.$//'
SET echo off heading off feedback off pages 0
SELECT column_name
FROM cols
WHERE table_name = 'MY_TABLE'
ORDER BY column_id;
QUIT
EOF
{myprompt}$

# 2  
Old 12-09-2009
Code:
sed 's/,$//'

Just a guess, though Smilie
# 3  
Old 12-11-2009
Thanks Doc, unfortunately my issue is not with the sed command itself (yours or mine would suffice), but that tr does not seem to pipe its output as expected. Unless anyone has any fancy ideas, I'm gonna have to do this in two steps. :-(
# 4  
Old 12-11-2009
it works perfect in my case ... there seems no issue with tr or sed ?! you may have one in the other..

Code:
cat | tr '\n' ','
COLUMN1
COLUMN2
COLUMN3
COLUMN1,COLUMN2,COLUMN3,15:15:51 : tmp :
15:15:52 : tmp :cat | tr '\n' ',' | sed 's/.$//'
COLUMN1
COLUMN2
COLUMN3
COLUMN1,COLUMN2,COLUMN315:16:07 : tmp :

# 5  
Old 12-11-2009
MySQL

Nope, definitely something wrong with tr my logic (see post below)! Regardless, I have a workable solution (albeit with an error that can be redirected away from view) using a different tr implementation that'll suffice:

Code:
{myprompt}$ cat whatever | tr '\n' ',' | /usr/xpg4/bin/sed 's/.$//'
sed: Missing newline at end of file standard input.
COLUMN1,COLUMN2,COLUMN3
{mypropmt}$ cat whatever | tr '\n' ',' | /usr/xpg4/bin/sed 's/.$//' 2>/dev/null
COLUMN1,COLUMN2,COLUMN3

Thanks to those who helped out!

Last edited by cs03dmj; 12-11-2009 at 11:51 AM..
# 6  
Old 12-11-2009
nothing wrong with 'tr' in what you are doing.

You've told 'tr' to replace '\n' with ',' so you don't have any
newlines.

The problem you are running into is that sed requires a newline, or
it will not process the data.

---------- Post updated at 12:45 PM ---------- Previous update was at 11:03 AM ----------

This works for me, though there's probably a better way:

First the test data:
Code:
(echo col1;echo col2;echo col3)
col1
col2
col3

Which mimicks what you'll get from your sql statement.

Next the sed command:

Code:
(echo col1;echo col2;echo col3) | sed -ne 'H
$ {
x;s/^\n//,s/\n/,/g;p
}'

Basically you put every line into the hold buffer. On the last line indicated by '$' address
you exchange the hold buffer with the pattern space, get rid of the first newline as it precedes 'col1' and then change all other newlines to ',' and print.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Abnormality while piping tr command output to sed

i have a file seperated each line seperated by newline. For example alpha beta gamma i am trying to replace the newlines to "," but dont want , present at the end of the line so i am trying the below one liner . but not sure whats wrong but its not working cat myfile | tr -s '\n' ',' | sed... (9 Replies)
Discussion started by: chidori
9 Replies

2. Shell Programming and Scripting

sed issue

I'm trying to change a date in a couple of large files using SED. The problem is when I use the -n parameter, it doesn't actually change the file. When I leave out the -n, it sends the whole file to the screen, but it does appear to change it. The problem is, these files are very large and it... (8 Replies)
Discussion started by: Drenhead
8 Replies

3. Shell Programming and Scripting

sed issue

I can't get this code to work, could I get some help... sed -i '' "s:${shLogpath1}${cell}:${shLogpath2}${cell}:g" test.txt Any Ideas, I think I need to separate the variables some how? (2 Replies)
Discussion started by: digitalviking
2 Replies

4. Shell Programming and Scripting

one more issue last- sed

hi i have following sed command this replaces "** in filename1 with octal value 007 filename2 when i put it in script it wont work but it works from command line my OS is sun OS ---------- Post updated at 06:38 PM ---------- Previous update was at 06:14 PM ---------- i... (10 Replies)
Discussion started by: er_zeeshan05
10 Replies

5. Shell Programming and Scripting

Sed Issue

Hi, I am trying to use 3 sed statements in a shell script, but it get foll error. sed : garbage after command. If I use only two sed statements, the script works well. Is there any restriction for sed usage or is there some catch which I am missing. Sample Script is as follows : ... (3 Replies)
Discussion started by: sameersalve
3 Replies

6. Shell Programming and Scripting

sed issue

Hi All I'm getting this error while executing a sed script sed: 0602-404 Function /</ i\ File from New Cube: cannot be parsed. sed "/</ i\ File from New Cube: />/ i\ File from Old Cube:" difference1.txt > Difference.txt I've a file like this < Y2008 Dec ..... .... ... 345 I want... (6 Replies)
Discussion started by: Celvin VK
6 Replies

7. Shell Programming and Scripting

Piping STDOUT as pattern to grep or sed

$>cat file.txt 123 d3 234 abc 3 zyf 23 124 def 8 ghi kz0 ... ... I have the following output on the screen through <some command>. $> <some command> abc def ghi ... ... I have to search for each of these patterns in the file.txt and print the lines in file.txt matching the... (4 Replies)
Discussion started by: VNR
4 Replies

8. UNIX for Dummies Questions & Answers

Using awk to get a line number to delete, piping through sed

Alright, I'm sure there's a more efficient way to do this... I'm not an expert by any means. What I'm trying to do is search a file for lines that match the two input words (first name, last name) in order to remove that line. The removal part is what I'm struggling with. Here is my code: echo... (4 Replies)
Discussion started by: lazypeterson
4 Replies

9. Shell Programming and Scripting

Issue with a sed one liner variant - sed 's/ ; /|/g' $TMP1 > $TMP

Execution of the following segment is giving the error - Script extract:- OUT=$DATADIR/sol_rsult_orphn.bcp TMP1=${OUT}_tmp1 TMP=${OUT}_tmp ( isql -w 400 $dbConnect_OPR <<EOF select convert(char(10), s.lead_id) +'|' + s.pho_loc_type, ";", s.sol_rsult_cmnt, ";", +'|'+ s.del_ind... (3 Replies)
Discussion started by: kzmatam
3 Replies

10. UNIX for Dummies Questions & Answers

SED Issue

Can anyone tell me ...on the below listed command cat /mnt/winbox/list_measurement/ds1c/ds1_f.rome_27A03A 2>> error_log | sed -e '1,3d;s/^/27A03A,/' | sed -e "s#\(.*\)#\1 ,$(date +%Y-%m-%d)#g" > /SBS/ds1_f.rome_27A03A The outcome is this: ,2005-08-29 Forestdale,3:02 am MON AUG 29,... (9 Replies)
Discussion started by: Redg
9 Replies
Login or Register to Ask a Question