tag:blogger.com,1999:blog-1255024703457423340.post6247873407837473413..comments2024-03-13T13:54:48.044+05:30Comments on The UNIX School: sed - 25 examples to delete a line or pattern in a fileGuru Prasadhttp://www.blogger.com/profile/07898245472802947101noreply@blogger.comBlogger69125tag:blogger.com,1999:blog-1255024703457423340.post-6363216815070741982019-08-18T20:29:03.013+05:302019-08-18T20:29:03.013+05:30Hello.
How can i delete lines where all the poin...Hello. <br /><br />How can i delete lines where all the points in the line are same<br /><br />line 1: 40 50 60 70 80<br />line 2: 30 50 50 90 100<br />line 3: 50 50 50 50 50<br />line 4: 20 15 20 32 45<br /><br />How can i delete only lines like line 3 where all the points are 50. Line 1 and 2 also has 50 but not for all the points. <br /><br />ThanksAbdulazeezhttps://www.blogger.com/profile/08986315972301984534noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-42123877784487798092016-08-06T11:55:19.183+05:302016-08-06T11:55:19.183+05:30Hello, ur tutorial is really being helpfull for be...Hello, ur tutorial is really being helpfull for beginners.........<br /> in my case i need to check every 17th col of the lines (row)in a file and if a particular character is found (say B) i need to delete the whole line...... please helpAnonymoushttps://www.blogger.com/profile/07892072456599383720noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-50656261888945151572016-04-10T19:58:44.347+05:302016-04-10T19:58:44.347+05:30That was really awesome stuff I learnt today!!!! T...That was really awesome stuff I learnt today!!!! Thanks for the detailed explanationAnonymoushttps://www.blogger.com/profile/11017160113142270872noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-32402697738607327572016-02-27T00:01:56.034+05:302016-02-27T00:01:56.034+05:30Hi, how do I delete everything between two pattern...Hi, how do I delete everything between two patterns, including the patterns. The start and end of pattern could be on different lines. <br />E.g. if input is <br />start aaa end bbb ccc<br />aaa start bb end cc<br />aa bb cc<br />dd start ee<br />ff end gg<br /><br />The output should be <br />bbb ccc<br />aaa cc<br />aa bb cc<br />dd<br />gg<br /><br />Thanks!Preeti Vhttps://www.blogger.com/profile/06107097675106149492noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-21980794997244462302016-02-16T05:12:27.342+05:302016-02-16T05:12:27.342+05:30Hi Guruprasad,
I have another issue referencing s...Hi Guruprasad,<br /><br />I have another issue referencing same example above, only change is every input file has Trailer info at the end appearing only once (showing the filtering/selection criteria).<br /><br />I wish to retain first header, deleting all in headers appearing on subsequent pages. The solution provided by you perfectly works in that case. however in the process trailer (end of report) information appearing at the end also gets removed. Do we have any way to retain it .<br /><br />Thank you very much.Mandarhttps://www.blogger.com/profile/06380507499665961611noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-38280149962393922002016-02-15T18:52:56.468+05:302016-02-15T18:52:56.468+05:30$ y=$(wc -l<file)
$ let x=y-3+1
$ sed "$x,...$ y=$(wc -l<file)<br />$ let x=y-3+1<br />$ sed "$x,$y d" file<br /><br /><br /><br />Guru Prasadhttps://www.blogger.com/profile/07898245472802947101noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-22427254797557524652016-02-02T21:04:02.545+05:302016-02-02T21:04:02.545+05:30Hi, how do i delete the last n lines? i mean.. i h...Hi, how do i delete the last n lines? i mean.. i have<br /><br />1111<br />2222<br />3333<br />4444<br />5555<br /><br />and i want to remove from 3 till the end and get this<br /><br />1111<br />2222<br /><br />Thanks in advanceNrdiaz92https://www.blogger.com/profile/11448902339123429982noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-14721500279343610122016-01-23T06:49:43.102+05:302016-01-23T06:49:43.102+05:30Thanks for the examples. Could you show us how to,...Thanks for the examples. Could you show us how to, as a variation of 25?<br /><br />Delete the line containing the pattern 'Linux', the line before, 2 lines after.<br />JThttps://www.blogger.com/profile/13267374077866976342noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-76287446581083239872016-01-22T16:18:57.729+05:302016-01-22T16:18:57.729+05:30One of the best article which explains useful sed ...One of the best article which explains useful sed command with examples harishhttps://www.blogger.com/profile/11756694123493491949noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-36082158940017681582016-01-15T01:11:23.697+05:302016-01-15T01:11:23.697+05:3023. doesn't work in ksh on AIX :( man! I reall...23. doesn't work in ksh on AIX :( man! I really need it!Unknownhttps://www.blogger.com/profile/06589148865343496617noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-32260196994822386372016-01-05T17:13:42.536+05:302016-01-05T17:13:42.536+05:30I'm facing a issue in deleting a line having &...I'm facing a issue in deleting a line having 'NUL' special character.<br />I used the below command<br /> sed '/^@/d' test.csv <br />but this is removing only the 'NUL' character and not the line it contains. Can someone help on this.Sagar Prasadhttps://www.blogger.com/profile/03077136169556206289noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-11117835559144138542015-11-22T09:12:14.173+05:302015-11-22T09:12:14.173+05:30sed -n '10p;11p;16p;17p;' filesed -n '10p;11p;16p;17p;' fileGuru Prasadhttps://www.blogger.com/profile/07898245472802947101noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-82907783075978105982015-11-19T21:26:20.506+05:302015-11-19T21:26:20.506+05:30How do you "sed" out multiple particular...How do you "sed" out multiple particular lines?<br /><br />Say I want to sed line # 10,11,16,17,38,37 and 40?Anonymoushttps://www.blogger.com/profile/07145678141946394101noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-61427417163822578712015-11-15T06:28:13.533+05:302015-11-15T06:28:13.533+05:30Refer to example 23Refer to example 23Guru Prasadhttps://www.blogger.com/profile/07898245472802947101noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-6852101727095418062015-11-06T01:49:29.760+05:302015-11-06T01:49:29.760+05:30what is the command to find a word in a line, then...what is the command to find a word in a line, then deleting (that line and previous line)<br /><br />example, I want to search for the word JDK, delete the JDK line and the previous line.<br /><br />INPUT <br />-------------------------<br />Version 5.1<br />Version 5.1.1.12<br />ID BASE<br />Version 1.4.2<br />ID JDK<br />Version 5.1<br />Version 1.4.2<br />ID JDK<br />Version 5.1.1.12<br />ID ND<br /><br /><br />OUTPUT<br />---------------------------<br />Version 5.1<br />Version 5.1.1.12<br />ID BASE<br />Version 5.1<br />Version 5.1.1.12<br />ID NDAmirhttps://www.blogger.com/profile/03450242659329776304noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-76064907506611490702015-10-29T22:48:13.903+05:302015-10-29T22:48:13.903+05:30This is a typical candidate for awk:
awk '$2 ...This is a typical candidate for awk:<br /><br />awk '$2 !="Prashant"' fileGuru Prasadhttps://www.blogger.com/profile/07898245472802947101noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-11355528790830705242015-10-16T22:26:38.560+05:302015-10-16T22:26:38.560+05:30Hi Guru,
Help me out in this please.
i have text...Hi Guru,<br /><br />Help me out in this please.<br /><br />i have text file abc.txt<br />L1137Q1 shantanu 040013.15 L Prashant .00<br />L134AQ1 Prashant .00 L shantanu 040013.15<br />L1337Q1 shantanu 040013.15 L Prashant .00<br /><br />i need to delete line containing pattern " Prashant" in 2nd coloumn only,<br /><br />output must be<br /><br />L1137Q1 shantanu 040013.15 L Prashant .00<br />L1337Q1 shantanu 040013.15 L Prashant .00<br /><br />Thank you in advance.Jayanthhttps://www.blogger.com/profile/14890362876252215794noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-17596623135544958722015-09-29T22:59:39.441+05:302015-09-29T22:59:39.441+05:30To get from 5th line, you can do like this:
sed &...To get from 5th line, you can do like this:<br /><br />sed '5,${/Header/,/- - -/d;}' a.txt<br /><br />Alternatively, if the content of a.txt is something you got out of a sql query, by adding the "set pagesize 0", automatically you will get the header only once..<br />Guru Prasadhttps://www.blogger.com/profile/07898245472802947101noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-82023088983091723712015-09-29T17:40:28.829+05:302015-09-29T17:40:28.829+05:30Totally agree with Ivan's comment above.Thank ...Totally agree with Ivan's comment above.Thank you very much Guru.<br /><br />I have a.txt as follows and I am looking for b.txt<br />Header<br />Page: 1<br />column1 column2<br />- - - - - - - -<br />data11 data21<br />data12 data22<br />data13 data23<br />data14 data24<br />Header<br />Page: 2<br />column1 column2<br />- - - - - - - -<br />data15 data25<br />data16 data26<br />data17 data27<br />data18 data28<br />Header<br />Page: 3<br />column1 column2<br />- - - - - - - -<br />data19 data29<br />data20 data30<br /><br />Expected output b.txt<br /><br />Header<br />Page: 1<br />column1 column2<br />- - - - - - - -<br />data11 data21<br />data12 data22<br />data13 data23<br />data14 data24<br />data15 data25<br />data16 data26<br />data17 data27<br />data18 data28<br />data19 data29<br />data20 data30<br /><br />I tried with sed '/Header/,/---/'d a.txt >b.txt<br />but in that process I am losing first occurance of header as well.<br />I wish to start above command from line 5, however I could not build one. <br />Tried sed '5,/Header/,/---/'d a.txt >b.txt, but its seems wrong syntax.<br /><br />any inputs, help?<br />Mandarhttps://www.blogger.com/profile/06380507499665961611noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-23850569991086910732015-09-19T21:29:27.263+05:302015-09-19T21:29:27.263+05:30This is gold information . Thank you for sharing i...This is gold information . Thank you for sharing it with us.Anonymoushttps://www.blogger.com/profile/01533864264228227856noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-52377887593787334972015-04-20T10:57:23.572+05:302015-04-20T10:57:23.572+05:30Thanks for the help. I'll try this and get bac...Thanks for the help. I'll try this and get back.Anurag Tiwaryhttps://www.blogger.com/profile/13061677899864186258noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-79363955458642826452015-04-18T18:32:42.334+05:302015-04-18T18:32:42.334+05:30Anurag,
A simple sed command like the following ca...Anurag,<br />A simple sed command like the following can accomplish this. First I created a short sample file with two matches and also one similar path starting with "d" instead of "c".<br /><br />$ more inputfile<br />This is a sample file with heading<br />c:\abc\bcd\rest\of\path<br />c:\abc\bcd\more<br />d:\abc\bcd\more<br />d:\abc\def\ghi<br /><br />My sed command:<br />sed 's&c:\\abc\\bcd\\&&' inputfile > outputfile<br /><br />The resulting output is:<br />$ more outputfile<br />This is a sample file with heading<br />rest\of\path<br />more<br />d:\abc\bcd\more<br />d:\abc\def\ghi<br /><br />- The "&" is used as a sed delimiter to make it more readable, since your pattern includes backslashes.<br />- The double backslashes "\\" are necessary since \ is used to escape special characters.<br />Hope this helps!<br />Rrsantinkhttps://www.blogger.com/profile/01517284896806095174noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-9714531269495308852015-04-15T13:29:36.426+05:302015-04-15T13:29:36.426+05:30I have a csv file which contains 65000 lines (Size...I have a csv file which contains 65000 lines (Size approximately 28 mb). In each of the lines certain path in the beginning is given e.g. "c:\abc\bcd\def\123\456". Now let say the path "c:\abc\bcd\" is common in all the lines and rest of the content is different. What i have to do is to remove the common part (In this case "c:\abc\bcd\") from all the lines.<br /><br />Can any of you please help me out with this ? Also apologies in advance if i have mistakenly not followed any of the forum rules to ask this question.Anurag Tiwaryhttps://www.blogger.com/profile/13061677899864186258noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-3976865055166803772015-01-23T04:41:20.191+05:302015-01-23T04:41:20.191+05:30Hi Guru,
I have the following text in one file
pa...Hi Guru,<br /><br />I have the following text in one file<br />pattern1<br />line1<br />line2<br />pattern2<br />line3<br />line4<br />line5<br />pattern1<br />line6<br />line7<br />pattern2<br />line8<br />line9<br />pattern1<br />line10<br />pattern2<br /><br />I want to replace the first occurrence of the block between pattern1 and pattern2 with some-text and delete the remaining 2 occurrences of the matching block. So the output should look like this:<br />some-text<br />line3<br />line4<br />line5<br />line8<br />line9<br /><br />Help please. Thanks!Preeti Vhttps://www.blogger.com/profile/06107097675106149492noreply@blogger.comtag:blogger.com,1999:blog-1255024703457423340.post-73140013699980485952014-12-04T11:02:58.094+05:302014-12-04T11:02:58.094+05:30Nice artical GuruNice artical GuruSagar Autihttps://www.blogger.com/profile/12053213126810615141noreply@blogger.com