tag:blogger.com,1999:blog-8032022811235182759.post8292536410278539357..comments2024-03-23T08:25:23.254-07:00Comments on Linux Commando: Using sed to extract lines in a text filePeter Leunghttp://www.blogger.com/profile/05589860210899238688noreply@blogger.comBlogger62125tag:blogger.com,1999:blog-8032022811235182759.post-50232603322019540562021-06-14T01:47:27.677-07:002021-06-14T01:47:27.677-07:00Hi guys!
I have a Jenksinsfile and i want to print...Hi guys!<br />I have a Jenksinsfile and i want to print and run fews commands from the Jenkinsfile in a script file <br />Can idea how i can do that?Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-80151116753845692302020-11-06T10:37:01.606-08:002020-11-06T10:37:01.606-08:00Thanks a lot for this info, couldn`t find a specif...Thanks a lot for this info, couldn`t find a specific line and the sed 'n!d' filename realy helped me!Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-20792523072797615142020-08-12T03:48:03.181-07:002020-08-12T03:48:03.181-07:00Hi,
My log file would be like mentioned below:
Fro...Hi,<br />My log file would be like mentioned below:<br />From that i have to refer only last "timestamp": and its timestamp, and pull the log between two timerange.<br />For example, if i want to collect log between 23:32 and 23:35. It should refer only last "timestamp": and pull the log between those time range.<br /><br />somecontent"TransDateTime\":\"2020-07-01T09:15:01.000Z","receiveTimestamp":"2020-07-01T02:15:01.335142083Z","textPayload":"[7/1/20 23:05],","timestamp":"2020-07-01T23:32:35.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:02.000Z","receiveTimestamp":"2020-07-01T02:15:02.335142083Z","textPayload":"[7/1/20 23:06],","timestamp":"2020-07-01T23:32:36.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:03.000Z","receiveTimestamp":"2020-07-01T02:15:03.335142083Z","textPayload":"[7/1/20 23:07],","timestamp":"2020-07-01T23:34:35.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:04.000Z","receiveTimestamp":"2020-07-01T02:15:04.335142083Z","textPayload":"[7/1/20 23:08],","timestamp":"2020-07-01T23:34:36.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:05.000Z","receiveTimestamp":"2020-07-01T02:15:05.335142083Z","textPayload":"[7/1/20 23:09],","timestamp":"2020-07-01T23:35:35.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:06.000Z","receiveTimestamp":"2020-07-01T02:15:06.335142083Z","textPayload":"[7/1/20 23:10],","timestamp":"2020-07-01T23:35:36.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:07.000Z","receiveTimestamp":"2020-07-01T02:15:07.335142083Z","textPayload":"[7/1/20 23:11],","timestamp":"2020-07-01T23:36:36.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:08.000Z","receiveTimestamp":"2020-07-01T02:15:08.335142083Z","textPayload":"[7/1/20 23:11],","timestamp":"2020-07-01T23:36:37.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:09.000Z","receiveTimestamp":"2020-07-01T02:15:09.335142083Z","textPayload":"[7/1/20 23:12],","timestamp":"2020-07-01T23:37:10.8",somecontent<br />somecontent"TransDateTime\":\"2020-07-01T09:15:10.000Z","receiveTimestamp":"2020-07-01T02:15:10.335142083Z","textPayload":"[7/1/20 23:13],","timestamp":"2020-07-01T23:37:15.8",somecontent<br /><br /><br />I tried sed command, <br />cat "filename" | sed -r 's/"timestamp":([^ ]+).*/\1/' <br />But its trimming the word timestamp, not collecting the log log file.<br /><br />Your early response is really appreciated.<br />Thanks in advance...<br />Thanks in advance.Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-8391044294745480762020-05-15T04:55:35.603-07:002020-05-15T04:55:35.603-07:00Hi Guys,
i am new to greping etc
I have a Hugh te...Hi Guys,<br />i am new to greping etc<br /><br />I have a Hugh text file from which i want to output a given number of lines text upon locating a line containing the { character<br /><br />example <br /><br />some text line 1<br />some text line 2<br />some text line 3<br />some text line 4<br />some text line 4<br /><br />{some more text line 6}<br />--------<br />some text line 8<br />some text line 9<br />some text line 10<br />some text line 11<br />some text line 12<br />some text line 13<br />some text line 14<br />some text line 15<br /><br />{some more text line 17}<br />------------------<br />some text line 19<br />some text line 20<br />some text line 21<br />some text line 22<br />some text line 23<br />some text line 24<br />some text line 25<br />some text line 26<br /><br /><br />output should be <br /><br />{some more text line 6}<br />--------<br />some text line 8<br />some text line 9<br /><br />{some more text line 17}<br />------------------<br />some text line 19<br />some text line 20<br /><br />Garfieldhttps://www.blogger.com/profile/16818620004403666537noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-68485167903527419452019-07-19T06:54:52.009-07:002019-07-19T06:54:52.009-07:00Hi,
I have file containing the following lines
0...Hi,<br /><br />I have file containing the following lines<br /><br />000101.html: <a href="http://solnet.sypsys.com/retrieve/009009.html" rel="nofollow"></a><br />000612.html:during ATPG, refer to the Solet article: <br /><a href="https://solnet.sypsys.com/retrieve/901030.html/" rel="nofollow">Difference Between the add cell const O</a><br />000612.html:regenerating a new set of vectors. <br /><a href="https://solnet.sypsys.com/retrieve/901006.html%22" rel="nofollow">Mask Patterns Without Rerunning ATPG</a><br />000636.html: <a href="http://solnet.sypsys.com/retrieve/000101.html" rel="nofollow">000101</a><br /><br />From the above excerpt I need to extract only the filename available towards the end of href tag - I want the following to be displayed:<br /><br />009009.html<br />901030.html<br />901006.html<br />000101.html<br /><br />If possible I also want to get rid of the .html towards the end and only display:<br /><br />009009<br />901030<br />901006<br />000101<br /><br />Can someone please help me with this requirement. Thanks in advance.<br /><br />-Shyamshyambhttps://www.blogger.com/profile/16325630138903563989noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-57066151295915504692018-08-09T06:34:27.319-07:002018-08-09T06:34:27.319-07:00Nice post. I had previously known about the `sed ...Nice post. I had previously known about the `sed '`1!d' file` command, but I had to update somebody else's .csh script. In .csh the `sed '1!d' file` command doesn't seem to work (it gives the error "d: Event not found.".<br /><br />So thank you for providing the alternative of `sed -n 1p file`!Matthttp://mattdturner.com/wordpressnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-17896508308392065542018-07-20T20:20:38.820-07:002018-07-20T20:20:38.820-07:00Totally late coming, but if all you want to do is ...Totally late coming, but if all you want to do is print the first so many lines you can just do<br /><br />sed 11q somefile.txt<br /><br />If you want N lines replace "11" with N+1 .Brianhttps://www.blogger.com/profile/05197436427375831411noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-42737304209421626232017-05-04T17:15:53.325-07:002017-05-04T17:15:53.325-07:00Under Win10x64, ssed (http://sed.sourceforge.net/g...Under Win10x64, ssed (http://sed.sourceforge.net/grabbag/ssed/) seems a lot faster than head or sed (from UnxUtils). Tested under TCC/LE 14 shell (JpSoft).Nanoo Visotorhttps://www.blogger.com/profile/10992892573232232096noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-87664679521789730092017-03-08T20:36:59.227-08:002017-03-08T20:36:59.227-08:00Hi, I have a question. Assume a file with 30 lines...Hi, I have a question. Assume a file with 30 lines. I want to extract 12th character of 4th line. how can this be accomplished?Anonymoushttps://www.blogger.com/profile/05255496747032282266noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-21878955418019624552017-03-04T21:58:29.180-08:002017-03-04T21:58:29.180-08:00Eight years ago, Alan posted the question: "H...Eight years ago, Alan posted the question: "How to delete line 5 if it contains some string?"<br /><br />The blog author Peter Leung gave answer in awk. Since the blog is about sed, a more consistent answer is to solve it using sed thus:<br /><br />sed -n '5!bL0;/some string/d;:L0;p' file.txt<br /><br />Explanation: Sed supports if-else conditional branching. Here, i created a label 'L0'. The solution means: If not line 5 then goto label L0 and just print the line. Else, check for presence of 'some string' and delete entire line 5 if that string is found.Anonymoushttps://www.blogger.com/profile/13747653319038000509noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-86425658340703726342015-08-28T14:11:23.276-07:002015-08-28T14:11:23.276-07:00what if I want to search for a text string, and th...what if I want to search for a text string, and then display the lines containing a second string that it falls between.<br /><br />For example, if I have the file foo.txt:<br /><br />a<br />b<br />c<br />----<br />d<br />e<br />f<br />---<br />g<br />h<br />i<br />j<br />---<br />k<br />l<br />---<br />m<br />---<br /><br />say I want to grep for c and display all the lines between --- <br /><br />The result should be:<br />---<br />a <br />b<br />c<br />---<br /><br />How would i do that?kevinhttps://www.blogger.com/profile/01178644337092630895noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-68089825449479658932015-08-27T01:17:19.673-07:002015-08-27T01:17:19.673-07:00There probably is a more elegant sed-only way to d...There probably is a more elegant sed-only way to do this but I would solve it with a bash script like this:<br /><br />for files in *.conf; do <br /> sed -n 14p "$files"<br />done<br /><br />Or as a one-liner: <br />for files in *.conf; do sed -n 14p "$files"; done<br />Christofhttps://www.blogger.com/profile/02693548945017707988noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-5308029284185903002015-08-21T23:56:20.246-07:002015-08-21T23:56:20.246-07:00Hello all,
I'm trying to run a sed command to ...Hello all,<br />I'm trying to run a sed command to recursively print out line 14 from all files in a directory, can someone advise how I can achieve this?<br /><br />I've tried this command but its only showing the first result:<br />sed -n 14p *.confAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-46140120306727605282015-07-20T00:21:56.711-07:002015-07-20T00:21:56.711-07:00Dear Nutrisi Wajib Untuk Ibu Hamil,
You could try ...Dear Nutrisi Wajib Untuk Ibu Hamil,<br />You could try using paste command:<br />paste - - - < filenameMark Ziemannhttps://www.blogger.com/profile/00623549232702735102noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-17795711873941059272014-11-17T01:16:39.743-08:002014-11-17T01:16:39.743-08:00I have a file of 9 lines say as below.
1
2
3
4
5
6...I have a file of 9 lines say as below.<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /><br /><br />I want to 3 lines in a single line lik mentioned below<br />123<br />456<br />789<br /><br />Can you help me with thisNutrisi Wajib Untuk Ibu Hamilhttp://goo.gl/IQTLQAnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-64202993301146627882014-11-17T01:14:39.721-08:002014-11-17T01:14:39.721-08:00great article sirgreat article sirNutrisi Wajib Untuk Ibu Hamilhttp://goo.gl/IQTLQAnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-32089594939578885192014-11-06T05:05:05.884-08:002014-11-06T05:05:05.884-08:00I have a file of 9 lines say as below.
1
2
3
4
5
6...I have a file of 9 lines say as below.<br />1<br />2<br />3<br />4<br />5<br />6<br />7<br />8<br />9<br /><br /><br />I want to 3 lines in a single line lik mentioned below<br />123<br />456<br />789<br /><br />Can you help me with thisAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-17550631160403437012014-10-28T20:29:27.884-07:002014-10-28T20:29:27.884-07:00Hi, Dana
See this post:
http://linuxcommando.blogs...Hi, Dana<br><br />See this post:<br />http://linuxcommando.blogspot.ca/2008/04/use-sed-or-perl-to-extract-every-nth.html<br /><br /><br> PeterPeter Leunghttps://www.blogger.com/profile/05589860210899238688noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-65271087191484330962014-10-28T19:57:38.143-07:002014-10-28T19:57:38.143-07:00So what if I had a huge file and I wanted to extra...So what if I had a huge file and I wanted to extract, say, every 4th line?<br /><br /><a href="http://goo.gl/RLKc1m" rel="nofollow">Dana Cepat</a>Mas Ucheng 17++http://facebook.comnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-10077526545479802512013-12-11T14:10:53.859-08:002013-12-11T14:10:53.859-08:00Hi, I want to extract lines from a file such that ...Hi, I want to extract lines from a file such that it extracts all the lines from line 1 to the line it encounters a string like "dump" in a new file. How can I do that ?<br /><br />ThanksAnonymoushttps://www.blogger.com/profile/07758690022908996577noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-87464754735911327152013-11-08T11:55:39.191-08:002013-11-08T11:55:39.191-08:00Hi Peter,
Thanks for the original article and for...Hi Peter,<br /><br />Thanks for the original article and for still answering questions 5.5 years later!!<br /><br />You've come very close to answering my question but not quite. If I want to extract certain lines you suggest using <br />$ sed -n -e 1,2p -e 4p somefile.txt<br /><br />Now, what if I have a very large file and need to extract a subset of the lines, the index of which is too long to type manually. <br /><br />Can I somehow use an index file combined with the above?<br /><br />Thanks in advance and I understand if I don't hear from you, you've already gone above and beyond.Anonymoushttps://www.blogger.com/profile/03958177568335668562noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-22764255320117280192013-09-17T13:51:41.715-07:002013-09-17T13:51:41.715-07:00cat file.txt | head -$desiredline | tail -1
for l...cat file.txt | head -$desiredline | tail -1<br /><br />for line 12<br /><br />cat file.txt | head -12 | tail -1Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-67843121674875286122013-09-04T02:15:20.187-07:002013-09-04T02:15:20.187-07:00I wish to extract all lines from a text file, whic...I wish to extract all lines from a text file, which contains a particular word.wholesale soccer Jerseyhttp://www.replicajerseysoccer.com/noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-25889186321225841922013-08-12T18:23:20.583-07:002013-08-12T18:23:20.583-07:00Srikanth
How about this?
sed -n -e '/http/ p...Srikanth<br /><br />How about this?<br /><br />sed -n -e '/http/ p' yourfilePeter Leunghttps://www.blogger.com/profile/05589860210899238688noreply@blogger.comtag:blogger.com,1999:blog-8032022811235182759.post-64841885614298751382013-08-12T01:54:08.031-07:002013-08-12T01:54:08.031-07:00Hello,
I wish to extract all lines from a text f...Hello,<br /> I wish to extract all lines from a text file, which contains a particular word.<br /><br />like:<br /> i wnat to print all the lines which contain "http"<br /><br />How can I do this?Srikanth G Knoreply@blogger.com