I'm trying to insert into a text file the string cd $var at the second line using sed, but it doesn't seem to work. I'm using the syntax for inserting a line at a specific line in a file,
sed -i '2icd $var' FILE
The format of which was found as the response to this question:
Insert a line at specific line number with sed or awk
My best guess is that sed is interpreting the command literally and evaluating it instead of copying it in. However, all of my attempts at forcing it to be evaluated simply as a string have failed. My attempts so far:
sed -i '2i\cd $var' FILE
sed -i '2i\cd \$var' FILE
sed -i "2i'cd $var'" FILE
and
Line='cd $var'
sed -i "2i$Line" FILE
I was fairly sure this last attempt would succeed, due to the hard quotes, but it still failed.
In fact, this also failed,
sed -i '2icd' FILE
Yet this succeeded (Just to confirm the general format):
sed -i '2ic' FILE
Just to be clear, all 5 of the failed attempts yielded the same error: A blank line was inserted at the desired location.
sed -i "2 i\\
$var" file
need a escape NewLine normaly after the i and depending the OS/sed a space before and/or after the i also. Finaly, with double quote, escape the \
Related
i have a sed command that looks for a particular word in a text file for pattern matching and then appends a date at the end of the line containing that particular word.
this is the code block
some_function() {
while IFS=: read -r jname state; do
echo "the job is $name"
if [[ -n ${name} ]]
then
//code to get creation date which works fine
creation_date="00-00-00_00-00-00"
sed -i '/^'$job_name':/s/$/':$creation_date'/' $logs_folder/job_state.$today
fi
done <"$logs_folder/job_state.$today"
}
everything works fine untill the sed command. This command does not work for certain names such as ARCHIVE-REQUEST - New, ARCHIVE-REQUEST - rss_master
The sed command throws error
sed -i '/^ARCHIVE-REQUEST' - 'rss_master:/s/$/:00-00-00_00-00-00/' file.txt
sed: -e expression #1, char 17: unterminated address regex
and also escaping these names is not an option because the file from where these names are taken has other delimiter as well that works with the sed command and since this file is generated dynamically there is no way to hard code delimiters in the sed command
here is my sample file
aggressive_cleanup_wrapper_M3
aggressive_cleanup_wrapper_M5
aggressive_cleanup_wrapper_others
aggressive_cleanup_wrapper_R720
ARCHIVE-REQUEST - Approval Pending
ARCHIVE-REQUEST - Archive
ARCHIVE-REQUEST - Lock Period
ARCHIVE-REQUEST - New
ARCHIVE-REQUEST - rss_master
How can i modify my sed command to work with the rest of my code?
Can someone please suggest?
You do not understand how escaping works. You need to do this:
sed -i '/^'"$job_name"':/s/$/':"$creation_date"'/' ...
I would like to use sed to remove all occurances of this line if and only if it is this
<ab></ab>
If this line, I would not want to delete it
<ab>keyword</ab>
My attempt that's not working:
sed '/<ab></ab>/d'
Thanks for any insight. I'm not sure what's wrong as I should not have to escape anything?
I'm using a shell script named temp to execute this. My command is this:
cat foobar.html | ./temp
This is my temp shell script:
#!/bin/sh
sed -e '/td/!d' | sed '/<ab></ab>/d'
It looks like we have a couple of problems here. The first is with the / in the close-tag. sed uses this to delimit different parts of the command. Fortunately, all we have to do is escape it with \. Try:
sed '/<ab><\/ab>/d'
Here's an example on my machine:
$ cat test
<ab></ab>
<ab></ab>
<ab>test</ab>
$ sed '/<ab><\/ab>/d' test
<ab>test</ab>
$
The other problem is that I'm not sure what the purpose of sed -e '/td/!d' is. In it's default operating mode, you don't need to tell it not to delete something; just tell it exactly what you want to delete.
So, to do this on a file called input.html:
sed '/<ab><\/ab>/d' input.html
Or, to edit the file in-place, you can just do:
sed -i -e '/<ab><\/ab>/d' input.html
Additionally, sed lets you use any character you want as a delimiter; you don't have to use /. So if you'd prefer not to escape your input, you can do:
sed '\#<ab></ab>#d' input.html
Edit
In the comments, you mentioned wanting to delete lines that only contain </ab> and nothing else. To do that, you need to do what's called anchoring the match. The ^ character represents the beginning of the line for anchoring, and $ represents the end of the line.
sed '/^<\/ab>$/d' input.html
This will only match a line that contains (literally) </ab> and nothing else at all, and delete the line. If you want to match lines that contain whitespace too, but no text other than </ab>:
sed '/^[[:blank:]]*<\/ab>[[:blank:]]*$/d' input.html
[[:blank:]]* matches "0 or more whitespace characters" and is called a "POSIX bracket expression".
Does anyone know what this means?
sed -e 's/\r$//' inputfile > outputfile
This is what I have so far:
\r refers to Carriage Return (CR)
so possibly Swap the blanks for Return Carriage? in the inputfile?
I'm not too sure really
It's changing files from CRLF-terminated lines into LF-terminated lines. The former tend to be Windows-type files where each line ends with a carriage-return/linefeed (CRLF or \r\n).
UNIX-type files just have a newline character (LF or \n).
Specifically, that sed command substitutes \r at the end of a line (indicated by $) with nothing, the same as s/xyzzy/plugh/ would change the first xyzzy in the line into plugh.
sed is the name of the program you call.
-e tells sed that the following argument is the expression to run.
s/\r$// is a substitution: it tells sed to replace carriage return at the end of line ($) with nothing. Sed does that for each line.
inputfile is the file from which sed reads its input.
> is a redirection operator, it means the output of sed will be redirected to outputfile.
Basically, the result should be the same as dos2unix (sometimes renamed to fromdos).
I want to empty a file using sed command. I searched lot of forums and tutorial. There is no available to delete all contents of the file. How to delete all contents of the file using sed command.
It looks like a strange request. Anyway, this is a way:
sed -i '/^/d' file
sed -i does an in-place replacement.
/^/ matches lines, in this case all of them because ^ means "beginning of line".
/d deletes them.
Or shorter (thanks glenn jackman as always):
sed -i d file
You don't need sed for this. To empty a file:
> filename
with no command, that redirection will truncate the file.
Try this sed. It will remove all.
sed -ni '' file
n do not print if not told to do so.
i in place.
Since no code is given, file will be replaced by nothing.
I'm trying to do the opposite of this question, replacing Unix line endings with Windows line endings, so that I can use SQL Server bcp over samba to import the file. I have sed installed but not dos2unix. I tried reversing the examples but to no avail.
Here's the command I'm using.
sed -e 's/\n/\r\n/g' myfile
I executed this and then ran od -c myfile, expecting to see \r\n where there used to be \n. But there all still \n. (Or at least they appear to be. The output of od overflows my screen buffer, so I don't get to see the beginning of the file).
I haven't been able to figure out what I'm doing wrong. Any suggestions?
When faced with this, I use a simple perl one-liner:
perl -pi -e 's/\n/\r\n/' filename
because sed behavior varies, and I know this works.
What is the problem with getting dos2unix onto the machine?
What is the platform you are working with?
Do you have GNU sed or regular non-GNU sed?
On Solaris, /usr/bin/sed requires:
sed 's/$/^M/'
where I entered the '^M' by typing controlV controlM. The '$' matches at the end of the line, and replaces the end of line with the control-M. You can script that, too.
Mechanisms expecting sed to expand '\r' or '\\r' to control-M are going to be platform-specific, at best.
You don't need the -e option.
$ matches the endline character. This sed command will insert a \r character before the end of line:
sed 's/$/\r/' myfile
Just adding a \r (aka ^M, see Jonathan Leffler's answer) in front of \n is not safe because the file might have mixed mode EOL, so then you risk ending up with some lines becomming \r\r\n. The safe thing to do is first remove all '\r' characters, and then insert (a single) \r before \n.
#!/bin/sh
sed 's/^M//g' ${1+"$#"} | sed 's/$/^M/'
Updated to use ^M.
sed 's/\([^^M]\)$/\0^M/' your_file
This makes sure you only insert a \r when there is no \r before \n. This worked for me.
Try using:
echo " this is output" > input
sed 's/$/\r/g' input |od -c
Maybe if you try it this way
cat myfile | sed 's/\n/\r\n/g' > myfile.win
will work, from my understanding your just making the replacements to the console output, you need to redirect output to a file, in this case myfile.win, then you could just rename it to whatever you want. The whole script would be (running inside a directory full of this kind of files):
#!/bin/bash
for file in $(find . -type f -name '*')
do
cat $file | sed 's/\n/\r\n/g' > $file.new
mv -f $file.new $file
done