Questions tagged [sed]

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

Filter by
Sorted by
Tagged with
28 votes
5 answers
71k views

Multiline pattern match using sed, awk or grep [duplicate]

Is it possible to do a multiline pattern match using sed, awk or grep? Take for example, I would like to get all the lines between { and } So it should be able to match 1. {} 2. {.....} 3. {........
user avatar
-9 votes
6 answers
535 views

Manipulating a file with sed

I have a file called students.txt and it contains the following data in the format Surname, Forename: day.month.year: Degree: Smith, John: 15.01.1986: MSc IT Taylor, Susan: 04.05.1987: MSc IT ...
user avatar
  • 197
3 votes
3 answers
425 views

Remove empty configuration section

Files like ~/.config/vlc/vlcrc are 99% junk if you want to version control only the configuration options. I've got a script to remove the comments, but there's a ton of empty configuration sections ...
user avatar
  • 46.8k
2 votes
2 answers
673 views

non-case-sensitive sed - OpenWrt

the input would be like this in a file: <sCripT and the parsing looks like this: sed -i "s/<script//g" SOMETHING.html the "/gi" is not good, because OpenWrt doesn't knows it (busybox..): ...
user avatar
  • 36.1k
35 votes
9 answers
18k views

How to delete line if longer than XY?

How can i delete a line if it is longer than e.g.: 2048 chars?
user avatar
  • 36.1k
17 votes
2 answers
75k views

How to replace quotation marks in a file with sed?

I have a file that contains multiple lines of xml. I would like to replace certain parts of the file. Some parts of the file contains quotation marks (") which I would like to replace. I have been ...
user avatar
7 votes
4 answers
38k views

How can I find and replace with a new line?

I have a CSV delimited by commas and I want to delimit it by newlines instead. Input: a, b, c Output: a b c I've written Java parsers that do this stuff, but couldn't this be done with vim or ...
user avatar
  • 541
3 votes
3 answers
2k views

Get last set of non-empty lines

I'm running a cron job which should get only the last result of iostat -d 1 2. This requires some parsing: What is the simplest way to get the last set of non-empty lines from standard input to ...
user avatar
  • 46.8k
19 votes
5 answers
27k views

Zero-fill numbers to 2 digits with sed

Input: 201103 1 /mnt/hdd/PUB/SOMETHING 201102 7 /mnt/hdd/PUB/SOMETH ING 201103 11 /mnt/hdd/PUB/SO METHING 201104 3 /mnt/hdd/PUB/SOMET HING 201106 1 /mnt/hdd/PUB/SOMETHI NG Desired output: 201103 01 ...
user avatar
  • 36.1k
2 votes
4 answers
3k views

Filtering paths to a specific depth

I'm using rsync and the flags -nPaAXz ~/ to check which files are going to be copied. This is far too verbose to make any sense of. How could I filter the output so I view the list of files/folders ...
user avatar
  • 785
5 votes
2 answers
11k 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 ...
user avatar
  • 12.8k
5 votes
3 answers
17k views

Extracting tokens from a line of text [closed]

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 … ...
user avatar
  • 315
4 votes
2 answers
2k 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', ...
user avatar
  • 315
8 votes
4 answers
15k 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', '...
user avatar
12 votes
2 answers
7k 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 ...
user avatar
  • 123
8 votes
3 answers
3k 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. :(
user avatar
  • 755
34 votes
13 answers
13k 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 ...
user avatar
  • 3,273
20 votes
4 answers
41k views

Processing bash variable with sed

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 LON which I'm trying to do via sed like ...
user avatar
  • 1,463
2 votes
4 answers
4k 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 ...
user avatar
0 votes
2 answers
783 views

Shell output question

What is the output of date -u +%W$(uname)|sha256sum|sed 's/\W//g' (on Arch Linux if it matters)? How do I find that out?
user avatar
1 vote
5 answers
4k 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; what'...
user avatar
  • 111
6 votes
4 answers
348 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
user avatar
5 votes
4 answers
1k 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 ...
user avatar
4 votes
3 answers
3k 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 ...
user avatar
110 votes
7 answers
69k 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, ...
user avatar
  • 3,640
2 votes
3 answers
407 views

Directory filenames with sed with whitespaces

I'm working on a script and I'm stuck, even with the help of the google. Here's my code: for FOLDER in `find . -type d | sed "s#^.#$(pwd)#" | sed 's/ /\ /g'` do echo "$FOLDER" done This will ...
user avatar
9 votes
6 answers
657 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 ...
user avatar
  • 1,255
9 votes
1 answer
12k 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.
user avatar
12 votes
4 answers
35k 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 gabe@...
user avatar
  • 11.1k
13 votes
4 answers
29k 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 ...
user avatar
  • 1,295
105 votes
8 answers
275k views

SED: insert text after the last line?

This sed 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?
user avatar
19 votes
7 answers
4k 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?
user avatar
11 votes
3 answers
336 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 --expression=...
user avatar

1
131 132 133 134
135