How can I remove everything from Part1, up to Part2, including Part1 and Part2, from the following text?
ABC
Part1
text
more text
Part2
DEF
The result should be:
ABC
DEF
Using a print flag with awk:
$ awk '/Part1/{p=1}!p;/Part2/{p=0}' file
ABC
DEF
Using sed:
sed '/Part1/,/Part2/d' filename
Try this :
awk '/^Part1/,/^Part2/{next}{print}' file
Related
I have a file with text as follows:
###interest1 moreinterest1### sometext ###interest2###
not-interesting-line
sometext ###interest3###
sometext ###interest4### sometext othertext ###interest5### sometext ###interest6###
I want to extract all strings between ### .
My desired output would be something like this:
interest1 moreinterest1
interest2
interest3
interest4
interest5
interest6
I have tried the following:
grep '###' file.txt | sed -e 's/.*###\(.*\)###.*/\1/g'
This almost works but only seems to grab the first instance per line, so the first line in my output only grabs
interest1 moreinterest1
rather than
interest1 moreinterest1
interest2
Here is a single awk command to achieve this that makes ### field separator and prints each even numbered field:
awk -F '###' '{for (i=2; i<NF; i+=2) print $i}' file
interest1 moreinterest1
interest2
interest3
interest4
interest5
interest6
Here is an alternative grep + sed solution:
grep -oE '###[^#]*###' file | sed -E 's/^###|###$//g'
This assumes there are no # characters in between ### markers.
With GNU awk for multi-char RS:
$ awk -v RS='###' '!(NR%2)' file
interest1 moreinterest1
interest2
interest3
interest4
interest5
interest6
You can use pcregrep:
pcregrep -o1 '###(.*?)###' file
The regex - ###(.*?)### - matches ###, then captures into Group 1 any zero o more chars other than line break chars, as few as possible, and ### then matches ###.
o1 option will output Group 1 value only.
See the regex demo online.
sed 't x
s/###/\
/;D; :x
s//\
/;t y
D;:y
P;D' file
Replacing "###" with newline, D, then conditionally branching to P if a second replacement of "###" is successful.
This might work for you (GNU sed):
sed -n 's/###/\n/g;/[^\n]*\n/{s///;P;D}' file
Replace all occurrences of ###'s by newlines.
If a line contains a newline, remove any characters before and including the first newline, print the details up to and including the following newline, delete those details and repeat.
I have a tab file with two columns like below
BB_12 100_AA
BB_13 101_AB
BB_14 102_AD
BB_15 103_AC
I wish to remove the number_ in second column (replace number_ with nothing). For this I tried sed replace in the following ways unsuccessfully.
sed 's/\d+\_//g' infile
sed 's/(\d+\_)//g' infile
But none of the tweaks worked. It looks like it is not searching in 2nd column. How to modify this ? The expected output is
BB_12 AA
BB_13 AB
BB_14 AD
BB_15 AC
Thanks in advance.
You may just process the last column with sed:
sed -E 's/[^ ]*_([^ ]*) *$/\1/' file
The output:
BB_12 AA
BB_13 AB
BB_14 AD
BB_15 AC
Awk alternative:
awk '{ sub(/^[^ ]+_/, "", $2) }1' OFS='\t' file
Following simple sed may help you in same.
sed 's/\([^ ]*\) \([^_]*\)_\(.*\)/\1 \3/g' Input_file
Output will be as follows.
BB_12 AA
BB_13 AB
BB_14 AD
BB_15 AC
I need to remove variables that have no value with a sed command.
Input:
a: x sdvsv rhrh
b:
c: sdbbb
d:
Output:
a: x sdvsv rhrh
c: sdbbb
sed '/:\s*$/d'
works for your example.
more general way:
sed '/^[^:]\+:\s*$/d'
With awk:
awk 'NF>1' inputFile
sed -E '/^\w+:\s*$/d'
match any line composed of a non empty word followed by a colon and any spaces, and deletes it.
sed -e '/^[^:]*:\s*$/d' a.txt
Match all characters followed by a ':', followed by any number of spaces, and the end of line.
My text looks like this:
cat
catch
cat_mouse
catty
I want to replace "cat" with "dog".
When I do
sed "s/cat/dog/"
my result is:
dog
catch
cat_mouse
catty
How do I replace with sed if only part of the word matches?
There's a mistake :
You lack the g modifier
sed 's/cat/dog/g'
g
Apply the replacement to all matches to the regexp, not just the first.
See
http://www.gnu.org/software/sed/manual/html_node/The-_0022s_0022-Command.html
http://sed.sourceforge.net/sedfaq3.html#s3.1.3
If you want to replace only cat by dog only if part of the word matches :
$ perl -pe 's/cat(?=.)/dog/' file.txt
cat
dogch
dog_mouse
dogty
I use Positive Look Around, see http://www.perlmonks.org/?node_id=518444
If you really want sed :
sed '/^cat$/!s/cat/dog/' file.txt
bash-3.00$ cat t
cat
catch
cat_mouse
catty
To replace cat only if it is part of a string
bash-3.00$ sed 's/cat\([^$]\)/dog\1/' t
cat
dogch
dog_mouse
dogty
To replace all occurrences of cat:
bash-3.00$ sed 's/cat/dog/' t
dog
dogch
dog_mouse
dogty
awk solution for this
awk '{gsub("cat","dog",$0); print}' temp.txt
How would like to join two lines usung awk or sed?
For example, I have data like below:
abcd
12:12:12:12:12:12:12:12
efgh001_01
45:45:45:45:45:45:45:45
ijkl7464746
78:78:78:78:78:78:78:78
and I need output like below:
abcd 12:12:12:12:12:12:12:12
efgh001_01 45:45:45:45:45:45:45:45
ijkl7464746 78:78:78:78:78:78:78:78
Running this almost works, but I need the space or tab:
awk '!(NR%2){print$0p}{p=$0}'
You're almost there:
awk '(NR % 2 == 0) {print p, $0} {p = $0}'
With sed you can do that as follows:
sed -n 'N;s/\n/ /p' file
where:
N reads next line
s replaces the new line character with a space to join both lines properly
p prints the result
This might work for you:
sed '$!N;s/\n/ /' file
or this:
paste -sd' \n' file