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

learn more… | top users | synonyms

4
votes
2answers
283 views

Merge fields in a file

I have a file with 7 columns, a GFF file having chromosomal regions.I want to collapse the rows where REGION ="exon" to only one row in the file.The row has to be collapsed on the basis of regions ...
4
votes
4answers
121 views

How to switch the first letters of two words?

$ echo 'foo bar' | sed SOMEMAGIC boo far $ echo 'one two' | sed SOMEMAGIC tne owo Q: in general, how can I replace the "b" from the "bar" to the "f" of the "foo"? And in reverse too, replace the "f" ...
1
vote
3answers
2k views

How to delete a line with variable search using sed command

I am new to sed. What is wrong with the sed command on deleting the line which matches the input data? InputData.txt 123, 1234, 1453, Datatodelete.txt 1234,hellofirstline 123,hellosecondline ...
2
votes
5answers
279 views

Print text between (and including) two delimiters on a line

What's the best way to print the text between (and including) the first pattern ("SELECT" or "SSELECT") and a second pattern ("[") using a command-line took (e.g. cut, sed, awk)? For example, given ...
1
vote
3answers
56 views

How to leave only given characters in a file?

Q: How can I leave only the following characters in a file? the ASCII decimal 10, the ENTER, so the so called "line feed" ASCII printable characters (character code 32-127) The extended ASCII codes ...
-14
votes
2answers
197 views

Add quotes around a value using sed

I created file called Test1.txt and I add a line. The file look like this, TEST = admin Now I want to add double quotes for TEST="admin". How to add double quotes using sed command? I want ...
1
vote
3answers
169 views

How can I delete the first dot before the first number?

How to remove the first dot before the first number? For example I have the following file more file Version …………2.465.76.8.332 I want to get only the version number - 2.465768.332
2
votes
1answer
65 views

How to sed and put back the part of the match? [duplicate]

So these are the original texts: $ printf 'bbb\nb2b\n' bbb b2b This is what I'm doing $ printf 'bbb\nb2b\n' | sed 's/^b[0-9]/XXX/g' bbb XXXb And this is the output that I want :D $ printf ...
2
votes
5answers
144 views

identifying words in file 1 and matching them to second file and printing them

I have two files as follows File 1 has list of Ids ram Tarah taikah jhdfklj File 2 has id=ram;*between*hkjhdswklf kjsdklhglshglkhgklsdgkhdfg id=taikah;jhdjfkhg**kjdfkjksfdjk ...
1
vote
2answers
150 views

Regular expression not working on sed

I am trying to use sed to change the format of decimal numbers in a large CSV file before importing it into a SQLite database. They all have two decimal places, may be negative use comma as a decimal ...
3
votes
3answers
1k 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 ...
1
vote
3answers
351 views

Print the last-but-one field

How to print the word before the last word in line (with ksh or awk or sed or perl one liner) Example 1: echo one two three will print "two" Example 2: echo 1 2 3 4 5 6 will print "5"
2
votes
6answers
85 views

Printing specific section of a line when a trigger value is present

I'm parsing data in the following format: prop1=value1:prop2=value2:prop3=value3+prop1=value4:prop2=value5 parts of the string are delimited by + properties can appear in any order the desired ...
0
votes
2answers
38 views

How can I use sed to only print what appears between these two strings?

How can I use sed to only post what appears between iterations of: "<title><![DATA[" and ]]></title> I tried doing sed 's/<title><![DATA[/(.*)/]]//g' ...
7
votes
3answers
1k views

How to remove a specific character in a string but only if there are no numbers in that line in Linux

I've been stumped with this seemingly simple-to-fix problem.. well, for a while. Here's example output from the file I need to edit: $cat file George Washington Geneva Convention 123,281,029 USD ...
0
votes
2answers
79 views

search for first pattern and copy the string inside the ( ) into the 3rd field in lines where the second pattern found

Here are my spec: first search for "#firstpattern(" copy the content inside the ( ) to the 3rd field inside the ( ) of all lines where the "secondpattern" found. remove the entire line with the ...
5
votes
6answers
975 views

How to match exact string using `sed`? But not the part of it.?

I have a input file FILE1.TXT as below. 11 id1 12 13 AGE = 20 14 NAME = NAME1 15 16 id2 17 18 AGE = 30 19 NAME = NAME2 . . . 110 idXYZ 111 112 AGE = AGEXYZ 113 NAME = ...
3
votes
2answers
270 views

case sensitive substitution; same target ids

I am struggled myself to make a case sensitive replacement in a text file. Please find below a segment of my sed file that I am running as sed -f file.sed < input.txt > output.txt ...
2
votes
5answers
249 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
1k 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
368 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
301 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
4answers
402 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 * ...
0
votes
2answers
98 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
265 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
142 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
1k 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
516 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
253 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
546 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
50 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
78 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
3answers
116 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 ...
6
votes
2answers
1k 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
46 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 ...
1
vote
2answers
1k 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
151 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
60 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 ...
6
votes
2answers
169 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, and vim have pretty similar usage and meaning: match the given regex ...
0
votes
1answer
551 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
329 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
897 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>. ...
2
votes
4answers
164 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
172 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
85 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 ...
3
votes
2answers
8k 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
32 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
113 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
89 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
102 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 ...