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

learn more… | top users | synonyms

-5
votes
1answer
327 views

sed and awk challenge [closed]

Here is a great challenge for someone to attack! Is there a way to achieve the following using sed and/or awk. Please see myfile.txt below: $cat myfile.txt: entry "entry-header" entry-top name ...
2
votes
3answers
2k views

Grepping several lines from Nmap output

I apologize in advance as this is a simple question. So I'm attempting to scan a Class C to identify all mail servers by IP. The vast majority of IPs aren't mail servers. I'm trying to filter out the ...
2
votes
1answer
269 views

Replace “<?php print t('Blabla'); ?>” to be “Blabla”

Lets say in file.php, there is lots of php print text: <?php print t('Blabla'); ?>, <?php print t('Text Here'); ?>, etc. What I need is to remove <?php print t(' and '); ?> of the ...
8
votes
2answers
12k views

How to replace a string with a string containing slash with sed?

i am looking for a way to replace a string in a file with a string that contains a slash by using sed. connect="192.168.100.61/foo" srcText="foo.bar=XPLACEHOLDERX" echo $srcText | sed ...
5
votes
3answers
3k views

How can sed output be formatted like printf's formatted printing?

Can sed replace text with a string formatted like printf's formatted printing? The following sed command replaces a line starting with the current value of "$domain" with several values specified in ...
2
votes
2answers
246 views

How do I ad an if statement (regarding punctuation in a word) to this bash script

I have this bash script #!/bin/bash cat $@ | while read line do for word in $line do echo $word | circling-the-square # here's where i need to add the if statement: ...
12
votes
6answers
7k views

How can you combine all lines that end with a backslash character?

Using a common command line tool like sed or awk, is it possible to join all lines that end with a given character, like a backslash? For example, given the file: foo bar \ bash \ baz dude \ happy ...
1
vote
2answers
131 views

How to parse fields out of a text file and write them to another file

I have executed a database command and have generated an output file of the results. Each field name starts with dbt_xxxxxx Each field value is the value after the equal sign. how can I parse the file ...
4
votes
3answers
408 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 ...
8
votes
4answers
5k views

Remove files older than 5 days in UNIX (date in file name, not timestamp)

I want to delete log files which are older than 5 days from a directory. But deletion should not be based on the timestamp of file. It should be based on the name of file. For Example todays date is ...
4
votes
2answers
673 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 ...
2
votes
2answers
405 views

Detect pattern repetition in a file

I want to make sure a file follows the following pattern: ... ... ... foo ... foo_KO ... ... ... bar ... ... ... ... bar_KO ... The file is extremely long, so, in other words, I want to make sure ...
1
vote
3answers
3k views

sed pattern matching

I recently asked someone at work about how to take the output of ipcs -qa and make it space delimited, so I can parse it/store it in the database for monitoring. He gave me this: ipcs -qa | sed 's/ [ ...
3
votes
3answers
210 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
2k 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
87 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
322 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
230 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
2k 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
2k 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
135 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 . . ...
4
votes
1answer
4k 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 ...
2
votes
1answer
2k 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
601 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
951 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 | ...
2
votes
1answer
2k 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
692 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 ...
2
votes
1answer
3k 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@ ...
6
votes
3answers
7k 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
944 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 ...
2
votes
2answers
2k 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 ...
3
votes
2answers
4k 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
974 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 ...
4
votes
3answers
4k 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/'
28
votes
3answers
56k 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 ...
4
votes
1answer
883 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. ...
3
votes
5answers
668 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
229 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 ...
6
votes
1answer
161 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 ...
1
vote
2answers
173 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 ...
1
vote
2answers
466 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
627 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
447 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
5answers
5k 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
614 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
840 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
2k 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
368 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
784 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' ...
2
votes
1answer
749 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?