Most of the commandline tools I'm looking at have the ability to pick a field delimiter.  However, I'd like to pick one delimiter to start, and a different one to end the segment of text I'd like to remove from each line I'm processing.  1text [blah blah blah] text number punctuation text text 2text text text 3text text (text) [blah blah blah] number text 4text &lt;url&gt; &lt;email&gt; text [blah blah blah] text   I'd like to remove all the 'blah blah blah' from those lines.  Blah can contain anything, except newlines, EOFs, and other breaky-things, and '['.  ie: I shouldn't have '[[' (nor '[blah[') in any of the data  I only have one (optional) instance of [] per line.  So, for line 2 there is nothing to remove, and this shouldn't cause a halt, stop or failure.  I'm almost 100% positive that if I've got a start '[' I also have a ']'.  That might be nice to check for, however.  There are other forms of punctuation, so I don't want to work it with something that just looks for non-alphanumeric stuff to start removing (ie: line 4)  Bonus points for being able to figure out if I'm putting together two (now adjacent) whitespaces at that particular point - but without removing double whitespaces at any other point.  I'm pretty sure I'll have to use awk or sed, but if there were a way to do this via regular commandline tools, to make it as portable as possible, that would be ideal.  Also, explaining what you're doing (if you're using regex / sed) would certainly help, as:    A suggestion  says:   sed 's/^.*%\([^ ]*\) .*\$\([^$]*\)$/\1 \2/' infile    I got that kinda working with this bit of monkeying:   cat data | sed 's/^.*\[\([^ ]*\) .*\]\([^$]*\)$/\1 \2/'    However it doesn't take out the whole swath of 'blah blah blah', and leaves with an extra line-break.      Doesn't really answer the question in a general sense (or, at least I wasn't able to figure something out after reading it - maybe just a fail on my part), but seems to be (too) specifically tailored to that person's data.
123868
I am working with some text that is full of stuff between brackets [] that I don't want. Since I can delete the brackets myself, I don't need the one-liner to do that for me, but I do need a one-liner that will remove everything between them.  What about parentheses () instead of brackets?
21864
I've been using sed for quite some time but here is a quirk I came around with, which I am not able to resolve.   Let me explain my problem with the actual case.    Scene#1  printf "ls" | xclip -selection clipboard echo "ls" | xclip -selection clipboard   In the first command, I pipe printf output to xclip so that it gets copied to the clipboard. Now, printf, unlike echo does not insert a new line at the end by default. So, if I paste this content into terminal, the ls command that is copied does not automatically run.   In the second, there is a new line at the end, so pasting the clipboard content also results in the running of the command in the clipboard.   This is undesirable for me. So, I wanted to remove the newline using sed, but it failed, as explained in the scene below.  Scene#2  echo "ls" | sed -r 's/\n//g' | xclip -selection clipboard   The content in the clipboard still contains new-line. When I paste it into terminal, the command automatically runs.   I also tried removing carriage return character \r. But nada. It seems I am missing something very crucial/basic here.
92395
I want to split a command line like string in single string parameters. How look the regular expression for it. The problem are that the parameters can be quoted. For example like:  "param 1" param2 "param 3"  should result in:  param 1, param2, param 3
37813
I want to split 'hello' into h e l l o in an array using only bash, I could do it in sed with sed 's/./&amp; /g' but I want to know how to split a string into an array in Bash when I do not know what the delimiter would be, or the delimiter is any single character.  I don't think I can use ${i// /} without some creativity because the delimiter is an unknown, and I don't think that expression accepts regex.  I tried using BASH_REMATCH with [[ string =~ ([a-z].).* ]] but it doesn't work as I expected.  What is the proper way to use only bash to accomplish a string.split() type of behavior?  The reason is that I am trying to write the rev utility in all bash:    while read data; do   word=($(echo $data|tr ' ' '_'|sed 's/./&amp; /g'))   new=()   i=$((${#word[@]} - 1))   while [[ $i -ge 0 ]]; do     new+=(${word[$i]})     (( i-- ))   done   echo ${new[@]}|tr -d ' '|tr '_' ' '   done   But I used tr and sed, I want to know how to do the split properly and then I will fix it to be all bash.  Just for fun.
106505
Using sed, how do I search for a line ending with foo and then edit the next line if it starts with #bar?  Or put another way, I want to remove a comment # from the next line if it starts with #bar and the previous line ends in foo.  For example:  This is a line ending in foo #bar is commented out There are many lines ending in foo #bar commented out again   I tried:  sed -i 's/^foo\n#bar/foo\nbar/' infile
87368
I did tried sed and awk, but its not working as the character involves / which is already there in command as delimiter. Please let me know how can I achieve this. Below is a sample Example. We want to remove the commented sections, i.e /*.....*/ /*This is to print the output data*/ proc print data=sashelp.cars; run; /*Creating dataset*/ data abc; set xyz; run;
44760
I need to repeatedly remove the first line from a huge text file using a bash script.  Right now I am using sed -i -e "1d" $FILE - but it takes around a minute to do the deletion.  Is there a more efficient way to accomplish this?
6721
I ended up writing a quick little script for this in Python, but I was wondering if there was a utility you could feed text into which would prepend each line with some text -- in my specific case, a timestamp.  Ideally, the use would be something like:  cat somefile.txt | prepend-timestamp   (Before you answer sed, I tried this:  cat somefile.txt | sed "s/^/`date`/"   But that only evaluates the date command once when sed is executed, so the same timestamp is incorrectly prepended to each line.)
87431
I use sed -n '/string/,$p' file &gt;&gt; otherfile to copy text from a line with certain string to another file.  Now I want the copied text to be deleted in the original file. I tried different methods with sed and awk but nothing worked.  How to use sed to delete the text of a file from the beginning line with a certain string in it?  texta  texta1  textb    &lt;- string, delete rest of the text from here  textb1  textc  textc1   or would it be easier to cut instead of copying with sed and what would be the command instead?
55127
The Unix environment has some excellent tools for parsing text in various forms. However, of late, the data is not in the traditional (historical) formats (CSV, TSV, record-based or some other delimiter-based) it used to be before. Data these days is exchanged in structured formats like XML/JSON.  I know there are some good tools like sed, awk and Perl which can chew down nearly any form of data out there. However, to work with this sort of structured data, often one has to write a complete program, and, given the little time available to extract information, one has to sit down and figure out the whole logic of what one wants to query and put it down programmatically. Sometimes this is not OK – basically because the information extracted from those files acts as inputs for further work; also because of the time it takes to search for the appropriate solution and code it up. A command line tool is needed with sufficient switches to find, query and dump data.  I'm looking for tools that take a XML/JSON or other forms of structured data and dump it into other formats like csv, etc., so that from there one could use other commands to get any information out of it.  Are there any command line utilities you know of which do this kind of a job? Are there already awk/Perl scripts available to this?
82705
I want to delete one or more specific line numbers from a file. How would I do this using sed?
117282
I am trying to append and prepend text to every line in a .txt file.  I want to prepend: I am a   I want to append:  128... [}  to every line.  Inside of a.txt:  fruit, like bike, like dino, like   Upon performing the following command:  $ cat a.txt|sed 'I am a ',' 128... [}'   it does not work how I want it to. I would really want it to say the following:  I am a fruit, like 128... [} I am a bike, like 128... [} I am a dino, like 128... [}
90307
I have a text file as follows:  test1,test2,test3 test4,test5 test6,test7,test8,test9,test10 test11,test12,test13,test14   How can I replace commas with semicolons, starting with the second one (and continuing to the last one)?  I want to get output as follows:  test1,test2;test3 test4,test5 test6,test7;test8;test9;test10 test11,test12;test13;test14
82128
NOTE: This question is the complement of this Q&amp;A:     I need to get only the lines from a textfile (a wordlist, separated with newline) that has a length range of minimum or equal than 3 characters, but not longer or equal than 10.   Example:   INPUT:   egyezményét megkíván ki alma kevesen meghatározó   OUTPUT:   megkíván alma kevesen   Question: How can I do this in bash?
66432
The Markdown editor knows that an empty line, followed by a number, followed by a period, followed by a space, is a list item.  This works great It's almost magic  The problem is that I almost always use a close parenthesis for my lists. I think it's a pretty common practice, but the problem is that the editor / Markdown parser doesn't accept parenthesis as an acceptable list delimiter:  This fails horribly This is on the next line I swear
690
What is the difference between \par, \newline, \\ and blank lines? When should I use which one?
31542
I need to replace single quotes with double quotes AND double quotes with single quotes in over 600 files. I've been reading various topics most of the day. I am hampered by only knowing some basic shell scripting commands and an unfamiliarity with regexp.   Below is a sample file I've made a bit generic.     some text KEYWORD_1 table name column = "string" AND column = "string"     Additional text           .           .           .    some text KEYWORD_2 text 'text in quote' etc.    I only want to change the quotes on the 2 lines with KEYWORDs. I can't guarantee the keywords are always going to be first line and last line. I need my solution to find the keyword and change all the quotes on that line only.   I tried some things I found in sort of similar questions on this forum, including a meta on SED. I tried to figure out how to use this command: sed -i 's/foo\(.*baz\)/bar\1/' file. According to that topic the command, "Replaces foo with bar only if there is a baz later on the same line." My keyword is not later on the same line its at the beginning and I couldn't make it work for me.   I also tried ex -sc 'g/DEL/s/ALF/BRA/g' -cx file that didn't do anything. I'm assuming it can't be executed from the command line. I really don't want to open each of 600 files.  At one point I tried, grep KEYWORD_1 file | sed -i "s/'/\"/g" file Of course, that changed all the quotes in the file not just the one line.   I'm betting the solution is simple and I just can't see it. How can I do this?     As requested my desired output is:  some text KEYWORD_1 table name column = 'string' AND column = 'string'     Additional text           .            .           . some text KEYWORD_2 text "text in quote" etc.