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

learn more… | top users | synonyms

7
votes
4answers
437 views

How to split long sed expression into multiple lines?

sed 's/[long1][long2]/[long3][long4]/' file.txt I would like to split this command onto multiple lines - f.x. something like this: sed 's/ [long1] [long2] / [long3] [long4] /' ...
6
votes
5answers
513 views

What's wrong with this sed expression?

$ echo "104_Fri" | sed 's/^\([0-9]+\)_\([A-Za-z]+\)$/\1;\2/' 104_Fri I would like to match the digits at the beginning and the letters at the end - each as a group. Afterwards I want to output the ...
2
votes
2answers
78 views

Replace column entry with sum of other column entries

Let's say I have a file that contains these lines: 02.03.14 14.50 14.50 0.00 Desc 02.03.14 17.00 0.00 17.00 Desc 01.03.14 2.82 1.68 calc Desc 02.03.14 1.04 0.00 ...
6
votes
5answers
223 views

Remove backslash + newline sequences

We are having a file in Linux which contains one line per record, but problem comes when the line contains some new line characters. In this case, the backslash is appended at the end of line and the ...
1
vote
2answers
89 views

Search for a string in a line and append related lines

I have a file containing some ID (identifier) and DS (description) lines. I'd like to keep the rows containing the string value in their ID line, as well as all the following (related) DS lines before ...
4
votes
2answers
253 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. ...
13
votes
9answers
481 views

Find all occurances in a file with sed

Using OPEN STEP 4.2 OS... I am currently using the following sed Command: sed -n '1,/141.299.99.1/p' TESTFILE | tail -3 This command will find one instance in a file with the ip of 141.299.99.1 and ...
1
vote
2answers
115 views

List all the html tags in a file

I want to know if there is a way to list all the html tags in a file. Lets say I have a file file.html <html> <head> <title>Test</title> </head> <body> This is a ...
32
votes
4answers
2k views

Reverse grepping

Let's say, I have a really big text file (about 10.000.000 lines). I need to grep it from the end and save result to a file. What's the most efficient way to accomplish task?
2
votes
2answers
69 views

Format output to a specific line length

So I have some output that has very long lines, and I want to have it formatted to lines no more than 80 columns wide, but I don't want to split words because they are in the column limit. I tried ...
5
votes
2answers
292 views

How is the inplace modification of a file being done?

What does the "inplace" modification of a file e.g. via sed -i or perl -i mean? My question is about how this inplace modification is done. Is the file copied the modification is done in the copy and ...
2
votes
1answer
112 views

What package has GNU sed for Solaris?

I need to use the features of GNU sed, instead of the built-in version under Solaris 10. Which GNU package contains it? I don't see it listed in the coreutils, and my Google searches haven't helped ...
3
votes
1answer
112 views

Is there a way to pass a variable from an awk statement to a bash function as an argument? [closed]

I am trying to look a for some specific values from (such as 1 or 4 and 2 or 3) a text file using an awk statement in a bash script. If this value is found in the file (within the awk statement), then ...
2
votes
3answers
364 views

Reverse match in sed, replace opposite of what was found

Say I have this output from my syslog, precalculated src_hash => 20 bytes @ 0x7f811c001e20 A6 2D E5 CD 2A BA F0 42 56 66 19 D4 61 1A E3 A0 .-..*..BVf..a... C7 5E 5F 77 ...
4
votes
2answers
78 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 ...
5
votes
2answers
193 views

Comment all lines from last commented line to line with 'foo'

Consider a text file users.txt: #alice #bob charlie dotan eric I need to comment everything from (exclusive) the last commented line until (inclusive) dotan. This is the result: #alice #bob ...
4
votes
3answers
164 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
361 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 ...
1
vote
4answers
512 views

Simple sed replacement of tabs mysteriously failing

This ought to be really simple, but for some reason it is not working: sed -i.bak -E 's/\t/ /' file.txt Instead of replacing tab characters, it's replacing t characters. I've tried every ...
5
votes
2answers
505 views

