sed

Ignore non-matching lines

How can I make sed filter matching lines according to some expression, but ignore non-matching lines, instead of letting them slip? As a real example, I want to run scalac (the Scala compiler) on a set of files, and read from its -verbose output the .class files created. scalac -verbose outputs a bunch of messages, but we're only intere...

How do I fix this sed problem with unterminated lines?

I'm guessing this has to be an escaping issue, but I can't find it. What's most frustrating is that this is based on an old sed script I used that worked, so why can't I make it work when I try to re-use it a year later? ;) Framework: I have a file with a list of filenames in it that all need the same string of HTML searched and repla...

How to sort by line length, then reverse alphabetically

I have a large (600 odd) set of search and replace terms that I need to run as a sed script over some files. The problem is that the search terms are NOT orthogonal... but I think I can get away with it by sorting by line length (i.e. pull out the longest matches first, and then alphabetically within each length. So given an unsort set o...

How can I replace a class with another class from another module in a lot of files without a lot of manual editing?

Basically, I have a lot of Python classes (representing our database schema) that look something like this: from foo import xyz, b, c class bar(object): x = xyz() y = b() z = c() ...and I want to change it to this: from foo import b, c from baz import foobar class bar(object): x = foobar() y = b() z = c() ...

sed regex to non-greedy replace?

I am aware of another question that is quite similar, but for some reason I'm still having problems. I have a GC log that I'm trying to trim out the Tenured section enclosed in []. 63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs] I...

Extract float from text line with sed?

I am issuing a sed replace on a line, trying to extract a particular floating-point value, but all that appears to be matched is the right-side of the decimal Text Line: 63.544: [GC 63.544: [DefNew: 575K->63K(576K), 0.0017902 secs]63.546: [Tenured: 1416K->1065K(1536K), 0.0492621 secs] 1922K->1065K(2112K), 0.0513331 secs] If I issue s...

sed stream editor unix linux command : how to keep retain a paragraph with a particular text string

Hi I have managed to put text in a file by separating them by blank lines. I am trying to keep only those paragraphs that have a particular string. Though the Sed FAQ mentions a solution it does not work (see examples below) http://www.catonmat.net/blog/sed-one-liners-explained-part-two/ 58. Print a paragraph that contains “AAA”. (P...

The easiest way to replace white spaces with (underscores) _ in bash

Hi, recently I had to write a little script that parsed VMs in XenServer and as the names of the VMs are mostly with white spaces in e.g Windows XP or Windows Server 2008, I had to trim those white spaces and replace them with underscores _ . I found a simple solution to do this using sed which is great tool when it comes to string manip...

How can I delete all /* */ comments from a C source file?

I have a C file which I copied from somewhere else, but it has a lot of comments like below: int matrix[20]; /* generate data */ for (index = 0 ;index < 20; index++) matrix[index] = index + 1; /* print original data */ for (index = 0; index < 5 ;index++) How can I delete all the comments enclosed by /* and */. Sometimes, the comment...

Can you mass edit all files returned in a grep?

I want to mass-edit a ton of files that are returned in a grep. (I know, I should get better at sed). So if I do: grep -rnI 'xg_icon-*' How do I pipe all of those files into vi? ...

How can I delete all lines that do not begin with certain characters?

I need to figure out a regular expression to delete all lines that do not begin with either "+" or "-". I want to print a paper copy of a large diff file, but it shows 5 or so lines before and after the actual diff. ...

Bulk Insert Code Before </body> Tag in 100 Files

I'd like to insert <?php include_once('google_analytics.php'); ?> before the closing body tag of about 100 php files. Unfortunately the person who made the site didn't make a header or footer template. What is the best way to do this? I've tried using grep/find for getting a list of files and piping the results through xargs to se...

how to use sed, awk, or gawk to print only what is matched?

I see lots of examples and man pages on how to do things like search-and-replace using sed, awk, or gawk. But in my case, I have a regular expression that I want to run against a text file to extract a specific value. I don't want to do search-and-replace. This is being called from bash. Let's use an example: Example regular express...

Showing only the uptime from uptime [unix]

I'd like to trim the output from uptime 20:10 up 23 days, 3:28, 3 users, load averages: 3.84 1.06 0.64 so that it just shows: 23 days I've tried using sed, but I'm not sure it's the right tool for the job, and don't have much experience using it. How can I achieve the output I want? ...

Parse players currently in lobby

I'm attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined). Note that Samual has rejoined. [Sun Nov 15 14:12:50 2009] [GAME: Tower Defense Join Fast!] player [Samual|192.168.1.115] joined the ga...

Strip Characters Before Period If Filename Has Prefix in Bash

I have a directory that looks like this: pages/ folder1/ folder1.filename1.txt folder1.filename2.txt folder2/ folder2.filename4.txt folder2.filename5.txt folder3/ filename6.txt I want it to look like this: pages/ folder1/ filename1.txt filename2.txt folder2/ filename3.txt filename4.txt folder3/ filename5.txt...

text manipulation and removal

I have text files generated by one of my tools with structure shown below. 1 line text (space) multiple lines text (space) multiple lines text nr 2 ---------------------------------------------------------- (58 '-' characters) different 1 line text (space) different multiple lines text (space) different multiple lines text nr 2 --------...

How to use sed to return something from first line which matches and quit early?

I saw from http://stackoverflow.com/questions/148451/how-to-use-sed-to-replace-only-the-first-occurrence-in-a-file How to do most of what I want: sed -n '0,/.*\(something\).*/s//\1/p' This finds the first match of something and extracts it (of course my real example is more complicated). I know sed as a 'quit' command which will ma...

Overwriting /boot/grub/menu.lst?

Before overwriting I have copied /boot/grub/menu.lst to /home/san. I am running sed on the /home/san/menu.lst just for testing. How can i overwrite default 0 to default 1 with the help of sed. I used following commands but none worked. It's most probably because i don't how many spaces are there in between "de...

sed script to delete all characters up to & including the 2nd comma on a line

Can anyone explain how to use sed to delete all characters up to & including the 2nd comma on a line in a CSV file? The beginning of a typical line might look like 1234567890,ABC/DEF, and the number of digits in the first column varies i.e. there might be 9 or 10 or 11 separate digits in random order, and the letters in the second colu...