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"'/' ...
Related
File Content
abab102
cdcd103
efef105
I want the username and id separated. Here, abab is user and 102 is id.
I use the command
sed 's/\([a-z]\)\{4\}\([0-9]\)\{3\}/Username:\1 ID:\2/' file.txt
Get this
Username:b ID:2
Username:d ID:3
Username:f ID:5
But I am expecting
Username:abab ID:102
Username:cdcd ID:103
Username:efef ID:105
But using the command
sed -e 's/\([a-z]\)\{4\}/Username:&/' -e 's/\([0-9]\)\{3\}/ID:&/' file2.txt
Output
Username:ababID:102
Username:cdcdID:103
Username:efefID:105
This output is close to what I need, but still I am expecting a blank space between Username:abab ID:102.
I want to know why \1 or \2 is not working here.
\([a-z]\) - between ( ) is one letter. If you repeat the \(\) group, back reference goes to the last matched expression. Put all the repetition inside.
's/\([a-z]\{4\}\)\([0-9]\{3\}\)/Username:\1 ID:\2/'
Ugh, simpler with extended:
sed -E 's/([a-z]{4})([0-9]{3})/Username:\1 ID:\2/'
Using sed
$ sed -E 's/[[:alpha:]]{4}/Username:& ID:/' input_file
Username:abab ID:102
Username:cdcd ID:103
Username:efef ID:105
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".
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 \
I'm trying to write a script to swap out text in a file:
sed s/foo/bar/g myFile.txt > myFile.txt.updated
mv myFile.txt.updated myFile.txt
I evoke the sed program, which swaps out text in myFile.txt and redirects the changed lines of text to a second file. mv then moves .updated txt file to myFile.txt, overwriting it. That command works in the shell.
I wrote:
#!/bin/sh
#First, I set up some descriptive variables for the arguments
initialString="$1"
shift
desiredChange="$1"
shift
document="$1"
#Then, I evoke sed on these (more readable) parameters
updatedDocument=`sed s/$initialString/$desiredChange/g $document`
#I want to make sure that was done properly
echo updated document is $updatedDocument
#then I move the output in to the new text document
mv $updatedDocument $document
I get the error:
mv: target `myFile.txt' is not a directory
I understand that it thinks my new file's name is the first word of the string that was sed's output. I don't know how to correct that. I've been trying since 7am and every quotation, creating a temporary file to store the output in (disastrous results), IFS...everything so far gives me more and more unhelpful errors. I need to clear my head and I need your help. How can I fix this?
Maybe try
echo $updatedDocument > $document
Change
updatedDocument=`sed s/$initialString/$desiredChange/g $document`
to
updatedDocument=${document}.txt
sed s/$initialString/$desiredChange/g $document
Backticks will actually put the entire piped output of the sed command into your variable value.
An even faster way would be to not use updatedDocument or mv at all by doing an in-place sed:
sed -i s/$initialString/$desiredChange/g $document
The -i flag tells sed to do the replacement in-place. This basically means creating a temp file for the output and replacing your original file with the temp file once it is done, pretty much exactly as you are doing.
#!/bin/sh
#First, I set up some descriptive variables for the arguments
echo "$1" | sed #translation of special regex char like . * \ / ? | read -r initialString
echo "$2" | sed 's|[\&/]|\\&|g' | read -r desiredChange
document="$3"
#Then, I evoke sed
sed "s/${initialString}/${desiredChange}/g" ${document} | tee ${document}
don't forget that initialString and desiredChange are pattern interpreted as regex, so a trnaslation is certainly needed
sed #translation of special regex char like . * \ / ? is to replace by the correct sed (discuss on several post on the site)
sed -n '$'!p abc.txt | tail +2 > def.txt
I have the above mentioned sed command in my code - I am unable to figure out what it does -I am going through sed tutorials to find it out but am not able to - Can some one please help me in figuring out what it does - Thanks
Taking this in stages:
sed -n abc.txt
"Run abc.txt through sed, but don't print anything out."
sed -n '$!p' abc.txt
(Note that I've corrected what I think was a misplaced quote mark.)
"Run abc.txt through sed; if a line isn't the last line, print it (i.e. print all but the last line)."
I guess you know the rest, but note that tail +2 is obsolete syntax-- tail -n 2 would be better.
EDIT:
To remove the last two lines, try
sed 'N;$d'
or if that doesn't work, crude but effective:
sed '$d' | sed '$d'
As far as the sed command '$'!p is concerned:
the $ matches only the last line of the input file.
the ! negates the sense of the match (so that it matches all but the last line).
the p prints out whatever was matched.
So basically this prints all but the last line of the file.
The -n option stops sed from performing its default action (to print the pattern space) - without that, you'd get one copy of the last line and two copies of all the other lines.
The quotes around $ are to stop the shell from trying to interpret it as a shell variable - I would have quoted the lot myself ('$!p') but that's a style issue, at least on bash. Other shells like csh (which uses ! for command history retrieval) may not be so forgiving.