How can I use sed or grep to delete the first line of input if it contains a string? [duplicate] - linux

This question already has answers here:
How do I match multiple addresses in sed?
(4 answers)
Closed 3 years ago.
I am redirecting input and have 2 fields that I extracted from the tail of an XML file, and I need to ignore the first line if it isn't the first of the 2 entries.
tail -n 327 ~/.local/share/recently-used.xbel | grep -e "<bookmark href=" -e "<mime:mime-type type="
Here is the output from that code, which is working fine, but the problem is that the first line is a
<mime:mime-type type="application/x-shellscript"/>
<bookmark href="file:///usr/local/bin/menu_manager.sh" added="2019-09-17T08:33:48Z" modified="2019-09-17T08:33:48Z" visited="2019-09-17T08:33:49Z">
<mime:mime-type type="application/x-shellscript"/>
I need to look at the first line, and if it contains the string
<mime:mime-type type=
then I need to remove that line and pass the rest of the lines on for the next processing step
I tried
sed '1/<mime:mime-type/d'
But is gives me an error:
sed: -e expression #1, char 2: unknown command: `/'

Try
sed '1{/<mime:mime-type/d}'
which uses a block {} which is only run on line 1, with the delete command in the block.

If you are OK with awk you can use this
awk 'NR!=1 || !/<mime:mime-type type=/'
This prints every line that is not the first line (NR!=1) or doesn't match the pattern (!/<mime:mime-type type=/). As there is no action specified, awk uses the default action print.

Related

inserting contents of one text file into another in bash [duplicate]

