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

learn more… | top users | synonyms

-1
votes
1answer
46 views

Can the string “abcedf” be matched to string “bafcde” in a single line command?

I am planning to implement an indexing structure in my program. For example, if I have 100 rows in the table, I will number these rows from 1 to 100 in another column by appending an _ to the end of ...
1
vote
4answers
112 views

add to end of line with pattern matched

inputs: line1 with the PATTERN that contains ( ) line2 with the PATTERN that contains ( ) lineN with the PATTERN that contains ( ) outputs: line1 with the PATTERN that contains ( ) ; line2 with ...
5
votes
3answers
179 views

Is it possible to match multiple specific line numbers (not range) with sed?

Consider: echo -e "a\nb\nc\nd\ne\nf\ng\nh" | sed '3,5a test' This will match lines 3, 4 and 5. But I am trying to match only lines 3 and 5 (not 4). And append 'test' after them.
0
votes
2answers
41 views

Use sed to replace line of text with new line including variable

I was looking at this solution: Using sed to edit lines in a file with a variable and tried to apply it to my situation, but I have only recently discovered sed and haven't got the hang of it yet. If ...
1
vote
3answers
70 views

Pattern Matching and Delete the whole line

I want to delete all the Lines of File 1, if Column1 of File1 matches exactly with Column 1 File2. File 1: r001:21:10 21 AAAAAATTTGC * = XM:21 r002:21:10 21 YAAAATTTGC * ...
-1
votes
2answers
49 views

How to extract submatches from a string and write those to a file?

I want to parse a string for specific submatches and output those - one submatch per line - into a file. Essentially I would like to extract the value of each href-attribute: $ cat Fin <a ...
0
votes
2answers
88 views

Using grep to find first two numbers of IP addresses?

I'm trying to figure out how to use grep to pull out only the first two numbers of an IP address in a list. I've got a routine that is isolating the log entires (from Exim) that I need, now I just ...
4
votes
2answers
125 views

How can I delete all unordered lines from a text file?

Overview Consider an ordered list interspersed with unordered elements, e.g.: Alligator Ant Falcon <-- Baboon Badger Armadillo <-- Caiman Cat How can this list be processed so that all ...
2
votes
2answers
69 views

sed: substitute text with a string that contains special characters

I have a lot of text file with specific tag called "Comment", example I have a file called 3dchess.sh, I need change the tag: Comment=3D chess for X11 to Comment='<span ...
4
votes
2answers
142 views

Replace string with sequential index

Can someone suggest an elegant way to accomplish this? Input: test instant () test instant () ... test instant () //total 1000 lines output should be: test instant1 () test ...
0
votes
3answers
64 views

removing spaces from first column

My input file has positions in first column with different number of spaces (or no space) 16504 16516 1650811 16520 1651 16524 16516111 16528 165204 16532 ...
6
votes
4answers
375 views

Using sed to add a URL to the start of every line

I have the URL (see below) of a certain web page that lists many different versions of a software package. URL=http://ftp.gnu.org/gnu/wget/ The following one-liner gets me the latest version tar ...
2
votes
2answers
46 views

join sed output

I'm looking to somehow pass the output of the following command: $ sed -n '5p' file-A into $ sed '5 c\ > "string extracted from previous sed command" file-B So basically, I want to replace ...
1
vote
3answers
52 views

parsing file from ls -laLR

ls -laLR /home/tools > all_site1.txt I need the state of all folders and files in subfolders of /home/tools. With this command I put all in this file. What I need now is a solution how to parse ...
5
votes
4answers
42 views

Increment numbers inside strings by an offset

I want to search a text file for strings like these. [task0] [task1] ... [task1613] and increment each by a given offset, in this case by 3640. So, with the example I gave above, I'd get the ...
5
votes
2answers
317 views

Remove unknown unicode character from textfiles - sed, other bash/shell methods

I need to search and replace all occurences of an unknown character in some files having the same name. Opening such files with vi, I read <91> code for that character. Opening them with nano, I ...
1
vote
2answers
37 views

How can I append multiple extensions to a string with sed?

I want to create a script that will let me download images from entire subreddits at once, but I'm currently stuck on just getting the links right (one problem at a time! ^^). Anyway, since I can't ...
0
votes
2answers
140 views

