how to remove rows before a selected row [duplicate] - sed

This question already has answers here:
Delete all lines before first occurrence of specific string in file
(5 answers)
Remove all lines before a match with sed
(4 answers)
How to ignore all lines before a match occurs in bash?
(7 answers)
Closed 2 years ago.
I have a flat file data.txt in Linux/Centos server which contains a list of files e.g.
# cat data.txt
/home/user/data/1
/home/user/data/2
/home/user/data/3
/home/user/data/4
/home/user/data/5
/home/user/data/6
how can I remove from data.txt all rows before
/home/user/data/4
to receive this result
# cat data.txt
/home/user/data/4
/home/user/data/5
/home/user/data/6

sed -ne '/^\/home\/user\/data\/4$/,//p'

Related

How to delete the first line of match by sed [duplicate]

This question already has answers here:
Delete the first line that matches a pattern
(5 answers)
Closed 2 years ago.
I have file like this,then now I want to delete only the first def_xxx
abc_xxx
def_xxx
ghi_xxx
abc_yyy
def_yyy
ghi_yyy
It delete the two lines def_xxx,def_yyy.
sed -e '/def/d' myfile.txt
How can I delete the only first line def_xxx??
sed -e '0,/def/{/def/d;}' myfile.txt
This deletes the first occurrence of the pattern.
From its manual:
0,addr2
Start out in "matched first address" state, until addr2 is found. This is similar
to 1,addr2, except that if addr2 matches the very first line of input the
0,addr2 form will be at the end of its range, whereas the 1,addr2 form will
still be at the beginning of its range. This works only when addr2 is a
regular expression.
Ref: https://linux.die.net/man/1/sed

Remove the contents of block based on condition [duplicate]

This question already has answers here:
sed multiline delete with pattern
(2 answers)
Closed 4 years ago.
Below sed removes all occurrences of blocks between {content-start} and {content-end}, but want to remove only block contains the sting 'labtest'.
sed -ie '/{content-start.*}/,/{content-end}/d' test.txt
test.txt
{content-start}
abc1
labtest
def1
ghi1
{content-end}
{content-start}
abc2
def2
labtest
ghi2
{content-end}
{content-start}
abc3
def3
ghi3
{content-end}
This might work for you (GNU sed):
sed '/{content-start}/{:a;N;/{content-end}/!ba;/labtest/d}' file
Gather up the lines between the start and end strings then test the collection for the required string and delete if positive.

Remove newline characters in file [duplicate]

This question already has answers here:
What's the most robust way to efficiently parse CSV using awk?
(6 answers)
Closed 5 years ago.
I have a text file with comma seperated values which has newline characters in the column values. So it makes the column data split to next line causing data issues.
Sample data
"604","56-1203802","xx","VEN","null","50","1","20","N�
jTï"
"5526","841328305","yyINC","VEN","null","50","1","20","~R¿½K�ï
¿½ï¿½}("
"604","561203802","C","VEN",,"null","50","1","20","2ï½a��"
Expected Output
"604","56-1203802","xx","VEN","null","50","1","20","N�jTï"
"5526","841328305","yyINC","VEN","null","50","1","20","~R¿½K���}("
"604","561203802","C","VEN",,"null","50","1","20","2ï½a��"
I need to remove the newlines inside double-quoted strings.
I tried the below awk command to remove it, but it is not working as expected.
gawk -v RS='"' 'NR % 2 == 0 { gsub(/\n/, "") } { printf("%s%s", $0, RT) }' infile.txt > outfile.txt
The required result would be to remove the LF and CR characters from the data.
I tried solutions for similar question posted, but not working for me.
Newline characters in the file are not visible unless copied to Notepad++ when it shows as CR LF.
You can try this sed:
sed ':loop; /" *$/!{N;s/\n//g; b loop}' file

SED command to remove trailing white spaces [duplicate]

This question already has answers here:
How to remove trailing whitespaces with sed?
(12 answers)
Closed 9 years ago.
Is there a sed command to remove trailing white spaces?
use the command below:
sed 's/\s*$//g' your_file

sed delete line with file path [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
sed: delete using a different delimiter
I can substitute line in file like this
sed "s|$PATH_WITH_SLASH||" file
but I cannot delete it
sed "|$PATH_WITH_SLASH|d" file
The problem is that | character cannot be used for deletion. Why?
If you use other character as address delimiter, you need to use backslash before the first char. So
/address/
or
\|address|