How can I make sed
not append a newline character to an input stream that was missing one? If I can't, what's the quickest way to remove a newline character from a variable?
A code sample was requested in the comments, so here you go. Imagine a .csv
file that has no newline at the end. Pipe it into a script.
Then inside that script:
function foobarize() {
sed \
-e "s|foo|bar|g" \
$1
}
INPUT=`tee`
printf "$INPUT" | foobarize
If the input has no newline at the end, foobarize
will add a newline to it 100% of the time.
echo -n abc | foobarize
on a linux box. – John1024 Sep 09 '14 at 06:07printf %s "$(printf %s "$INPUT" | foobarize)"
< that works. – mikeserv Sep 09 '14 at 06:12sed
bangline, you know. i prefer to do everything possible at the start and then to pass off all of the args i need onto the next process - in a row. the shell is glue you know. its great at mangling arguments any which way y0u like. it sucks at storing/understanding them. – mikeserv Sep 10 '14 at 14:43getopts
,set
, andshift
while each parameter is stored in$[num]
, the total index count in$#
and the entire array in special parameters$*
and$@
. What you're using arebash
arrays - they don't work insh
. Likelyreadlink /bin/sh
will point tobash
on your machine. – mikeserv Sep 10 '14 at 16:11sh --version
reportsGNU bash, version 3.2.48(1)
. For now I'll change the bangline to bash. Regarding modularity, even in bash you can't export arrays which is why I was forced to make it all one script. However one script may be more CPU-efficient in my use case because git is multithreaded and will be running n number of instances of this script simultaneously. If it's spawning multiple shells due to each script calling multiple sub-scripts, I would expect that to slow things down. – CommaToast Sep 10 '14 at 17:46