How do I multiply and sum column data using awk and or sed?

I have several columns of data. I always have the same number of rows (say 5). In the 2nd column, I want to multipy the first value by 5, then the second value by 4, the third value by 3, etc. I then ...
3
votes
3answers
114 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 ...
2
votes
1answer
37 views

Highlight today in Mac's cal

Every Mac comes with cal which shows a simple calendar of current month. Output is something like this: February 2014 Su Mo Tu We Th Fr Sa 1 2 3 4 5 6 7 8 9 10 11 12 13 ...
2
votes
2answers
68 views

Regex `/pattern/g` and ed `:g/pattern/`: which came first, and why `g`?

The g option (e.g. s/pattern/replacement/g) for many tools that use regex-style pattern matching and the :g command in ed/ex/vi/vim have pretty similar usage and meaning: match the given regex ...
0
votes
1answer
66 views

Removal of extra spaces but not the newline from csv files

I have to compare 2 files by using comm command like the below comm -23 csv1 csv2 > csv3 I seem to face the problem that both the csv are generated from different sources (csv1 is extracted ...
5
votes
4answers
209 views

reverse order of paragraphs in file

I have a file containing text in paragraphs (lines with text separated by one or more empty lines). I would like to reverse the order of paragraphs (i.e. last paragraph will become the first, ...), ...
4
votes
4answers
143 views

Print line after nth occurrence of a match

I am looking to display the line 4598 in the following file. Effectively I want to display the line AFTER the nth occurrence of a match. In this case, the line after the 3rd occurrence of <Car>. ...
1
vote
4answers
66 views

Count rows with specific integer in a column

I have 6 columns, each with multiple rows. I want to count the number of rows which have the integer 4 or 5 in the fifth column. A1 jhfj jdhfjkhd kdkfjjh 5 jhsdjkfh A2 ujhf jhdfhsd dsfkks 4 ...
2
votes
1answer
69 views

Counting consecutive repetitions of a specific character (e.g. ,)

Say I have a file with lines like the two following: foo,bar,,baz,xy foo,,bar,baz,xy,, I would like to count how many times I have ,, (two consecutive commas surrounded by any other character) in ...
0
votes
4answers
53 views

What are alternative tools for searching files in full content?

I realized that most of the text related tools I use, grep/egrep/awk/sed are line-oriented. I guess that is a simplified approach to search in files but I was wondering if there is another set of ...
1
vote
2answers
589 views

Can sed replace new line characters?

Is there an issue with sed and new line character? I have a file test.txt with the following contents aaaaa bbbbb ccccc ddddd The following does not work: sed -r -i 's/\n/,/g' test.txt ...
0
votes
1answer
25 views

Help on editing a script using grep and sed

First I'm new to unix so excuse my simple question, also be aware that I work on AIX. I have a lot of sql files that contains the following string: ${DB_USERNAME}.${TABLE_NAME} Can anyone please ...
1
vote
1answer
54 views

Insert a line after a block of text with sed

I have text file: file.conf ### Option: LogFile # Name of log file. # If not set, syslog is used. # # Mandatory: no # Default: # LogFile= ### Option: LogFileSize # Maximum size of ...
1
vote
1answer
43 views

Why isn't sed using the extended regex mode by default?

I am using sed. I was using a regex that was correct as far as I could see but sed did not do anything. Turns out that I was using \s+ which sed can not understand (?) and when I switched to [ ]+ it ...
1
vote
1answer
39 views

pcregrep fails due to segmentation fault

I am extracting the HTML source of a web page using the below command. curl http://www.linkedin.com/pub/ramesh-venkataraman/63/621/b42 -s | w3m -dump -T text/html > foo.txt The foo.txt has the ...
1
vote
1answer
35 views

Sed: search and replace a patten with special characters which has a arbitrary string in between

I have a load of nfo files which I want to make some global substitutions for some values within each file e.g. a.nfo has the value <set>Tonight&apos;s Menu ( June 2012 )</set> ...
3
votes
3answers
166 views

extract only the substring after double quotes - grep

I have a file which is as below. <a href="http://firstlink.com" title="title1"> <a href="http://secondlink.com" title="title2"> <a href="http://thirdlink.com" title="title3"> <a ...
1
vote
2answers
77 views

bash replace part of string based on location [duplicate]

Sorry I've looked around for a one line solution ( as bash offers ) to replace part of filename. Given that a folder has image sequence like ve2_sq021_sc001_v000.0101.jpg ...
2
votes
2answers
48 views

Why don't I need to escape a character class in sed but I need to escape the rest?

I am confused with sed. I had a file with the following contents: [ 2014/02/10 14:45:15 | INFO ] Saving data for customer: 273667 [ 2014/02/10 14:45:15 | INFO ] Saving data for customer: ...
2
votes
2answers
93 views

SED change only start of line and preserve end of line

Basically I have a situation where I frequently need to modify my hosts file to access different servers at my workplace. In an attempt to automate the process. I've been trying to use sed to make the ...
1
vote
1answer
77 views

How to get the JSON STRING from the given string

I have a string given below. I just need to get the first accountHeader (bolded) JSON string, I tried everything.. It didn't work. Can you help me to get the data? <START ...
0
votes
1answer
37 views

All occurrences of a regex get removed without using the “global” modifier

I have the following text in the file sentence.txt Linux refers to the family of Unix-like computer operating systems that use the Linux kernel. Linux can be installed on a wide variety of ...
1
vote
2answers
66 views

Text substitution inside compressed files

I have several zip files, each one having several text files inside them. I want to substitute all occurrences of a in these text files with b, and leave them zipped with the same structure. If I ...
1
vote
3answers
52 views

sed or grep to get only the values without numbers

I have a file like below. 1. VMware ESX 2. SCCM 3. Healthcare Information Technology 4. vSphere 5. Citrix Metaframe 6. IT Service Management 7. ITIL 8. Active Directory 9. Project Portfolio ...
0
votes
4answers
112 views

Removing certain fields from a CSV file

I have a large CSV file (~15GB) which a client has sent over as an export of a table. The format is a follows: field, ... , field, field which sometimes contains commas, another field which sometimes ...
1
vote
2answers
55 views

Different behavior of sed with respect to an array and a string in the presence of backslashes

Let the file foo.txt contain foo\\ bar baz xx Why does the following correctly change "foo\\ bar" to "someotherstuff" in foo.txt? sed -i 's/foo\\\\ bar/someotherstuff/gI' foo.txt while this ...
2
votes
1answer
57 views

Does [[:space:]] work for sed patterns on all Linux distros?

Using [[:space:]] looks a little non-elegant and, quite frankly, Microsoft-ish for sed match patters. I was trying to extract "Last" out of "First Last" and either of the following worked: echo ...
1
vote
1answer
67 views

test recursive sed search and replace before running

Is there anyway to do a trial run of a recursive search and replace of sed before actually running it? I just want to print out the results before actually doing the search and replace. Something ...
3
votes
1answer
44 views

Reorder Multiple Line Blocks with Sed

I am trying to reorder generated output as follows; given a tex fragment as follows, I want to move the /^Constant/ down below /Dummies/, with the line before and after /^Constant/. Further, I would ...
1
vote
1answer
52 views

Shell script to list locked packages in FreeBSD

I'm trying to make a oneliner to be able to list all locked (forbidden to upgrade) packages in FreeBSD. Normally, you issue this command: pkg info -ak It outputs list of all packages in system ...
-2
votes
3answers
71 views

Deleting Lines, which match a particular Identifier from another file

I have 2 files. File 1, has an identifier (eg. D7MHBF:11:1449:1988) and every new entry starts with @. It has few more fields, which are not important in our analysis. File 2 consists of a column of ...
6
votes
1answer
159 views

How can I replace a string in a file(s)?

Replacing strings in files based on certain search criteria is a very common task. How can I replace string foo with bar in all files in the current directory? do the same recursively for sub ...
0
votes
3answers
138 views

Awk/grep/sed get comma separated list of numbers from lines of text

I have a log file that has lines that look like this: blah blah blah Photo for (702049679 - blah blah blah Now I know I could get all the lines like that from the file by doing: grep "Photo for" ...