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

learn more… | top users | synonyms

4
votes
2answers
67 views

Concatenating fields from lines with different numbers of fields

Given input like this: x y a b c t p q w w t a b c d p q r I'd like to concatenate fields from field 3 up to but not including the last one. If field 3 is the last field, I'd like to insert a ...
4
votes
4answers
857 views

sed behaves different on FreeBSD and on Linux?

I use both Linux and FreeBSD (specifically, I use Debian Linux and PC-BSD), and I found something weird about sed. I frequently need to convert "tab separated values" files to "comma separated ...
4
votes
2answers
425 views

Use sed or awk to join lines in a certain pattern?

Not sure on how to turn this input: A B C apartner bpartner cpartner into this output: A apartner B bpartner c cpartner I have this pattern in a 80 line file that needs to be 40 line. Also, how ...
4
votes
7answers
7k views

How to print all lines after a match up to the end of the file?

Input file1 is: dog 123 4335 cat 13123 23424 deer 2131 213132 bear 2313 21313 I give the match the pattern from in other file ( like dog 123 4335 from file2). I match the pattern of the line ...
4
votes
1answer
679 views

How is this sed command working?

I want to understand, how following sed command is working: sed -nre "/jar$/ { x; d; }; /class$/ { x; p; } Actually it is part of another command to find a particular class file from the given list ...
4
votes
1answer
3k views

find & sed (search and replace)

I'm using the following command on my mac: $find . -name “*.java” -exec sed -i ’s/foo/bar/g’ {} \; and it seems to have no effect. I have two files in the directory that end in .java, which both ...
4
votes
4answers
932 views

Replace whole line in a file from command-line

I have a text file which has some contents similar to this: # General information about the project. project = u'Py6S' copyright = u'2012, Robin Wilson' # The version info for the project you're ...
4
votes
4answers
2k views

How to echo an escaped string

How can I echo an escaped string that contains $ in Bourne Shell? user@server:~$ cat test.sh #!/bin/sh echo $1 user@server:~$ ./test.sh \$sad\$test $sad$test I want it to return an escaped ...
4
votes
6answers
320 views

sed first n occurrences result set between 2 tags / patterns

I have a big XML file and I get all occurrences between 2 tags: Here is what I do: sed -n '/<tag>/,/<\/tag>/p' file.xml And I need to filter to get only first N occurrences. I've ...
4
votes
2answers
292 views

Merge fields in a file

I have a file with 7 columns, a GFF file having chromosomal regions.I want to collapse the rows where REGION ="exon" to only one row in the file.The row has to be collapsed on the basis of regions ...
4
votes
2answers
10k views

Can sed replace new line characters?

Is there an issue with sed and new line character? I have a file test.txt with the following contents aaaaa bbbbb ccccc ddddd The following does not work: sed -r -i 's/\n/,/g' test.txt ...
4
votes
3answers
131 views

Add leading zeros until all lines before the comma consist of nine characters and subsequently insert a character every three digits using sed

The aim is to add leading zeros until all lines before the comma consist of nine characters and subsequently insert a character every three digits using sed. Input 12345,1s4c3v6s3nh6 ...
4
votes
2answers
145 views

How can I delete all unordered lines from a text file?

Overview Consider an ordered list interspersed with unordered elements, e.g.: Alligator Ant Falcon <-- Baboon Badger Armadillo <-- Caiman Cat How can this list be processed so that all ...
4
votes
2answers
93 views

Why sed prints first string from file even if it's failed to find matching groups

I'm trying to write sed expression which will extract matching groups from file. E.g. I have .cue sheet and I want to extract it's genre tag. It has following format: $ cat file.cue | grep GENRE REM ...
4
votes
1answer
623 views

Unix - How to append text to every line of a file except for the header line/first line

I have a file say abc.txt with following data (real data has many records,say around 200) Sno | Name 1 | Jack 2 | Jill 3 | June Now how do I append text to make my file look like this, ...
4
votes
2answers
636 views

Using sed to delete everything between two words?