Does `sed` exist in all *nixes?

My application will be running on all versions of Unix-based OS, say, Linux, MacOS, HP-UX, Sun Solaris , OSX, etc. I'm using the following sed command in a common place to delete the string matched ...
3
votes
3answers
132 views

“Tuning” tr and sed expression

I have MD5 file which lists filename and MD5 sum in the following way: MD5 (file14n0001.xml.gz) = 04eb2524d271077ec8f1a0021779b171 MD5 (file14n0002.xml.gz) = 07e2b29645e9211cce9daf1ae27e7b99 MD5 ...
2
votes
3answers
323 views

sed + remove the first directory from path

Please advise on how to remove the first directory from a path by sed, (the inverse pf what basename does). For example echo "/mnt/VPfig/Amer/AR4/Celtel/files/COM.txt" | sed ... I should get: ...
2
votes
1answer
335 views

Removing non-printable characters using POSIX sed

Files created with roff and other "old-school" tools (for example man pages on many Unix systems) generate bold and underlined text in minimalistic terminals using tricks involving non-printable ASCII ...
1
vote
3answers
43 views

Delete whole line if after “:” less than 4 chars

I have a file with many lines, such as : blue:fish red:tomato black:moon I would like the whole line to be deleted if after ":", there is less than 4 chars. Thanks for your help!
3
votes
2answers
260 views

Counting comma separated characters in a row

How can I count how many elements are separated with comma in a given column of a file (tab_delimited), using awk or sed? Exemple: AC 1,23,5 DC 2,8,89 Would return: AC 3 DC 3
4
votes
3answers
169 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 ...
0
votes
2answers
28 views

Replace selected caracters