This question already has answers here:
Insert newline (\n) using sed
(4 answers)
Insert contents of a file after specific pattern match
(1 answer)
Closed 1 year ago.
I would like to insert the contents of one text file (with newlines) into another at a predetermined location (after some string):
I tried the following:
the base file:
cat base
ONE
TWO
THREE
the extension:
cat ext
100
200
300
400
500
600
700
800
900
A00
B00
C00
D00
and the script I tried:
#!/bin/bash
ext=$(<ext)
sed -i "s/TWO/TWO\n$ext/" base
which gives me sed: -e expression #1, char 14: unterminated `s' command
If you want to edit a file directly, I always suggest ed instead of the non-standard sed -i (Where different implementions of sed that do support it act differently, a common source of questions here):
printf "%s\n" "/TWO/r ext" w | ed -s base
will insert the contents of file ext after the first line containing TWO in base, and then write the new version of base back to disk.
If you must use sed, the proper invocation will look very similar (No surprise since they're sibling commands):
sed -i '/TWO/r ext' base
(This will insert the ext file after every TWO in base, though, not just the first.)
The key in both is the r command, which reads the contents of the given file and inserts it after a line with the matching address. Works a lot better than trying to read the file contents into a variable and including the text directly in the ed/sed commands.
If you want to insert the contents of a variable after the line, you can use
printf "%s\n" "/TWO/a" "$ext" . w | ed -s base
(As long as the variable doesn't have a line with just a period)
or with GNU sed
sed -i "/TWO/a ${ext//$'\n'/\\\n}" base
to append text after the addressed line.
While the answer given by Shawn shows a possible solution, it does not explain why your attempt did not work.
Note that your variable ext contains line feed characters. Hence, sed sees here a physical line feed. You would get the same error message when typing on the command line (in two lines):
sed -i "s/TWO/TWO\nfoo
/" base
This might work for you (GNU sed):
sed -e '0,/TWO/{/TWO/r extFile' -e '}' baseFile
This will place the file extFile after the first occurrence of the string TWO. The -i option may be used to amend the file in place.
An alternative solution:
sed -e '/TWO/!b;r extFile' -e ':a;n;ba' baseFile
N.B. The use of the -e option to split the commands after the r filename command. The r must be followed by a newline and the -e simulates this on a single line.
If you want to insert the file after each occurrence of the string TWO, then just use (as Shawn has already explained):
sed '/TWO/r extFile' baseFile

grep weird behaviour in linux command line [duplicate]

This question already has answers here:
Using the star sign in grep
(12 answers)
Closed 1 year ago.
I have a file test.txt which contains this text data
# cat test.txt
*#=>*#
if I use grep to check if the string is in the file using this way
# grep "*#=>*#" test.txt
#
it returns nothing..
while if I grep a partial string search
# grep "*#=>" test.txt
# *#=>*#
it works correctly ..
Why in the first case do grep return nothing ?
The asterisk is special to grep, it means "the previous token is repeated zero or more times". So >* would match an empty string or > or >> etc.
To match an asterisk, backslash it in the pattern:
grep '\*#=>\*#' test.txt
(The first asterisk follows no token, so the backslash is optional.)

How to use sed to replace text with a file path? [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Closed 2 years ago.
I'm writing a bash script where I need to replace text in a file with a specific file path, but my understanding is that sed does not work with specific characters such as /. I'm wondering if there is some way around this?
Here is my script currently:
currentdir="$PWD"
filepathvar="${currentdir}/settings.ini"
sed -i -e "s/filepath/$filepathvar/g" aimparmstest
When I print out filepathvar everything is as I expect it to be, but it seems the fact that filepathvar contains special characters, it gives me the following error:
sed: -e expression #1, char 13: unknown option to `s'
Is there any way around this? Or perhaps another command I can use? I haven't had any success with changing around the parameters. Any help is greatly appreciated.
You can use any character as the separator (the first character). For example:
echo "a/b/c" | sed -e 's|/|_|g'
In your case:
sed -i -e "s|filepath|$filepathvar|g" aimparmstest

How to delete 1 or more matching line(s) while reading a file in bash script [duplicate]

This question already has answers here:
How to pass a variable containing slashes to sed
(7 answers)
Combining two sed commands
(2 answers)
Linux, find replace on a folder of files using a list of items for replacement?
(1 answer)
Closed 4 years ago.
I want to read file using a bash script and delete line(s) which are matching with my specific scenario (line(s) starting with 'z').
my code works fine if the 'inputFile' contains only alphabetic characters.
but, if a line with 'specific characters of sed' (line eg : z-2.10.3.2 x/y/z F (&)[]+* ) then i got an error,(error : sed: -e expression #1, char 29: unterminated `y' command).
#!/bin/bash
inputFile="test.txt"
while IFS= read -r line
do
echo "$line"
if [[ $line == z* ]];
then
sed -i "/$line/d" $inputFile
fi
done < "$inputFile"
i want to delete 'z-2.10.3.2 x/y/z F (&)[]+*' kind of lines, how can i do this...?
As you mentioned you don't need line which has z*
Simply use grep -v
grep -vE "^[[:blank:]]*z" file
I have created one scenario where I have a file which contains
root#ubuntu:~/T/e/s/t# cat file
hello world
sample line 1
sample line 2 world
sample line 3
sample line 4
In my case, I want to remove the line contains "world"
root#ubuntu:~/T/e/s/t# grep -v "world" file
sample line 1
sample line 3
sample line 4
If you want you can redirect your output in another file.

How to use sed 's/$var/$var2' file [duplicate]

This question already has an answer here:
sed fails with "unknown option to `s'" error [closed]
(1 answer)
Closed 5 years ago.
I'm trying to replace a value shown as a variable with another value shown as a second variable, using the following line:
sed -i "s/$header/$new/" file.f
Where "$header" is the old variable I want replaced with the new one ($new).
I'm getting this error:
sed: -e expression #1, char 20: unknown option to `s'
I've tried
sed -i 's/$header/$new/' file.f
sed -i "s/$header/$new/" file.f
sed -i 's/"$header"/"$new"/' file.f
None of it seem to work.
How should I write this line so I can get the right output (replacing '$header' with '$new' on the file)?
Thanks in advance
sed -i "s/$old/$new/" file
works fine. You can change the separtor if your data has the character /.
sed -i "s#$old#$new#" file
If you are not sure of the content of the variables and to reduce clashing the separator you can use
sed "s^A$old^A$new^A"
to enter the CTRL-A press CTRL-V + CTRL-A (or any other value not expected in vars)

Resources