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

learn more… | top users | synonyms

4
votes
4answers
737 views

How to remove character and space from a string

Hello I have one file with output Name : ABC Name : CDE Name : ZYS I want to get the result ABC CDE ZYS How can i get it through SED command. I have tried. sed s/'Name' but ...
4
votes
3answers
1k views

Show lines matching a pattern and the 4 lines before each

For example, from this file: CREATE SYNONYM I801XS07 FOR I8010.I801XT07 * ERROR at line 1: ORA-00955: name is already used by an existing object CREATE SYNONYM I801XS07 FOR ...
4
votes
3answers
2k views

Inserting text at the beginning of a file with sed via the terminal in Linux [duplicate]

Possible Duplicate: How can I prepend a tag to the beginning of several files? How do I insert text at the beginning of a file via terminal?
4
votes
4answers
204 views

How to get lines 10 to 100 from a 200 line file into a new file [duplicate]

I have file with 200 lines. I need to extract lines from 10 to 100 and put them into a new file. How do you do this in unix/Linux? What are the possible commands you could use?
4
votes
4answers
771 views

Print line after nth occurrence of a match

I am looking to display the line 4598 in the following file. Effectively I want to display the line AFTER the nth occurrence of a match. In this case, the line after the 3rd occurrence of <Car>. ...
4
votes
2answers
877 views

sed pattern space and hold space

i was just going thorugh this and i was unable to understand. sed -n '1h; 1!H; ${ g; s/foo\nbar/bla\nblub/ p }' file. where cat file is : foo foo bar why there is 1!H because to me 1h ...
4
votes
2answers
549 views

Append line to many files

I want to add some text to over 200,000 files I am trying this find . -name *.txt -print | xargs -I % echo "hello world" >> % But nothing is happening. When i run find . -name *.txt it work ...
4
votes
3answers
162 views

Regex alternation/or operator (foo|bar) in GNU or BSD Sed

I cannot seem to make it work. GNU sed documentation says to escape the pipe, but that doesn't work, nor does using a straight pipe without the escape. Adding parens makes no difference. $ echo ...
4
votes
2answers
6k views

Compare two file columns

I have long text files with space-delimited fields: cat file1.txt Id leng sal mon 25671 34343 56565 5565 44888 56565 45554 6868 23343 23423 26226 6224 77765 88688 87464 6848 66776 23343 63463 ...
4
votes
2answers
2k views

How to search and replace multiple needles by one word via one expression?

Assume you have a text file: foo fnord bar bizz foo poit And now I would want to replace both "foo" and "bar" into "narf". I know I could use: sed -e 's/foo/narf/g' -e 's/bar/narf/g' fileName ...
4
votes
4answers
4k 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 ...
4
votes
3answers
188 views

How to only output lines that are younger than 3 months?

cat a.txt 1387111124 ./asfjlasdf.txt 1348681215 ./akdfyxcv.txt So I need this: cat a.txt | SOMEMAGIC 1387111124 ./asfjlasdf.txt The output should only have the lines that are younger than 3 ...
4
votes
2answers
279 views

Why does this add spaces? echo “x ax” | sed 's/x\s*/x /'

I want to find a x, and replace the 0 or more following spaces (\s*) with just a single space. echo "x ax" | sed 's/x\s*/x /' For some reason, instead of replacing the spaces with the single space, ...
4
votes
2answers
3k views

Working with columns - awk and sed

I am trying to parse a text file that is generated by an expec script that pulls down some information from a switch. Here is a sample output: 192 0000.0000.0000 1/g23 Dynamic ...
4
votes
2answers
157 views

sed to search for multiple words and remove those lines

I have a master file with more than 50000 lines and my requirement is to remove around 20000 lines from actual file, for this I used grep -vE command which is showing an error as too many arguments. ...
4
votes
3answers
120 views

Frequency of words in non-English language text: how can I merge singular and plural forms etc.?

I'm sorting French language words in some text files according to frequency with a focus on insight rather than statistical significance. The challenge is about preserving accented characters and ...
4
votes
3answers
109 views