This doesn't seem to work The next example would delete everything between "ONE" and "TWO:" #!/bin/sh sed ' /ONE/ { # append a line N # search for TWO on the second line /\n.*TWO/ { # ...
4
votes
2answers
913 views

How to only substitute lines that match several patterns in sed?

$ echo -e 'CH12\nCH23au' | sed '/^CH/s=^=<b>=' <b>CH12 <b>CH23au I know I can match lines starting with CH by ^CH but how can I match multiple patterns? Example: Input: CH12 ...
4
votes
3answers
2k views

Remove string from a particular field using awk/sed

I have a file (>80,000 lines) that looks likes this: chr1 GTF2GFF chromosome 1 249213345 . . . ID=chr1;Name=chr1 chr1 GTF2GFF gene 11874 14408 . + . ...
4
votes
4answers
7k views

How to get only filename using sed

How can I get only the filename using sed? I've this out_file=$(echo $in_file|sed "s/\(.*\.\).*/\1mp4/g") But I get the path too /root/video.mp4, and I want only video.mp4.
4
votes
2answers
2k views

delete first line of file only if blank using sed

I have a solution for this in awk: awk '{if (NR==1 && NF==0) next};1' somefile but was unable to find one that worked in sed. E.g., sed -i.bak '/^$/{1,1d;}' somefile ended up deleting ...
4
votes
3answers
392 views

How to parse a file to extract 3 digits numbers kept in a “group number”

I am a beginner, and trying to write a shell script to parse a text file that is extracted from a standardization pdf file. I would like for each test group (identified by Group 0, Group 1... to get ...
4
votes
2answers
104 views

Cut a string- Catch: There is file with same name as string in the same path

Input /in/archive/ABC/20140111_ABC_018_PART_001.dat Desired Output: 20140111_ABC_018_PART_001.dat Command used: cut -d/ -f4 "/1a/ftproot/archive/ABC/20140111_PR1_018_SALESDOC_ITEM_001.dat" ...
4
votes
1answer
158 views

Reverse a line in sed?

echo 1234 | sed ' /\n/ !G s/\(.\)\(.*\n\)/&\2\1/ //D s/.// ' I'm unable to comprehend the above sed code. my understanding is: PATTERN SPACE=1234 first ...
4
votes
3answers
916 views

using sed to replace pattern with hash values

I want to search the file and replace specific pattern with its hash (SHA1) values. For example, let file.txt has the following content: one S56G one two three four five V67X six and I want to ...
4
votes
3answers
213 views

Check for, and add, missing timestamps to individual lines in a file

I have a file in the following format: ABCD 01206001022T01YA022T01YA022T07SO 09:20:38 ABCD 01206001022ACION 09:24:40 ABCD 04006001021S01UK 09:24:42 ABCD 7878696621321312 23213213213213 ABCD ...
4
votes
3answers
2k views

print lines between start and end using sed

I need to print the lines between two delimiters. For example, given the input fluff1 fluff2 BEGIN good3 good4 END fluff5 fluff6 BEGIN good7 good8 END more fluff I want the output to be good3 ...
4
votes
3answers
5k views

How can I use SED or AWK to replace placeholders in a template file with variable content that contains special characters?

CentOS 6.3 I'm trying to get a small script to send an email containing a copy of email headers in the body (for the purpose of internal reporting). The template file contains the following: ...
4
votes
2answers
2k views

sed: multi-line replace of config block

I have some configuration files that basically look like (...content...) # BEGIN DYNAMIC BLOCK - DO NOT EDIT MANUALLY (... more content ...) # END DYNAMIC BLOCK (... even more content ...) Now, in ...
4
votes
2answers
3k views

Removing Color Codes From Output [duplicate]

Possible Duplicate: Removing control chars (including console codes / colours) from script output I'm working on a script to work alongside a program that I'm writing. What i'm trying to do ...
4
votes
3answers
254 views

sed one-liner to replace word-medial capitals

I used OCR to turn some scans into plaintext, but unfortunately the letters 'fi' which are commonly joined in some fonts, got read in as capital W's. Now I need to replace all the W's with 'fi', and ...
4
votes
1answer
159 views

Using sed command convert only first character in specific line with uppercase

How can I convert the first character of each word in the line after gr-description: to uppercase? For example : $ cat /tmp/test1 groMail: test@xyz.com gr-description: abc-location-999 ...
4
votes
1answer
174 views

Matching Five Columns in two Files using Awk

I have two Input files. File1: s2/80 20 . A T 86 F=5;U=4 s2/20 10 . G T 90 F=5;U=4 s2/90 60 . C G 30 ...
4
votes
2answers
477 views

Passing parsed output of sed to find (in this direction)

Well, I think you can find dozens of questions on this platform how to pipe find output to sed, but I haven't found anything for the reverse direction so far. What I want to do is modify my input, and ...
4
votes
5answers
1k views

extract fields from “tail -f” of a syslog stream

Example line from syslog file: Aug 1 10:25:50 10.10.10.1 id=firewall sn=XXXX time="2012-08-01 14:35:18 UTC" fw=x.x.x.x pri=6 c=1024 m=537 msg="Connection Closed" f=11 n=195273698 ...
4
votes
1answer
294 views

substituting third occurrence of a pattern using sed

Here is a sed command. I want to substitute third occurrence of 00 to 11 echo "0000000000000000" | sed -e 's/00/11/3' The output is: 0000110000000000 But I thought output would be: ...
4
votes
1answer
4k views

Substituting the first occurrence of a pattern in a line, for all the lines in a file with sed

Is it possible to do it in one liner? I have an output like this: "First line" - Description " Second line" - Description "Third line" - Description " Fourth line" - Description This input ...
4
votes
2answers
593 views

Replace one line with STDIN in a Makefile

One Makefile target is supposed to be in charge of: grabbing the output of $ perl Markdown.pl src/index.md (markdown to html) using that string to replace one line, something like CONTENT ...
4
votes
2answers
800 views

Moving average on a log file with awk or other unix utilities?

Scenario: I have a log file that has a few number of entry "classes", like this: R0 dx=0.00500 rb=0.00000 sn=1 3145.88 2.59 0.08 se=21315 id=16190 R0 dx=0.00300 rb=-1.00000 sn=1 3150.40 2.38 0.05 ...
4
votes
4answers
634 views

Bash Scripting : Printing column data in the same row

I have written a bash script to calculate the size of a PostgreSQL database and print the output along with the date when the script was executed in a text file. The script code is as follows: ...
4
votes
3answers
441 views

How do I stop sed from adding extra newline characters

I am running the following 2 sed commands. The first one adds newline characters where I want them, the second also adds newline characters where I want them, BUT also adds an extra one at the end of ...
4
votes
1answer
82 views

Is strace a suitable tool for finding out where a program wrote to the filesystem?

I'd like to determine where a certain app (as an example, surf) writes to the filesystem in order to make sure it leaves no traces. Without a dedicated sandboxing system this probably isn't easy, but ...
4
votes
2answers
470 views

Out of memory while using sed with multiline expressions on giant file

I am currently trying to remove all newlines that are not preceded by a closing parenthesis, so I came up with this expression: sed -r -i -e ":a;N;$!ba;s/([^\)])\n/\1/g;d" reallyBigFile.log It does ...
4
votes
3answers
433 views

Want to search for two consecutive lines with at least n characters

I would like to use sed to search for the first part of a text file with consecutive lines with at least n non-space characters. I would like to print from the first of those lines to the end of the ...
4
votes
1answer
4k views

Delete last character in a word but only if the character is there - in bash

How to remove last character only if it's there? input: OpenOffice.org/m openOffice.org/ozm Pers. Pfg. phil. Prof. resp. Roonstr./m roonstr./ozm desired output: OpenOffice.org openOffice.org ...
4
votes
5answers
485 views

Multi-line grep beween two tags, only show the last match

I'm having a program that intermingles data segments into log output: log message log message ----BEGIN INLINE DATA---- data data data -----END INLINE DATA----- log messge I'm looking for something ...
4
votes
3answers
794 views

Delete the matching line and several more from a file

I have a text file called file_a.txt. My first command is grep -A 12 ".production =" file_a.txt The output is a few block. Each block of string contains 13 rows I specifically want to delete all ...
4
votes
3answers
546 views

How to skip file in sed if it contains regex?

I currently use the following simplified command to remove trailing whitespace and add a newline at end of file where needed: find . -type f -exec sed -i -e 's/[ \t]\+\(\r\?\)$/\1/;$a\' {} \+ As ...
4
votes
1answer
749 views

Use sed to fix broken newlines

I have a folder full of files, each with many hundreds of lines. Many of these files have occasional broken newlines: when I open them in vim, many have ^M^L interspersed where newlines should be. ...
4
votes
1answer
483 views

Are there zero-width assertions in sed?

Is it possible to make lookahead or lookbehind zero-width assertions in sed? I want to emulate Perl's (?=) and family. My sed is not GNU sed version 4.0.
4
votes
2answers
70 views

sed regex issue

I'm trying to replace a string in a file: $RELEASE = '1234'; The whitespace between RELEASE and the = symbol could be either spaces OR tabs. The quotes around the version number could be either ...