Excluding/ignoring (Not Deleting) first and last line based on line number [duplicate] - linux

This question already has answers here:
Delete first and last line or record from file using sed
(5 answers)
Closed 4 years ago.
I'm using sed command to edit some text file.
How can i exclude the first and last line in each text file for being edited.
I went through sed gnu manual but i only found commands to match line ranges i.e. 1,$ or to exclude ranges i.e. 1,$!. i just need to exclude line # 1 and last line $. i'm not sure if its possible to select a range i.e 2, $-1?
Here's my code.
sed -e '1,$ s/.*/<p>&<\/p>/'' file.txt

You can't use maths in sed addresses. But you can tell sed to do nothing on the first and last line:
sed -e '1n; $n; s/.*/<p>&<\/p>/'
where n means "read the next line of input into the pattern space" (in case of the last line, it won't read anything).
(The two single quotes at the end of the expression are probably a typo, right?)

Related

How to change file parmater by input in bash [duplicate]

This question already has answers here:
How do I use sed to change my configuration files, with flexible keys and values?
(8 answers)
Closed 2 years ago.
I have a file that contain this line
SELINUX = enforce
I want to change this by given input to permissive
How i do that without demage?
If [[ "$1" == "Y"]]
then
sed -ri 's/(^.*SELINUX=)(.*$)/\1enforce/' file
else
sed -ri 's/(^.*SELINUX=)(.*$)/\1permissive/' file
fi
If the first passed parameter ($1) is equal to "Y" use sed to split SELINUX line into to 2 sections. Substitute the line for the first section followed by "enforce". If the passed parameter is not "Y" substitute the line for the first section followed by "permissive".

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

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.

Bash shell script: appending text to a specific line of an existing file without line break [duplicate]

This question already has answers here:
How to add to the end of lines containing a pattern with sed or awk?
(6 answers)
Closed 4 years ago.
I have a file called tmp.mount with the following contents
[Mount]
Options=mode=1777,strictatime,noexec,nosuid
I'd like to search the file via the word Options= to get the
line number.
I will search the word nodev via the given line number.
If it does not exist, I will insert the word ,nodev to the end of
this line via given line number.
With the results
[Mount]
Options=mode=1777,strictatime,noexec,nodev,nosuid,nodev
All without line break. Most of the solution was to use sed but i'm clueless on how I could incorporate the line search with sed.
awk '/Options=/ && ! /nodev/ {print $0 ",nodev"; next};1' file
no need to get the line number, just append the ",nodev" to the corresponding line

finding specific pattern in linux [duplicate]

This question already has answers here:
Print only matching word, not entire line through grep
(2 answers)
Closed 5 years ago.
I want to find specific pattern in all the files in a directory and copy them to another line
For E.g
I want to find LOG_WARNING in one file XYZ and copy them to another file.
LOG_WARNING (abc, xyz,("WARNING: Error in sending concurrent_ to pdm\n"));
command i have used is :
grep -rin "LOG_WARNING.*" file_name.c > output.txt
but it is not copying till the semicolon, please note that other texts are available in next line. I want to copy till ;(semi-colon)
grep -rh "LOG_WARNING" * > out.txt
This will match the pattern in all the files inside the directory.
Since you mentioned that the texts that are present after the ';' are on the next line, I have provided this command.
This will match the pattern and print the entire line, till the ';'.
Else,
try this
grep -roPh 'LOG_WARNING[^;]*;' * > out.txt

I have a requirement of searching a pattern from a file and displaying the pattern only in the screen,not the whole line .How can I do it in linux? [duplicate]

This question already has answers here:
Can grep show only words that match search pattern?
(15 answers)
Closed 5 years ago.
I have a requirement of searching a pattern like x=<followed by any values> from a file and displaying the pattern i.e x=<followed by any values>, only in the screen, not the whole line. How can I do it in Linux?
I have 3 answers, from simple (but with caveats) to complex (but foolproof):
1) If your pattern never appears more than once per line, you could do this (assuming your shell is
PATTERN="x="
sed "s/.*\($PATTERN\).*/\1/g" your_file | grep "$PATTERN"
2) If your pattern can appear more than once per line, it's a bit harder. One easy but hacky way to do this is to use a special characters that will not appear on any line that has your pattern, eg, "#":
PATTERN="x="
SPECIAL="#"
grep "$PATTERN" your_file | sed "s/$PATTERN/$SPECIAL/g" \
| sed "s/[^$SPECIAL]//g" | sed "s/$SPECIAL/$PATTERN/g"
(This won't separate the output pattern per line, eg. you'll see x=x=x= if a source line had 3 times "x=", this is easy to fix by adding a space in the last sed)
3) Something that always works no matter what:
PATTERN="x="
awk "NF>1{for(i=1;i<NF;i++) printf FS; print \"\"}" \
FS="$PATTERN" your_file

Resources