Search and replace strings that are not substrings of other strings

I have a list of replacements like so: search_and -> replace big_boy -> bb little_boy -> lb good_dog -> gd ... I need to make replacements for the above, but at the same time avoid ...
4
votes
3answers
164 views

Extracting text using sed does not work as expected [duplicate]

I am just starting out using sed and I intend to use it to extract IP addresses from ping output. Here's what I am trying to achieve exactly: input text: ytmti (192.188.2.3) jjggy desired ...
4
votes
4answers
113 views

How to switch the first letters of two words?

$ echo 'foo bar' | sed SOMEMAGIC boo far $ echo 'one two' | sed SOMEMAGIC tne owo Q: in general, how can I replace the "b" from the "bar" to the "f" of the "foo"? And in reverse too, replace the "f" ...
4
votes
2answers
466 views

Replace string with sequential index

Can someone suggest an elegant way to accomplish this? Input: test instant () test instant () ... test instant () //total 1000 lines output should be: test instant1 () test ...
4
votes
2answers
242 views

Why do I need to escape regex characters in sed to be interpreted as regex characters?

It seems e.g. cat sed_data.txt | sed 's/\b[0-9]\{3\}\b/NUMBER/g' that I must escape characters to form a regular expression. In this case I had to escape braces in order to be interpreted as a number ...
4
votes
3answers
651 views

sed convert 4 spaces to 2

How does one convert 4 spaces into 2 spaces with sed? Is it possible? I found this but it converts tab to spaces: sed -r ':f; s|^(\t*)\s{4}|\1\t|g; t f' file
4
votes
2answers
136 views

Column mismatch and substituting

input.txt (tab-delimted) TTTTOTTT00000000008 RTTTT899 5.00E-28 TTTTOTTT00000000046 RTTTWRR 3.00E-31 TTTTOTTT00000000051 2.00E-11 TTTTOTTT00000000051 7.00E-12 TTTTOTTT00000000054 ...
4
votes
2answers
588 views

Why does this awk command not play as well with find as sed does?

I am very much a newbie at Unix/Linux command line stuff, and mostly get to where I'm going by copying, pasting, and modifying code I find on the internet. Just mentioning that in hopes people will ...
4
votes
5answers
4k views

How can I convert tab delimited data to comma delimited data?

I'm requesting a list of ec2 snapshots via amazon's ec2 command line tool: ec2-describe-snapshots -H --hide-tags > snapshots.csv The data looks something like this: SnapshotId VolumeId ...
4
votes
2answers
165 views

\n prints an n instead of a newline when using sed in MaxOSX

It's a general problem when I use sed but \n works when I use it outside of sed. This is one example of what I get with sed. I have the file myfile with the following content: 1 2 3 4 5 When I use ...
4
votes
2answers
65 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
691 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
353 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
1answer
667 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
882 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
2answers
278 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
6answers
6k 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
2answers
6k views

Sed: Replace N first occurrences of a character

I am looking to replace the 5 first occurences of the whitespace character per line inside a sed script. Here's what I have so far sed -e "s/ /;/" -e "s/ /;/" -e "s/ /;/" -e "s/ /;/" -e "s/ /;/" ...
4
votes
3answers
169 views

Replace multiple strings in a single pass

I'm looking for a way to replace placeholder strings in a template file with concrete values, with common Unix tools (bash, sed, awk, maybe perl). It is important that the replacement is done in a ...
4
votes
2answers
141 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
86 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
3answers
3k views

Filter a .CSV file based on the 5th column values of a file and print those records into a new file

I have a .CSV file with the below format: "column 1","column 2","column 3","column 4","column 5","column 6","column 7","column 8","column 9","column 10 "12310","42324564756","a simple string with a , ...
4
votes
2answers
516 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
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
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
375 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
2k views

Make sed ask for confirmation before each replacement?

Is there a way to make sed ask me for confirmation before each replace? Something similar to 'c' when using replace inside vim. Does sed do this at all?
4
votes
6answers
170 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
91 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
135 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
744 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
1answer
535 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, ...