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

learn more… | top users | synonyms

4
votes
4answers
111 views

Extracting strings from between special characters

I have a large text file created from combining lots of html files. cat *.html > all_html_files.txt Inside the text file are specfic strings that I want to extract to another text file. For ...
5
votes
7answers
295 views

Remove lines from tab-delimited file with missing values

I have a large (~900MB) tab-delimited text file that I will be processing in a downstream program. I need to delete any line with a missing value. The correct number of columns are on each line (so ...
1
vote
2answers
24 views

sed to match zero or more number of spaces in a string

function getVal { sedPattern='s/^.*"keyVal":"\([^"]*\)".*$/\1/' finalSedPattern=${sedPattern/keyVal/$2} echo $(sed $finalSedPattern <<< $1) } This is my Json parser written using ...
6
votes
2answers
5k views

How to search and replace multiple needles by one word via one expression?

Assume you have a text file: foo fnord bar bizz foo poit And now I would want to replace both "foo" and "bar" into "narf". I know I could use: sed -e 's/foo/narf/g' -e 's/bar/narf/g' fileName ...
4
votes
1answer
116 views

In POSIX sed, does the period (dot) match a newline in a multiline pattern space?

In GNU sed it works. For example, this matches two blank lines in a multiline pattern space (created with N) /^\n$/ Is this standard?
3
votes
2answers
112 views

need to replace a file contents

I am stuck in a situation. I have a file script.txt with contents $cat script.txt SET TABLESPACE CONTAINERS FOR 3 -- IGNORE ROLLFORWARD CONTAINER OPERATIONS USING ( FILE ...
3
votes
2answers
57 views

Replace only on the first matching line with sed

Using BSD sed (no GNU extensions), how can I perform an operation similar to the example provided below, but where instead of the line number, the replacement is performed on the first line in which a ...
5
votes
5answers
426 views

Remove lines from file up to a pattern, unless the pattern doesn't exist

I am attempting to write a filter using something like sed or awk to do the following: If a given pattern does not exist in the input, copy the entire input to the output If the pattern exists in ...
4
votes
3answers
413 views

Parsing Log Files with sed -e. Need to count unique class names

I have a file, let's call it filename.log, in it I have something like this (2014-11-18 14:09:21,766), , xxxxxx.local, EventSystem, DEBUG FtpsFile delay secs is 5 [pool-3-thread-7] (2014-11-18 ...
6
votes
2answers
413 views

Why does [0-9]* match where there aren't any digits?

so the command is : echo "abc 123" | sed "s/[0-9]*/h/g" and im getting output as hahbhch h how am i getting this output? the output i expected it to be is abc h which im getting by ...
10
votes
5answers
15k views

Delete range of lines above pattern with sed (or awk)

I have the following code that will remove lines with the pattern banana and 2 lines after it: sed '/banana/I,+2 d' file So far, so good! But I need it to remove 2 lines before banana, but I can't ...
2
votes
5answers
160 views

Extract lines from a file containg From <str1> To <str2>

Let's says there is file "abc.txt" that contains a paragraph/lines: Hello, how are you doing sir? when are you going to arrive at the SBAHN platform to catch the train? How can I extract all the ...
0
votes
2answers
63 views

Substituting \ for \\ in column

I exported a column of thousands of salts from mysql for hashcat. Every instance of the character \ was replaced with a double character \\ in the exported column. Is there a way to either sed or awk ...
4
votes
4answers
271 views

Remove a character between 2 known strings

I have a data set like below: \"XXX \ START sapiodj \\" aj \d 2387 END hddo\" START bbcc \\" END ss My Requirement: I want to remove all occurrences of backslash\ and double quotes " between ...
3
votes
7answers
6k views

How to remove duplicate letters using sed?

Using sed, how can I remove duplicate letters from HEADERS within a text file? NNAAMMEE nice - run a program with modified scheduling priority SSYYNNOOPPSSIISS nice [-n ...
0
votes
2answers
42 views

Remove both lines if column A values are repeated in next row

My input is like this: pak_c35079_g1_i1|m.14890 Pfam pak_c20797_g1_i1|m.3458 ProDom pak_c20797_g1_i1|m.3458 Pfam pak_c28265_g1_i3|m.9595 TIGRFAM pak_c28265_g1_i3|m.9595 ...
2
votes
2answers
58 views

Exchanging One Column From File1 with files from File2

I have two files. The first file has 11 columns, for example: 1 2 3 4 5 6 7 8 9 10 11 The second has 10 columns and might look like this: 11 22 33 44 55 66 77 88 99 100 What I want to do is look ...
-1
votes
1answer
64 views

bash - sed: -e expression #1, char 15: unterminated `s' command

I try to use sed command inside a loop to replace IP address, but it is always giving me this error: sed: -e expression #1, char 15: unterminateds' command` even it is not missing the end slash. ...
3
votes
2answers
24 views

vi — replace chars from part of line

I've got a file with all Belgian cities. Some of these cities have boroughs. Lier;stad;Antwerpen;49,70;33930;Lier;Koningshooikt ...
0
votes
1answer
28 views

Use sed to edit a line in a file with spaces in the name [duplicate]

I am looping through folders, opening a file within the folder with the same name, and doing some manipulation. I'm using sed to manipulate a line: sed "1s/Log R Ratio/$SAMPLE.LRR/" $FILE where ...
2
votes
5answers
40 views

Help on edit a sed statement

I'm writing a script to change on the fly the firefox home page through a text variable: sed -i 's|\("browser.startup.homepage",\) "\(.*\)"|\1 "$ddrs"|' /home/max/.mozilla/firefox/*.default/prefs.js ...
1
vote
1answer
41 views

Don't run this sed when a string is matched?

I have this sed command for deleting numbers inside of tags: sed 's/\(<[^0-9>]*\)[0-9]*\([^0-9]*>\)/\1\2/g' But does anyone know how I can exclude certain strings from this? Now it would ...
1
vote
1answer
70 views

sed returns the following error sed: -e expression #1, char 101: unknown option to `s' [duplicate]

I'm writing a script to replace a label <p> with a path in some text files (*.prop). However , when I run it I get the following error: sed: -e expression #1, char 101: unknown option to `s' ...
2
votes
2answers
210 views

Solaris using sequence generator at the beginning of each new line

I am new to shell scripting. I created a script that extracts data from a database and loads it to a csv file. I am using SunOS hstz1454 5.10 system. I am trying to do a sequence at the beginning of ...
1
vote
1answer
47 views

Remove leading 0 from specific field

I have a file with multiple fields but only want to remove the leading 0s from the 2nd column (col B) rather than all of the columns. I have tried: sed 's/^0//g' filename > new_filename but this ...
4
votes
3answers
7k views

Replace regex capture group content using sed

I'm trying to edit my nginx.conf file programmatically, which contains a line like this: access_log /var/log/nginx/access.log; which I want to look like this: access_log ...
1
vote
2answers
54 views

Linux convert a number into a string on the 6th column

I created a script that extracts data from a database and loads it to a csv file. I am using SunOS hstz1454 5.10 system. Is there a way I can translate/convert a number to a string on my csv file. So ...
3
votes
3answers
76 views

Print all matching lines and the previous unindented line

For example, lets say I want to find all lines with 'matching' and the previous unindented lines in this text. Container 1 some text some text matching text some text Container 2 some ...
2
votes
1answer
86 views

Modify filenames in Makefile

For reasons that are not important here, I have a source code that I process automatically file-by-file and the processed source files are renamed in a systematic manner. For example, I start out with ...
14
votes
5answers
32k views

Remove line containing certain string and the following line

I use this cat foo.txt | sed '/bar/d' to remove lines containing the string bar in the file. I would like however to remove those lines and the line directly after it. Preferably in sed, awk or ...
4
votes
2answers
136 views

concatenate n lines with sed

Recently, I asked a question how to sort pairs of lines, and one of the answers suggested concatenating lines with sed, like this: cat file.txt | sed -n 'N;s/\n//;p' | sort -t";" -k43,43n | perl ...
0
votes
1answer
30 views

In bash I'm unable to concatenate a string using a variable defined from output of echo + sed

I'm trying to concatenate a string with $result. Placing another string BEFORE $result results in the two strings not being concatenated: ...
3
votes
1answer
203 views

Using SED to cut after 7th character of each line

Just trying to cut any characters after the 7th on each line of a text. I initially tried this: sed 's/(.{7}).*/\1/' TestText.txt But got this response: sed: 1: "s/(.{7}).*/\1/": \1 not defined ...
0
votes
4answers
71 views

Why isnt this simple grep/sed script working [closed]

I have a variable with, among others, a line that looks like this iBMC=277 I'm trying to match that like this: BMC="$(echo "$allnow" | grep iBMC=[0-9] | sed 's/iBMC=//g')" This makes BMC empty. ...
7
votes
5answers
607 views

How to abbreviate /path/to/file to /p/t/file

I'm looking for an elegant one-liner (eg, awk) that will shorten a string of a Unix path using the first character of each parent/intermediate level, but the full basename. Easier to show by ...
4
votes
2answers
1k views

How to “grep” for line length in a given range?

NOTE: This question is the complement of this Q&A: How to grep for lines shorter or longer than specific thresholds? I need to get only the lines from a textfile (a wordlist, separated with ...
11
votes
2answers
3k views

Using sed to color the output from a command on solaris

I have a ksh script that must work on both linux and solaris. I'm trying to color the output of specific commands. It works on linux (specifically RHEL6), but not on solaris (SunOS 5.10). Command ...
4
votes
4answers
2k views

How can I find and replace only if a match forms a whole word?

My script is: n="y" while [ "{n}" = "y" ] if [ $n == "n" ]; then break; fi echo "n is $n" do read -p "Enter the word to find = " word read -p "Enter word to replace = " replace ...
3
votes
2answers
58 views

Two sed commands in one command

I have a file with a lots of names etc. and with spaces in between. Now, I want to delete all trailing spaces and all empty lines in this file with sed. I have two commands for this task but I would ...
0
votes
1answer
24 views

write variable into specific line with sed command

echo -n "##### STATIONS? #######" read station text="stab_site " I need to write echo $text$station into the 27th line of text file. I used below command but it didn't work, sed -i ...
2
votes
1answer
48 views

Add text between multiple lines with sed if previous line does not match pattern

I'm struggling to add <dl> tags around a definition list with sed, and perhaps there is an easier way to do it (I'd love to know). I would like to search a file for any lines that contain a ...
1
vote
1answer
55 views

Why doesn't this work when I add it to the current sed being run?

I have a text file that looks like this: (empty) str int int If I add this s/^/\</g to my current sed (making it 's/\&/\</g;s/\</\ /g;s/^/\</g') this is the output. < ##only ...
1
vote
1answer
21 views

Use both a variable and newline in BSD sed?

I can do this for example sed 's/\>\</\>\ </g' To replace >< with > < However, say I want a variable, like this: sed 's/\>\</\>\ <$1/g' It will interpret ...
1
vote
2answers
52 views

Sed - How can I insert a string after a certain line AND a certain character?

I need to insert some lines of code into a file. The problem is, I want to insert it after some function calling AND after the semicolon behind it, too. I'd like to achieve this just with sed, but I ...
0
votes
1answer
32 views

sed: multiple ranges between patterns, with one pass

I have a script that reads a file of about 3GB and sends it through a pipeline involving a very small number of replacements in a very small part of the file. To accomplish this with minimal overhead, ...
0
votes
1answer
35 views

How to replace word with unique prefix and suffix?

testfile starts with: abc: aprefixAnYthingasuffix This sed operation is NOT working: sed -i 's/aprefix*asuffix/mynewword/g' testfile testfile ends with: abc: mynewword ...
0
votes
0answers
34 views

copying a set of lines into new file

I have a text file with 100,000 lines or more. I want to copy specific lines from the text file into a new file using: sed -n '1,1000p;1000q' orig-data-file > new-file However, I want to create ...
8
votes
7answers
16k views

Print odd-numbered lines, print even-numbered lines

I want to print the odd-numbered and even-numbered lines from files. I found this shell script which makes use of echo. #!/bin/bash # Write a shell script that, given a file name as the argument ...
0
votes
0answers
39 views

How to write into local file and set its content to a variable within a ssh command substitution

I would like to write on local file (or local variable if better) and set its content to a variable within a ssh command substitution. Here is my code : REMOTE_OUTPUT=$(ssh remote@server /bin/bash ...
9
votes
3answers
2k views

Print a line only if the next line does NOT contain a particular match

I am trying to search a log file for logged activities that did not complete. For example, I log a "Starting activity for ID 1234..." and if successful, the next line will be "Activity 1234 ...