Sed is a command-line stream editor for filtering and transforming text.

learn more… | top users | synonyms

3
votes
2answers
2k views

sed or tr one-liner to delete all numeric digits

So, I have this textfile, and it consists of mostly alphanumeric characters. It's a standard document. But since I copied it and pasted it from a PDF, there are page numbers in there. I don't much ...
2
votes
3answers
3k views

Extracting tokens from a line of text

Using bash scripting and grep/awk/sed, how can I split a line matching a known pattern with a single character delimiter into an array, e.g. convert token1;token2;token3;token4 into a[0] = token1 … ...
3
votes
2answers
679 views

Using grep/sed/awk to classify log file entries

I need to process a very large log file with many lines in different formats. My goal is to extract unique line entries who have the same starting pattern, e.g. '^2011-02-21.*MyKeyword.*Error', ...
7
votes
4answers
10k views

Copy/rename multiple files using regular expression (shell script)

I have a collection of files matching a pattern such as 'assignment02.cc', 'assignment02.h', 'assignment02.txt', etc. I would like to copy/rename these files into 'assignment03.cc', 'assignment03.h', ...
9
votes
2answers
3k views

How to remove multiple lines per occurrence in a file?

Say I have this 857835 line file, containing stuff like this: a1 rubbish1 rubbish2 rubbish3 rubbish4 a1 rubbish5 rubbish6 rubbish7 rubbish8 And I wish to remove all occurences of a1 and the next ...
5
votes
3answers
1k views

Sed to reverse two words

echo "AXIS2C_HOME=/usr/local/Axis2C" | sed 's/\(^AXIS2C_HOME=\) \(.*\)/ \2 \1/' The output I am expecting is /usr/local/Axis2C AXIS2C_HOME=. I can't figure out what I am doing wrong. :(
14
votes
10answers
3k views

There must be a better way to replace single newlines only?

I am in the habit of writing one line per sentence because I typically compile things to LaTex, or am writing in some other format where line breaks get ignored. I use a blank line to indicate the ...
6
votes
4answers
10k views

Processing bash variable with sed

Been banging my head off a wall on this bash variable LATLNG contains a latitude & longitude value in brackets like so (53.3096,-6.28396) I want to parse these into a variable called LAT and ...
1
vote
4answers
2k views

Processing control characters

I have a log file which contains a bunch of non visible control characters such as hex \u0003. I would like to replace this using something like SED, but can't get the first part of the regex to ...
1
vote
4answers
1k views

Debian sed: can't read

I'm trying to run a command to remove require_once from php files from here (code below), but I am getting the error: sed: can't read -: No such file or directory I am in the correct folder; ...
5
votes
4answers
227 views

How could I simplify this command to only use awk?

awk '/user/ {print $1 }' /etc/userdomains | sed 's/://' the format of /etc/userdomains is domain.tld: user otherdomain.tld: otheruser
5
votes
3answers
512 views

Why does sed act differently depending on the output file?

If I run: cat messages.txt | sed -e 's/a/a/g' > messages.txt on one large file (2500+ lines) I find that the resulting file will only have about 900 lines after the command in cygwin and will ...
3
votes
3answers
2k views

Substitute part of text file using bash script

I'm writing a shell script (bash) to fetch and build several bits of software. The script also writes several small config files and needs to alter a couple of pre-existing config files. What is the ...
55
votes
7answers
16k views

Is there a basic tutorial for grep, awk and sed? [closed]

I've been a Linux user for a while, and I've a pretty decent understanding of most the common command line utilities. However, ones that come up and up again in relation to programming are grep, awk, ...
1
vote
3answers
235 views

Directory filenames with sed with whitespaces

I'm working on a script and I'm stuck, even with the help of teh googles. Here's my code: for FOLDER in `find . -type d | sed "s#^.#$(pwd)#" | sed 's/ /\ /g'` do echo "$FOLDER" done This will ...
8
votes
6answers
240 views

Grab certain contents of a file

So I know tools exist for this problem because I've heard about them, but I don't know what they are. I want to do something like filter out all data but the usernames in /etc/passwd. For example, I ...
5
votes
1answer
2k views

Replacing multiple lines in sed or awk [closed]

I'm trying to use sed or awk to replace 5 lines in a smb file but I just don't have any idea how to deal with the newlines.
4
votes
4answers
5k views

Adding a line of text to multiple files

So, I have a bunch of files in a directory, and I need to insert a line of text into each of them. They have essentially the following format: <VirtualHost *:80> ServerAdmin ...
8
votes
4answers
9k views

grep — removing text after delimiter token

I have a file in which I need to eliminate everything after the first ; on every line. So a file like this: sdfsdsdf; fsdfsddf;sdfsd; Will result in this: sdfsdsdf fsdfsddf I have looked into ...
15
votes
6answers
22k views

SED: insert something to the last line?

The command inserts a tag to the beginning of a file: sed -i "1s/^/<?php /" file How can I insert something to the end of each file with SED?
14
votes
7answers
1k views

How can I prepend a tag to the beginning of several files?

I need to add PHP tags surrounding a file. It's easy to append them using find . -exec echo "?>" >> '{}' \; but how can I prepend the tag <?php?
7
votes
3answers
224 views

Locate postion then make a change using sed

This script uses sed to change all "" to "new stuff". How would one change just the "" after the yyy: using sed or anything else? cat >sample.txt <<EOF xxx: "" yyy: "" } EOF sed ...