Sed is a command-line stream editor for filtering and transforming text.
4
votes
2answers
329 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
450 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
3answers
152 views
Multi-line replace
I spent ages trying to figure this out. As it happens, I don't need to do it any more; I found another way. But for the sake of my sanity, I'd like to know the answer anyway.
It's quite easy to use ...
4
votes
2answers
81 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
291 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
225 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
3answers
249 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
1answer
250 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
1answer
211 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
958 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
2answers
194 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
802 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 ...
3
votes
3answers
177 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
77 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 ...
3
votes
4answers
103 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
3answers
620 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?
3
votes
5answers
255 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
2answers
142 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
3answers
2k views
How to insert a line into text document right before line containing some text in bash?
I have a variable say $strToInsert I have a file file.html I wonder how to find last appearence of </head> and insert a new line before line with it and fill it with $strToInsert contents?
...
3
votes
2answers
100 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 ...
3
votes
4answers
468 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
475 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
3answers
67 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
4answers
2k 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
2answers
1k 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 ...
3
votes
5answers
308 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
60 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
5answers
125 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. ...
3
votes
6answers
173 views
Extracting help message from script itself
I want that my help message be extracted from the script itself.
#!/bin/bash
#
# foo - do things
# Author: John Doe <jhon@doe>
# ----------------------------------------------
# SYNOPSIS
# ...
3
votes
4answers
286 views
Align a hex text file at 9 bytes each line
I want to align the selected portion of a text file at 9 bytes each column.
For example suppose my text file looks like below.
00 2f c6 b8 29 fd 02 37 11 00 9f 74 34 0b 60 72 38 20 00 9e 61 33 8e ...
3
votes
2answers
46 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
...
3
votes
2answers
256 views
Parsing a text-file table and aggregating information
I have long text file with the following columns, space-delimited:
Id Pos Ref Var Cn SF:R1 SR He Ho NC
cm|371443199 22 G A R Pass:8 0 1 0 0
...
3
votes
2answers
83 views
What is the usage of pattern before substitute command in sed
There is an example in this link about sed:
To delete the first number on all lines that start with a "#" use:
sed '/^#/ s/[0-9][0-9]*//'
What is the benefit of first pattern(/^#/)? It could be ...
3
votes
2answers
5k views
Removing all spaces, tabs, newlines, etc from a variable?
This is the error I am getting and it's failing because of a variable whose value is supposed to be 2 (I am getting this using a select * from tabel).
I am getting spaces in that variable.
+ 0 !=
...
3
votes
1answer
353 views
Why can't I sed two [or more..] empty lines to one empty line?
Why can't I sed two [or more..] empty lines to one empty line? What is the trick?
echo -e "hello\n\n\nhello2" | sed 's/^$\n^$/\n/g'
hello
hello2
3
votes
3answers
42 views
how to form a sed expression containing escaped characters
Given a sed expression (and GNU sed 4.2.2 on ArchLinux)
/match/i\tline1\n\tline2
which should insert two tab-indented lines above the match, I find that the escaping of the first character (in the ...
3
votes
2answers
161 views
Colorizing tail output with sed
How would I go about colorizing the output of tail with sed?
echo "`tput setaf 1`foo`tput op`" works as expected
echo "foo" | sed -e 's/(foo)/`tput setaf 1`\0`tput op`/g' however, does not.
What am ...
3
votes
3answers
187 views
Delete a block of lines with a condition on the last line
I have a log-file where at the end of a series of lines you can see if this block is relevant.
Now I'm looking for a command like sed to delete de blocks ending with "Content-Length: 0" and beginning ...
3
votes
2answers
345 views
What is the best way to find a list of several strings within a large text file
The short, general question is: In Unix/Linux, what is the best way to find a list of several (about 150) strings within a large text file?
I am asking this to all Unix/Linux experts as a general ...
3
votes
2answers
171 views
Print row contain maximum and minimum value
I need to read the file (contains 16K rows)and print the entire row if any of columns and all columns contains max value (100) and all columns contain min value (0).The ouput example is given
...
3
votes
1answer
271 views
What's wrong with this sed command?
This command runs fine:
$ sed -e '/foo/{g; d}' myfile
But this one has an error:
$ sed -e '/foo/{g; a bar}' myfile
sed: -e expression #1, char 0: unmatched `{'
What's wrong with it?
3
votes
2answers
2k 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/ /;/" ...
3
votes
2answers
828 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?
3
votes
3answers
1k 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 ...
3
votes
2answers
86 views
How to convert *text* to {\i text} with sed?
I would like to replace every occurrence of *text* into {\i text}.
text *text* text
*text* text *text*
*text text text* text
should become
text {\i text} text
{\i text} text {\i text}
{\i text ...
3
votes
2answers
628 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 ...
3
votes
3answers
126 views
Sed processed file displays differently in vi vs cat
I can't tell if sed is mucking my file up. In vi or less it displays properly, but cat and more insert other characters. why are they showing up differently
I am on a redhat linux system with a ...
3
votes
5answers
481 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 ...
3
votes
6answers
833 views
Text file look-up by column
I have a file in this format:
[#] OWNER_NAME NAME SIZE
[6] Robottinosino Software 200
[42] Robottinosino Ideas worth zero 188
[12] ...
3
votes
4answers
365 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 ...

