Skip to content

Tag: sed

sed/awk – remove a multiline block if it contains multiple patterns

In an xml file a multiline block is identified by <start></start>. I need to find and delete these multiline blocks if they contain a set of pattern in any order (pattern1, pattern2, etc). For example, in the following: if searching for pattern1 only, blocks 1, 3, 4 should be deleted if searching for pattern2 only, blocks 1, 2, 4 should

Shell : choosing string between two strings using sed

I have a log file in format like this : I want to return all the strings which are between pseudo and pseudoConcat, my desired output is : How can I do this using sed or awk? I’m trying for a few days in vain. Thanks. Answer With GNU grep: Output without trailing spaces: thierry33 i love you With bash:

Grabbing data between two commas

I am in the process of writing a simple script to grab battery information from acpi so I can format and output it. Currently, I am using cut to grab this information, but as the battery state changes, cut does not grab the correct data and instead will grab a portion of a string instead of the battery percentage. When

sed command string variable concatenation within single quotes

I want to modify the include_path directive in my php.ini file using sed. Below, I’ve got a command that looks for a string and places the directive on a line after the match. However, I cannot get it to work while still maintaining the double quotes within the ${install_path} variable. The double quotes must be there. The file I want

Partially replace string using ‘sed’ shell command

I need to delete the <#> in the following pattern: Outputs should be like: I was trying to do this sed ‘s/(vdd1[a-z]*).<[0-9]>/1/’ file1 > file2 But it gives me “vdd1” all the way. How can I do it correctly? Answer The dot . after the paren is matching the letter after the 1. You need to get rid of it.

compare the length of multiple files using awk or sed

I want to compare the number of lines of each file and choose the one that contains the maximum number of lines for example I will get as result Answer Alternative with lot’s of piping this script assumes the file names are under your control and space or _V won’t appear in the base names. Otherwise check out @Qualia’s version.

SED one liner to uncomment and replace first occurrence of a pattern

I have this settings.conf file in linux defined as follows: I would like to uncomment # second-setting = off of Section A only (first occurrence), and set the value to on. So far, I have this: cat settings.conf | sed ‘/^# second.*/ {s/^#//;s/off/on/}’ Any tips? Answer Is this what you had in mind? Or if your on osx with non-gnu

Extract substring from string in linux

i need to extract oracle-xe-11.2.0-1.0.x86_64.rpm from and i have no clue how to start. The condition that must be met is only .zip needs to be removed. If there is no .zip then nothing should happen to the string . How to go with it ? Answer Use basename instead: Result: