Sed is a command-line stream editor for filtering and transforming text.
3
votes
3answers
176 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.
2
votes
2answers
521 views
regex replace text in xml file within node from the command line
I have an XML file and I would like to replace everything that is between the open and closing tag within multiple instances of the g:gtin node with nothing.
Is this possible from the command line, ...
1
vote
1answer
75 views
What happens for the last line when using N command with sed
Here is my sed command:
echo -e "AB\nCD\nEF\nGH" | sed 'N; D;'
It prints:
GH
while sed processing input line by line, what happens with the last line? (When after that sed saw End-Of-File)? I ...
4
votes
1answer
152 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:
...
2
votes
1answer
169 views
Is it a good idea to supplement shell script with perl purely for use of regex?
I'm looking for a simple way to utilize regex in a UNIX shell script where not every system will have perl extensions built into grep. What is really helpful about perl regex here is back/forward ...
0
votes
2answers
587 views
nmap script needed to output text into csv format
ultimately I am looking to insert some info into a database. So I'm trying to output a file from something (probably nmap) that is formatted like this:
10.1.150.1,Up,1/3/2012,host.domain.lan
...
9
votes
3answers
772 views
Substituting strings in a very large file
I have a very long series of urls with no separating character, in the same format as below:
http://example.comhttp://example.nethttp://example.orghttp://etc...
I want each URL to be on a new line. ...
1
vote
2answers
128 views
sed substitution matches too many inputs
This is my command:
echo "Test" | sed -f <(sed -e 's/.*/s,&,gI/' mydic)
The file mydic contains 2 columns delimited by commas (,)
a,AlphabetA
.
.
.
e,AlphabetE
.
.
...
3
votes
1answer
534 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 ...
0
votes
2answers
235 views
Removing a long string from php files - using grep and sed?
I have encountered some malware on my Linux server, and am trying to remove it from many php files.
I've tried endlessly with grep | sed and grep | tr and couldn't even erase a simple text string, ...
2
votes
1answer
518 views
Variable substitution into a sed script
After running this code
#!/bin/bash
rm -f myfile myfile-individual
echo "Greetings, foobar!" > myfile
MYSTRING="Jack"
cat myfile | sed 's:foobar:$MYSTRING:' > myfile-individual
we end up with ...
2
votes
2answers
331 views
Search and replace with sed
Last week I accidently externalized all my strings of my eclipse project. I need to revert this and my only hope is sed. I tried to create scripts but failed pathetically because I'm new with sed and ...
1
vote
2answers
268 views
How can I delete lines that has an empty first column?
INPUT:
CC2352345|m,safnlasndfmnyxdcvyxcvyxcv |klasjdf |3|lasdjflasdf| |2345567356
CC3543353|asdfasdffghntz |klasjdf |3|lsajdfl | ...
1
vote
6answers
261 views
Print word that doesn't contain character
My file is in the following format:
this!,is!,another!,test,yes!
this!,is!,another!,column,yes!
no,not!,another!,column
My output should be:
test
column
no
It shouldn't contain the '!' ...
2
votes
1answer
852 views
Swap two columns in a CSV using SED
I have a CSV file that contains 10 different fields (, is the deliminator). Example data:
student-id,last,first,hwk1,hwk2,hwk3,exam1,hwk4,hwk5,exam2
pts-avail,,,100,150,100,200,150,100,300
...
4
votes
2answers
373 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 ...
1
vote
1answer
664 views
Use AWK add new line if match exists
I have a file with the following format:
aaa@ bbb
bbb@ ccc& ddddd@ aaaf
ccc@ ddd& eee: abcd& dassda
The output should be:
aaaa@ bbb
bbb@ ccc&
ddddd@ aaaf
ccc@ ddd&
eee@ ...
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
469 views
simple script rotating table
I'm trying to write a simple script, that just rotates csv table.
I mean I have some file:
head1;head2;head3
field11;field12;field13
and so on.
All I want, just to make my file
...
1
vote
2answers
831 views
Recursively replace string in files
I want to replace the backslash in the string page_path\example_filename.txt with a forward slash. I also want to be able to run this on a large file system and have it recursively search all ...
2
votes
2answers
656 views
Sed: replacing digits
I am trying to write some install scripts for when I install/upgrade Ubuntu. I'm having trouble with fixing logrotate.d:
/var/log/ufw.log
{
rotate 7
weekly
missingok
notifempty
compress
delaycompress
...
1
vote
2answers
384 views
To pass a parameter with sed
For my script I have to read the data from a file.
For example I have 2 files: one is a text file, the second is a csv file with 10 columns.
What I am trying to do is to read the csv and change the ...
0
votes
3answers
507 views
What is the purpose of -e in sed command?
I can't find any documentation about the sed -e switch, for simple replace, do I need it?
e.g.
sed 's/foo/bar/'
VS
sed -e 's/foo/bar/'
1
vote
2answers
2k views
Using sed to get rid of characters < > ,
I have a file with lines as follows:
...
... <230948203[234]>, ...
... <234[24]>, ...
..
I would like to use sed to remove the characters < , and > from every line
I tried using ...
6
votes
3answers
7k views
What characters do I need to escape when using sed in a sh script?
Take the following script:
#!/bin/sh
sed 's/(127\.0\.1\.1)\s/\1/' [some file]
If I try to run this in sh (dash here), it'll fail because of the parentheses, which need to be escaped. But I don't ...
3
votes
1answer
166 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.
...
1
vote
2answers
1k views
grep piping into sed, replacing inline; but I want sed to print the filename and changed line. Is it possible?
Here's my command (break intentional):
grep FOO "/Users/gjtorikian/blah" -l | xargs sed -i '' '/FOO/{s/FOO/BAR/g; w /dev/stdout
}'
At the high-level: grep for FOO in the blah directory; pipe in ...
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
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 ...
5
votes
1answer
132 views
Confusion about sed and '>'
The following command works fine:
sed s/input/raw_input/ p.py >p2.py && mv p2.py p.py
However, the following command turns p.py into an empty file:
sed s/input/raw_input/ p.py >p.py
...
5
votes
1answer
3k views
Extracting a regex matched with 'sed' without printing the surrounding characters
To all the 'sed' doctors out there:
I have a seemingly trivial 'sed' question to which I have not been able to find
a solution.
How can you get 'sed' to exctract a regular expression it has matched ...
1
vote
2answers
145 views
Where does the -i option in sed store the lines matching a regex?
I am trying to release space in my server's hd so I'm matching old dates in a log file with sed and it is decreasing but my root partition is now using 98% percent of it's space instead of 91% it was ...
17
votes
9answers
3k views
How can I write to the second line of a file from the command line?
I have an external program that produces an output file (largish, 20K lines possible).
I need to insert a new line between the existing line 1 and line 2. I've been looking at awk and sed - I use ...
1
vote
2answers
162 views
Prepend x/ to line where x is a variable on each line
I want to add $x/ from data formatted as $x-xyz (xyz is a mix of text and number to be ignored).
Example data
AB-10C
CD-02E
Output
AB/AB-10C
CD/CD-02E
How do I describe the variable as text ...
4
votes
2answers
326 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 ...
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 ...
4
votes
6answers
2k 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 ...
6
votes
1answer
224 views
How to detect and delete lines containing ˆ@
I have a simple problem:
In my file, the are lines containing the string ˆ@ˆ@ˆ@ˆ@ˆ@ˆ@. I just want to delete all lines with this string, using for example the sed or grep commands.
And I would like ...
4
votes
2answers
448 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 ...
8
votes
6answers
728 views
Delete lines beginning with #
How do I delete lines beginning with a #, given that there can be whitespace on the left and right of the #?
# Master socket provides access to userdb information. It's typically
3
votes
2answers
215 views
Extracting bigrams with sed
How do I split an entire text consisting of only letters into bigrams.
For example:
odd even
od -> de
dd -> ev
This is what I have so far but it does not produce the desired result.
[some ...
3
votes
2answers
388 views
sed inline editing fails for multiple sed statements
I want to append #SB# at the beginning of every line where it matches a given string
incron.sh
sed -i -e'/test1/s/^/#SB#/g' file1
sed -i -e'/test2/s/^/#SB#/g' file1
sed -i -e'/test4/s/^/#SB#/g' ...
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?
1
vote
2answers
519 views
put tab before every output line on AIX/ksh
0:root@SERVER:/root # echo "something" | sed -e 's/^/\t/g'
tsomething
0:root@SERVER:/root #
AIX/ksh .. why doesn't it works? I just want to put a "tab" before every line!
2
votes
4answers
1k views
Using sed to convert newlines into spaces
Say I have a shell variable $string that holds some text with several newlines, e.g.:
string="this
is
a test"
I would like to convert this string into a new string new_string where all line breaks ...
4
votes
4answers
5k views
SED: insert something to the last line?
The command inserts a tag to the beginning of a file:
sed -i "1s/^/<?php /" file
How can I insert something to the end of each file with SED?
6
votes
4answers
3k views
How can I use sed to replace a multi-line string?
I've noticed that, if I add \n to a patter for a substitute using sed, it does not match. Example:
$ cat > alpha.txt
This is
a test
Please do not
be alarmed
$ sed -i'.original' 's/a test\nPlease ...
2
votes
1answer
273 views
Getting data section with maximum element
I have some data stored in JSON-like format.
{
{
value1: 14,
value2: 12,
value3: 1
},
{
value1: 4,
value3: -1
}
}
How can I get subsection ...
1
vote
1answer
411 views
Insert after a second pattern in sed
I'm using sed to edit a text file. The command:
sed -n -e '/pattern1/,/pattern2/p' test01.txt
show the lines between pattern1 and pattern2. But how can I insert a new line after this pattern2? my ...
1
vote
1answer
380 views
reference problem in using sed
I'm trying to use sed command to find/replace a pattern in file.
It gives:
sed: -e expression #1, char 27: invalid reference \1 on `s' command's RHS
My command is:
sed ...