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

learn more… | top users | synonyms

4
votes
3answers
759 views

How to remove all white spaces just between brackets [] using bash? [duplicate]

Replace text between brackets Input testing on Linux [Remove white space] testing on Linux Output testing on Linux [Removewhitespace] testing on Linux So, how can we just remove all the ...
4
votes
3answers
379 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
3k 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
1answer
3k 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
5answers
307 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
671 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
491 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
434 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
49 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 ...
4
votes
1answer
256 views

sed : insert sth before subsequent lines that begin the same, but are not the same

I have a LaTeX file with one glossary entry per line: ... \newglossaryentry{ajahn}{name=Ajahn,description={\textit{(Thai)} From the Pali \textit{achariya}, a Buddhist monk's preceptor: `teacher'; ...
3
votes
4answers
6k views

removing the first and the last character of every line from command line

I am trying to remove the first and the last characters of everyline in a text file and save the resulting truncated version in a new file. Does anyone have an idea about how to do that efficiently ...
3
votes
4answers
295 views

How to use Sed to replace all characters before colon?

How do I replace the following string hd_ma_prod_customer_ro:*:123456789:john.doe with john.doe Basically I need to look for the last colon (:) and delete everything before and including it.
3
votes
4answers
313 views

How to Prefix a column values with an apostrophe ( ' )?

I have a CSV file with multiple columns and 1000's of records, I need to prefix all the values of one of the columns (lets say 2nd column) with an apostrophe ' except in the first line or header line, ...
3
votes
3answers
4k views

extract part of string using sed

ls lib/oracle-11.2.0.3.0.txt | sed 's/lib.\([oracle.*]\)\.txt/\1/' It is giving the whole string instead of just oracle part until .txt What am I doing wrong? I can do it using awk as follows, but, ...
3
votes
3answers
201 views

How to perform an action only on the first line?

sed 's/.\(.*\)/\1/' myfile Say myfile contains: abc def ghi I want to remove the first character only from the first line but the above removes it from all the line.
3
votes
2answers
216 views

What is this sed command doing?

Can someone explain me what this sed command is trying to achieve? sed 's/^[[:space:]]*//;s/[[:space:]]*$// I understand that it is searching for whitespace characters from the beginning of the ...
3
votes
3answers
232 views

Regex not matching

why this does not match..? sed -e '/--Updated?[[:space:]]+Date/d' inputfile this..: --Updated Date: 2013-11-06 15:32:13 d? is because sometimes I have Update Date, and sometimes Updated Date. ...
3
votes
4answers
200 views

Split the input file with line breaks

I am trying to insert line breaks on a file based on the same names which happens to be the second field in my file. So, my input file is like below. 17412193|name1|organization ...
3
votes
3answers
1k views

How to sed a range of lines?

I have this: sed -i '/^$/d' temp_spec.rb which is stripping blank lines and works well. How can I make it only do this for lines 5-999 (or ideally 5 to end-of-file). I tried: sed -n5,999 -i ...
3
votes
4answers
145 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 ...
3
votes
3answers
147 views

sed syntax to remove 2 characters only when it is box and space

I have a sed expression to remove the first 2 characters from all the lines in a file. sed 's/^..//' raw.txt > converted.txt However, the problem is some lines are large and get appended to the ...
3
votes
2answers
279 views

sed: match two lines

I have a text file, where records are split on two lines. I would like to put them on one line. The records look something like this: 2013-07-29 apt-get install git 2013-07-29 apt-get install rsync ...
3
votes
4answers
803 views

Replacing pattern after nth match is found on each line?

I have a file containing lines: india;austria;japan;chile china;US;nigeria;mexico;russia I want to replace all the occurences of semicolon on each line with e.g. ;NEW;, but starting from the 2nd ...
3
votes
5answers
915 views

Remove lines based on pattern by keeping first n lines

I need to remove lines from a text file based on pattern but I need to keep the first n lines of that pattern. Input % 1 % 2 % 3 % 4 % 5 text1 text2 text3 output %1 %2 text1 text2 text3 I used ...
3
votes
6answers
463 views

Remove last comma from a bash for loop generated string

I am dealing with a situation where I need to create a comma separated list from an array into a heredoc and remove the last comma. I am using bash for piped into sed which is erasing all commas ...
3
votes
3answers
143 views

Easily get a particular column from output without sed or awk

Is there a quicker way of getting a couple of column of values than futzing with sed and awk? For instance, if I have the output of ls -hal / and I want to just get the file and directory names and ...
3
votes
2answers
228 views

Extract lines containing PAT1 but not PAT2 with sed

I want to extract the lines containing PAT1 but not PAT2 from a file. For example, with PAT1='dog', PAT2='cat', and the following input: 1 cat chicken 2 bird dog apple 3 dog orange cat 4 cat juice ...
3
votes
1answer
189 views

Fastest `uniq` tool in linux

I have large text file (1.5 G), I want to know what the fastest and more reliable tool in linux is. I usualy use: awk '!x[$0]++' file.txt But when I use htop command I see my memory usage is ...
3
votes
3answers
3k views

How to parse JSON with shell scripting in Linux?

I have a JSON output from which I need to extract a few parameters in Linux. This is the JSON output: { "OwnerId": "121456789127", "ReservationId": "r-48465168", "Groups": ...
3
votes
2answers
170 views

Join: Two files - but only append last two columns

Given the files: 1.txt 1, abc, 123, 456, 789 2, lmn, 123, 456, 789 3, pqr, 123, 456, 789 2.txt 1, abc, 123, 000, 000 3, lmn, 123, 000, 000 9, opq, 123, 000, 000 OUTPUT.txt ID, NAME, X, ...
3
votes
3answers
1k views

Delete XML node containing certain element

I want to remove all Placemarks from a KML file that contain the element <tessellate>. The following block should be wholly removed: <Placemark> ...
3
votes
4answers
4k views

How to get all lines between first and last occurrences of patterns?

How can I trim a file (well input stream) so that I only get the lines ranging from the first occurrence of pattern foo to the last occurrence of pattern bar? For instance consider the following ...
3
votes
4answers
356 views

Piping paths with different types of quotes for slash substitution

I would like to use sed to convert a path with backslashes to the same path with forward slashes: E.g. I would like to pipe \\path\to\file\ and obtain /path/to/file None of the following commands ...
3
votes
4answers
759 views

Strip spaces after single capital letters with sed

I am writing a bash script to automatically generate some other files, and I have to format some strings a certain way. Specifically, the last problem I'm having is formatting a string that has ...
3
votes
2answers
4k views

Strip trailing whitespace from files

The answer to removing trailing whitespace with sed has most of the answer, but I want sed -i 's/[ \t]*$//' $1 to be able to take arbitrary number of file arguments as a shell script on the command ...
3
votes
2answers
4k views

Show only text between 2 matching pattern

I'm going to submit form using cURL, where some of the contents is come from other file, selected using sed If param1 is line matching pattern from other file using sed, below command will works ...
3
votes
2answers
625 views

How do remove identical lines in one file from another, using sed?

I have two files, one being a superset of the other. I want to remove the identical lines in the smaller files from the larger file. One possible complication is that the lines contain backslashes. ...
3
votes
2answers
128 views

Search a string with last two characters as numeric

Suppose I have a file like this: adasfddfd09 dsassd90897 323sdsdsdsd sdddsdf56 dfdf45fdfdf I want to search only those strings which have last two characters numeric with sed, grep and awk.
3
votes
3answers
175 views

Rearrange text file by prepending data rows with header row

I have a file whose content looks like this: 2009 150 0 0 0 0000 75.316 0.0400390625 0.00007 0.00000 0.8980 76.216 0.0400390625 0.00007 1.00000 0.9046 77.217 0.0400390625 ...
3
votes
3answers
95 views

How to select 30 files and do a bunch of sed commands on them

I want new copies of the files, so I want to do something like: dir *.rb foreach file make a copy of the file, e.g. 'blob.rb' to 'blob_processed.rb' do the 50 sed commands to process the ...
3
votes
3answers
162 views

checking data in columns when a data or some may be missing or present?

I am not sure if this possible. say i have columns like : Team Colour Game Rainfall PlayerName XYZ Blue Cricket Yes Kapil suppose i need to search ...
3
votes
5answers
576 views

changing pattern of a text file

A text file has contents something like chair table pen desk Now i want it to be changed and stored in a variable say var as below ('chair','chair'),('table','table'),('pen','pen'),('desk','desk') ...
3
votes
3answers
180 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" ...
3
votes
6answers
133 views

sed remove characters from new line

After some processing, I have my input file like below. file1.txt 12345|john|student 43321|jack|professor 78965|alex|lecturer I need to process the above file further and so I need a line ...
3
votes
4answers
253 views

extract the characters before () using grep

I need to extract the characters before the character '('. My file structure is: rose(good)ook daisy()no lilly(bad)fine sunflower()nice I need output file as in the form of: rose ...
3
votes
3answers
698 views

awk remove the rows based on another column value

I have a file like below which I have sorted based on the username field. UserID score UserName 1234 200 Jack 5678 150 Jill 8543 200 Jill 5678 100 John I am trying to remove ...
3
votes
2answers
547 views

Basic sed command on large one-line file: couldn't re-allocate memory

I have a 250 MB text file, all in one line. In this file I want to replace a characters with b characters: sed -e "s/a/b/g" < one-line-250-mb.txt It fails with: sed: couldn't re-allocate ...
3
votes
1answer
400 views

grep on line before the match plus the match

zzzzzzzzz aaaaaaaaa bbbbbbbbb & ccccccccc & ddddddddd hhhhhhhhh eeeeeeeee fffffffff & ggggggggg & in the above line, what i want is to grep/sed/awk (any method is fine) line that ...
3
votes
5answers
5k views

Using sed to find and replace complex string (preferrably with regex)

I have a file with the following contents: <username><![CDATA[name]]></username> <password><![CDATA[password]]></password> ...
3
votes
5answers
180 views

grep device name and look for next value :

I have this output from a find command: abc,10.11.13.14,def,1.2.3.4,geh,6.7.54.23 where abc,def and geh are device names and could be of any length and others are IP address belong to devices. ...