How can one replace only the first [:space:] character in each row of a file with a \t using sed ? Input: AC 12 45 DC 56 89 Expected output: AC 12 45 DC 56 89 (Something like sed ...
3
votes
1answer
64 views

How do back-references match in sed?

I found a one-line sed command on this site which removes duplicate entries in $PATH. There is one concept that I can't get my mind around. It uses saved matches in the match section to detect the ...
2
votes
3answers
65 views

Merging multiple files by row

Is it possible to copy the rows of File1 and File2 in a new File4 following the instruction given by File3 by using a simple bash script (sed? awk?). File1: /*two or more columns, tab delimited*/ ...
1
vote
2answers
322 views

Replace columns using a shell

How could I replace column 1 in File1 with column 2 in File2, when a match is found? File1 id1 36987 id2 75685 id3 12345 id4 12896 File2 id1 ID1 id2 ID2 id3 ID3 id5 ID5 I'd like to write this on ...
2
votes
1answer
131 views

Compare the values of a column with all the values in another column

I have 2 input files. Each line of File1 should be compared to each and every line of File2. Logic is: If Column1 of File1 does not match Column1 (all the values under it) of File2, then print the ...
2
votes
1answer
60 views

SED and PDSH: adding a line to a file on a slave node in a cluster

I am trying to add a new line containing GATEWAY=10.0.10.1 to the /etc/sysconfig/network file on each of 32 slave nodes in a cluster. I read this post on how to insert a line with sed. This works for ...
0
votes
2answers
135 views

Using sed to remove string from list of files fails

I'm on bash (Mac OS X). I have a list of files and there's a string I want to remove: $ grep -l \</html\> *.html 21888601.html 21906283.html 21977081.html ... All matching files are ...
2
votes
3answers
2k views

sed - replace string with file contents

I have two files: file1 and file2. file1 has the following contents: --- host: "localhost" port: 3000 reporter_type: "zookeeper" zk_hosts: - "localhost:2181" file2 contains an IP ...
6
votes
4answers
207 views

How to insert lines with numbers from X to Y into another file after line Z?

I created two files: echo -e "1\n2\n3\n4\n5" > 123.txt echo -e "a\nb\nc\nd\ne" > abc.txt I want to get the file 123.txt with the following contents: 1 2 3 b c d 4 5 So in other words ...
3
votes
1answer
44 views

Matching several patterns on one line

I often use a program called tty0tty, which creates a null modem using PTYs. Now I want to make it part of an automated process. When I start tty0tty it prints (/dev/pts/1) <=> (/dev/pts/2) ...
1
vote
3answers
106 views

Sed + how to set parameters in sed command in order to display lines from text file

I find the following way to print line from file In this example we want to print the first line from the hosts file sed -n '1,1p' /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 ...
6
votes
5answers
243 views

Strategy to extract movies's name from this ununiform dataset?

I am working on a movie database problem to improve regular expressions, this is the problem I'm running into. My dataset looks like this: Movie Name (variable space and tabs) year Movie1(can ...
6
votes
4answers
203 views

Replace \n by a newline in sed portably

I'm so confused with GNU sed, POSIX sed, and BSD sed's myriad of options. How do I replace literal \n with the newline character using these three sed types?
4
votes
4answers
235 views

Match two words that is on the same line

The command that I have prints out two of the lines whenever FW_6.0.0 is found, below is the code: grep -oP 'FW_6.0.0, (.*)$' file Below is the output, both has the same value of FW_6.0.0 ...
1
vote
1answer
55 views

Retrieving fasta sequences using bed file information from locally installed file

I have a .bed file containing around 30000 rows for which I have the sequences retrieved using fetch-sequences module of the rsat tool (http://rsat.ulb.ac.be/rsat/help.fetch-sequences.html#usage). ...
2
votes
1answer
63 views

Strip color on OS X with BSD sed (or any other tool)

I'm on OS X, which has a BSD version of sed (which I'm guessing is inferior to a GNU version on Linux), and all the techniques for removing color on commandlinefu.com did not work. I tried replacing ...
1
vote
3answers
71 views

How to truncate only given column length?

INPUT: a@notebook:~$ cat in.csv 'XYZ843141'^'ASDFSAFXYVFSHGDSDg sdGDS dsGDSgfa assfd faSDFAS saDFSAFD adFSA343fa sdfSADF'^'BAAAR'^'YYY'^'..... and so on, further columns ...
3
votes
3answers
256 views

String replacement in file

I have the following file: <?xml version="1.0" encoding="utf-8"?> <!--Generated by crowdin.net--> <string name="test" >- test</string> <string name="test" ...
2
votes
1answer
87 views

Change a specific line using sed

I need to change a line in a Java file called DbConnection.java using the sed command. The line is: connection = DriverManager.getConnection("jdbc:mysql://**localhost**/**bajaj**","**root**", ...
2
votes
1answer
229 views

Extract download progress from curl output

I'm trying to extract the download progress of a file that's being retrieved by curl. I tried this, but this doesn't work: curl --progress-bar http://127.0.0.1/test.tar.bz2 -o test.tar.bz2 ...
6
votes
4answers
484 views

how to remove ctrl F in unix file

While editing a unix file I'm getting data as below in vi editor. MGW:^FVMG107 MGW:^FVMG113 MGW:^FVMG108 MGW:^FVMG103 where in above data ^F is not viewable in cat command. I have tried dos2unix ...
2
votes
2answers
66 views

new pattern required from an input file using sed or awk

I have data as below: MGW: VMG110 836-16 836-18 836-19 336-20 836-23 MGW: VMG120 3802-1 3802-2 3802-3 3456-1 3456-4 Required output is: VMG110:836-16&-18&&-20&-23 ...
1
vote
1answer
49 views

How to echo from specific line of a file to another specific line [duplicate]

Assume I have a file with about 10000 lines. How can I print 100 lines, starting from line 1200 to line 1300?
2
votes
3answers
63 views

A challenge for sed convert code from Mathematica to Matlab

I have the following output from Mathematica to C code: $ cat file.txt Complex(1.3, 2.7)*Power(kappa, 2.0) + Complex(0.3, 2.0)*Power(kappa,5.0)*log(alpha) The file is extremely long (many tens of ...