(sed) Delete an entire line if either condition is true -


i working csv file looks this

kmgm more words , things 7hsq other more words , stuff jhgq8 more other stuff kh21 , more stuff 

the valid lines first word letter followed 3 characters letters or numbers. in above example, lines containing kmgm , kh21 valid. want delete others using sed.

i wanted make condition say,

if first character not letter or     fifth character not space or     characters two, three, or 4 contain         other uppercase letter or number     delete entire line 

i don't know how formulate in sed. had similar problem yesterday rows 4 characters long. have added information , rows vary in length.

this might work you.

sed -n '/^[a-z][a-z0-9]\{3\} /p' 

instead deleting, keeps lines match conditions. this:

if first character letter ,    fifth character space ,    characters two, three, or 4 contain         uppercase letter or number     keep (print) line 
  • /p printing if regex matches
  • -n avoids printing otherwise

if want edit file in-place, can run this:

sed --in-place=.bak -n '/^[a-z][a-z0-9]\{3\} /p' yourfile.csv 

it delete lines want delete directly in file , stores backup of original file in yourfile.csv.bak.


Comments

Popular posts from this blog

javascript - Karma not able to start PhantomJS on Windows - Error: spawn UNKNOWN -

Nuget pack csproj using nuspec -

c# - Display ASPX Popup control in RowDeleteing Event (ASPX Gridview) -