Help with tr


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help with tr
# 8  
Old 04-03-2013
Why not try the back door:-

Code:
#!/bin/bash

# Assume there IS a newline at the end also...
printf "abc\ndef\nghi\n" > /tmp/sometext
ls -l /tmp/sometext

# Important line, note backticks...
somestring=`cat /tmp/sometext`

# Voila trailing newline eliminated...
printf "$somestring" > /tmp/sometext
ls -l /tmp/sometext

# Now try your code, as I haven't... ;o)

Code:
Last login: Wed Apr  3 21:20:20 on ttys000
Barrys-MacBook-Pro:~ barrywalker$ ./simple_str.sh
-rw-r--r--  1 barrywalker  wheel  12  3 Apr 21:20 /tmp/sometext
-rw-r--r--  1 barrywalker  wheel  11  3 Apr 21:20 /tmp/sometext
Barrys-MacBook-Pro:~ barrywalker$

# 9  
Old 04-03-2013
Quote:
Originally Posted by Scrutinizer
@yoda, that would be problematic if there is any kind of whitespace other than the newlines. Even if you set FS to a newline, the RS= would compress multiple newlines and create multiple records.

As a work-around one could choose a character that will not occur in the text for example:
Code:
awk '{$1=$1}1' FS='\n' RS=§ OFS="|"

Note that there would need to be curly braces around $1=$1 otherwise it would not print in case of 0 or whitespace in $1

But this would leave a trailing pipe symbol after the last record, so that still would not work as desired...

Also there might be record length limitations..

The code doesn't work as intended.

Code:
awk '{$1=$1}1' FS='\n' RS=^ OFS="|" aa

output:

Quote:
abc
def
ghi
---------- Post updated at 03:42 PM ---------- Previous update was at 03:40 PM ----------

Quote:
Originally Posted by Scrutinizer
Try:
Code:
paste -sd \| in.txt


This works. Thanks!!
# 10  
Old 04-03-2013
@pinnacle. Glad the paste works. I am curious. What is your OS and version, where the awk snippet produced those result? AIX? There you would need:
Code:
awk '{$1=$1x}1' FS='\n' RS=^ OFS="|" file

Login or Register to Ask a Question

Previous Thread | Next Thread
Login or Register to Ask a Question