sed append text after match - sed

I am writing a shell script to be able to append text after the match is found in a file
for example, in ~/.bash_profile file for the following line
PATH=$PATH:$HOME/bin
we need to append it with :/usr/java/jdk1.6.0_38/bin
so it'll become the following
PATH=$PATH:$HOME/bin:/usr/java/jdk1.6.0_38/bin
how could I do it with sed?
I tried with the following command from inside the console first, but it gave me error complaining 'sed: -e expression #1, char 13: unknown option to `s''
sed '/PATH/s/$/:/usr/java/jdk1.6.0_38/bin' ~/.bash_profile
what's wrong with my command above?

The problem is that you have regex delimiters in the replacement part of the substitute command. Either escape them with \ or use a different delimiter (comma in this case):
sed '/PATH/ s,$,:/usr/java/jdk1.6.0_38/bin,' ~/.bash_profile

This might work for you (GNU sed):
sed 's|PATH=$PATH:$HOME/bin|&:/usr/java/jdk1.6.0_38/bin|' ~/.bash_profile

Related

Removing line breaks from CSV exported from Google Sheets

I have some data in the format:
-e, 's/,Chalk/,Cheese/g'
-e, 's/,Black/,White/g'
-e, 's/,Leave/,Remain/g'
in a file data.csv.
Using Gitbash, I use the file command to discover that this is ASCII text with CRLF terminators. If I also use the command cat -v , I see in Gitbash that each line ends ^M .
I want to remove those terminators, to leave a single line.
I've tried the following:
sed -e 's/'\r\n'//g' < data.csv > output.csv
taking care to put the \r\n in single quotes in order that the backslash is treated literally, but it does not work. No error, just no effect.
I'm using Gitbash for Windows.
Quotes within quotes cancel each other out, so you actually undo the quotes around the sed command for the newline characters. You could escape the quotes like 's|'\''\r\n'\''||g', but that would just include them in the string, which would not match anything in your case.
But that is not the only problem; sed by default only processes strings between newlines.
If you have the GNU version of sed, RAM to spare if the file is huge, and are sure the file does not contain data with null characters, try adding the -z argument, like:
sed -z -e 's|\r\n||g' < data.csv > output.csv
Though I guess you probably also want to replace it with a comma:
sed -z -e 's|\r\n|,|g' < data.csv > output.csv
For non-GNU versions of sed, you may have an easier time using tr instead, like:
tr '\r\n' ',' data.csv > output.csv

sed - 'extra character after the command'

having trouble with a sed command.
I'm looking to find a line in a file and replace it.
In my script I've used this command without issue; (I use it to set variables)
sed -i '/job=empty/c\job='$job'' $sd/pingcheck-mon-$job.sh
The line I want to replace looks like this,
bash home/user/pingcheck/pingcheck-jobs/job1/pingcheck-mon-job1.sh
This is the command I can't get to run:
sed -i '/bash '$sd'/pingcheck-mon-'$job'.sh/c\jobslot=empty' $wd/pingcheck-worker.sh
Error I get:
sed: -e expression #1, char 9: extra characters after command
Could someone please tell me where I'm going wrong?
Thanks in advance!

Inserting numbers with sed in Linux?

I have the following line in cmdline
sed -e '1s/^/\\documentstyle\[11pt\]\{article\}\n/' -e 's/[0-9]//g' test.txt
My desired output is something like this
\documentstyle[11pt]{article}
rest of the file
However I only get this
\documentstyle[pt]{article}
rest of the file
I can't seem to find a way to insert numbers. I tried backslashing. Solution might be simple, but I'm a newbie with sed.
Note that sed has more commands than just s///. To insert a line at the top of a file:
sed -e '1i\
\\\documentstyle[11pt]{article}' -e 's/[0-9]//g' file
(frustratingly, the number of backslashes to achieve a backslash in the output was found by trial and error)
The bonus is that does not affect your goal to remove numbers.
My second command was removing numbers, working as intended indeed, but I was just trying to do it all at once. Credits to Jonathan Leffler.

Execute sed command inside TCL script

I am trying to execute sed command inside TCL script . Basically i wanted to remove all empty lines from the input file before reading the file using TCL. so i tried following in my script
exec sed -i '/^\s*$/d' .tmp.PG_Ring
set fid [open ".tmp.PG_Ring" r]
But the script is dumping following Error .
sed: -e expression #1, char 1: unknown command: `''
while executing
"exec sed -i '/^\s*$/d' .tmp.PG_Ring"
(file "pg_ring.tcl" line 1)
could you please provide me work around for this & help me with best way to do this?
That won't work, as single quotes have no special meaning to Tcl at all. Tcl uses braces to mean the same sort of thing (except they nest nicely), so instead you can use this:.
exec sed -i {/^\s*$/d} .tmp.PG_Ring

How to output the contents of a variable to a newline using sed

PING=$(ping -c 2 google.com)
sed -i.bak "s/test:/&\n$PING/g" test.txt
Im trying to output the variable PING on a newline after test: in the test.txt file.
But i keep receiving this error.
sed: -e expression #1, char 64: unterminated `s' command
I don't know where I'm going wrong any help is much appreciated.
you can write sed scripts like you write bash scripts, and newlines are command separators. you need a line continuation.
untested:
sed "s/test/&\
$PING/g" file
Style advice: get out of the habit of using UPPERCASE variable names. One day you'll use PATH and break your script.