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

learn more… | top users | synonyms

5
votes
4answers
889 views

Why does sed act differently depending on the output file?

If I run: cat messages.txt | sed -e 's/a/a/g' > messages.txt on one large file (2500+ lines) I find that the resulting file will only have about 900 lines after the command in cygwin and will ...
24
votes
7answers
4k views

How to achieve portability with sed -i (in-place editing)?

I'm writing shell scripts for my server, which is a shared hosting running FreeBSD. I also want to be able to test them locally, on my PC running Linux. Hence, I'm trying to write them in a portable ...
0
votes
1answer
43 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 ...
0
votes
1answer
29 views

Delete Non numeric row from a csv

I have used sed to allow only numeric and csv format ,I have used sed '/[^0-9, -, .]/d' as regex. It has still "_","1-0","1-1" character as o/p which worries me. sed 's/'"$(printf '\015')"'//g' ...
0
votes
1answer
60 views

replace long text string (script with MANY special characters). sed, awk, grep

My site was hacked / infected. I replaced the url of the malicious link, but other elements in the malicious script are still making my site get blocked. Without inserting a hundred or so "escapes", ...
1
vote
1answer
148 views

Why doesn't this sed substitution for a non-ASCII byte work?

I tried this substitution with GNU sed on OS X (4.2.2 installed through Homebrew). But it doesn't work. printf "Hello\x92 World" | gsed -r "s/[\x92]/'/g" The expected output is: Hello' World The ...
0
votes
1answer
49 views

Multiple sed commands in a file

I want to save a .txt file to 2 separate .txt files using command placed in a .txt file. I can use sed -e '1,20 w file1' -e '21,$ w file2' input.txt But how would I plac e this command in a file ...
1
vote
1answer
158 views

Sed regex printing into a file.txt

I'm connected to a MQTT stream, and I filter out the data by entering : mosquitto_sub -d -t +/# 2>&1 | sed -n "/PUBLISH/{s|.*\('.*',\).*|\1$(date),|;N;s/\n//;p}" into the Ubuntu terminal. ...
17
votes
2answers
70k views

How to insert text after a certain string in a file?

Right now I'm using echo "Hello World" >> file.txt to append some text to a file but I also need to add text below a certain string let's say "[option]", is it possible with sed? EG: Input ...
4
votes
4answers
929 views

How do I delete all lines with “.png” in a file?

I've a list of urls of which some point to images: http://s.thebrighttag.com/iframe?c=A5lqOqP ...
-2
votes
4answers
130 views

Insert blank line in UNIX after different number of lines

Suppose I have a file abc.csv with below data: abc def geh ijk lmn opq rst Now, I want to insert blank lines after line2 and line 6. Please suggest which command shall I use.
3
votes
4answers
526 views

How to change the content of file in linux?

I have 3 configuration files in my Linux box which contains config information for a custom application. I want to change some values in configuration files. The contents of the files and descriptions ...
1
vote
1answer
32 views

Escape >');} < / in sed

In 30 files in 30 directories, I want to find: script>');} < /script> and replace it with: script>');} < /script--> What special characters do I need to escape in the find ...
0
votes
2answers
36 views

Sed incremental arithmetic

I want to replace a number at the end of a string by incrementing the number by one each time I run a specific sed line. the string looks like this: server-port=25555 I was thinking I could run ...
-2
votes
1answer
61 views

list of directories into comma separated directories [closed]

Hi I am trying to do scp copy of directories from one environment to another environment. Suppose directories at location \ as dir1 dir2 dir3 and so on. Now as the scp copy I need to list these as ...
0
votes
1answer
65 views

Sed command for performance improvement or tr command for the same

I have a command to replace the non printable characters and single quotes from a file but its taking more time to execute as I am replacing these characters for multiple files and the files size is ...
15
votes
4answers
14k views

Delete the first n bytes of files

I've got an extreme problem, and all of the solutions I can imagine are complicated. According to my UNIX/Linux experience there must be an easy way. I want to delete the first 31 bytes of each file ...
10
votes
12answers
3k views

What's a good way to filter a text file to remove empty lines?

I have a .csv file (on a mac) that has a bunch of empty lines, e.g.: "1", "2", "lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum lorem ipsum ...
44
votes
8answers
102k views

Delete First line of a file

How can I delete the first line of a file and keep the changes? I tried this but it erases the whole content of the file. $sed 1d file.txt > file.txt
-1
votes
3answers
50 views

How to swap two words with sed and with multiple delimiters?

Just letters are accepted in words, any other characters are delimiters. I want to exchange the first word with the third word. sed -E 's/([A-Za-z]+) [^A-Za-z] ([A-Za-z]+) [^A-Za-z] ([A-Za-z]+)/\3 \2 ...
0
votes
2answers
37 views

Remove lines in a file where pattern only occurs once

I have a CSV file. It is ordered by an identifier the first field. The identifier in the first field can appear in either 1 or in 2 lines in the field. I want to remove all lines in the file where ...
4
votes
2answers
1k views

Unix - How to append text to every line of a file except for the header line/first line

I have a file say abc.txt with following data (real data has many records,say around 200) Sno | Name 1 | Jack 2 | Jill 3 | June Now how do I append text to make my file look like this, ...
25
votes
11answers
12k 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
1answer
29 views

string substitution using sed

I have this string for example : asddsa "ll" sadd " fsgsg" "sdafaf" "lkkx" "fsgfgsg" I want to substitute all characters inside " " with a character x. Expected output in this case should be: ...
1
vote
2answers
37 views

Using sed to substitute a string containing variable integers

I have a text file and want to use sed to replace the following string (including the "s): " rel="lightbox[1846]" title=" with # The numbers between [ and ] are variable and change. I want to ...
1
vote
1answer
37 views

How can I get my bash script to remove the first n and last n lines from a variable?

I'm making a script to preform "dig ns google.com" and cut off the all of the result except for the answers section. So far I have: #!/bin/bash echo -n "Please enter the domain: " read d echo "You ...
0
votes
1answer
28 views

Substituting words in parenthesis using sed [duplicate]

I have a basic python script that looks like this: my_name = 'Zed' my_age = 35 my_height = 74 my_weight = 180 my_eyes = 'Blue' my_teeth = 'White' my_hair = 'Brown' print "let's talk about %s" % ...
1
vote
1answer
40 views

Using sed to replace spaces in text files with _ only when between “ ” after specific string

How can I use sed to substitute a space with an _ only when it occurs in a text file after the string title= and between " " For example (lines in a text file): title="This is the title of my book" ...
9
votes
6answers
47k views

How to use a shell command to only show the first column and last column in a text file?

I need some help to figure out how to use the sed command to only show the first column and last column in a text file. Here is what I have so far for column 1: cat logfile | sed 's/\|/ /'|awk ...
0
votes
2answers
163 views

Printing all lines of a file with multiple columns in a shell script

I have a file with 4 columns with the below format: name;user_id;dob;date_of_joining I want to print all the variables in below output via shell script The employee "$name" is having "$user_id" ...
0
votes
1answer
33 views

sed: replace only the first occurrence of wildcard

I am using sed to replace an attribute in an XML file and up until now it was working just fine. I have an XML file that contains a tag like this: <osgiApplication id="com.mycompany.site.app" ...
9
votes
6answers
9k views

Want to substitute only first occurence with sed

Original file claudio antonio claudio michele I want to change only the first occurrence of "claudio" with "claudia" so the file result claudia antonio claudio michele I have tried sed -e ...
0
votes
3answers
17 views

find muli lines in file from other file

i have text file that have multi lines i wanna search for lines containing some text and add # at the start of those lines ex: file1 that i wanna change it acnet 6801/tcp # ...
0
votes
1answer
29 views

print only lines that starts with digit from specific column

I have a file which has second column as hostname of all hosts in my network: Host example.com (x.x.x.x) is up (0.0041s latency). And so on, but many lines doesn't start with hostname in second ...
3
votes
1answer
41 views

Combining every two column values in file

I have a file with (with only one line) with multiple columns say 4 (in this case) : A B C D And I want the output as every possible combination of column values taking 2 at a time like: AB AC AD ...
0
votes
1answer
44 views

How to make sed as fast as grep when replacing values

If I search with grep in a 30 gig database for some values it finds all of them in no time, if I run the same command with sed to replace those values it takes ages. Why is grep so much faster than ...
0
votes
2answers
43 views

Remove string with spaces and quotes from xml file

I would like to remove the string currencyId="GBP" from an xml file. Please note there is a single space before the first letter c. I am having trouble parsing the data and removing this string ...
2
votes
1answer
44 views

How to convert a timezone in logs as they are being tailed

I would like to manipulate the format of a datestamp from a log, in realtime, as it is being tailed. The current format is 2016-04-06T23:19:20.878Z. I would like to convert it to our local timezone ...
0
votes
1answer
31 views

Extract two lines following a line with a specific value

I have a text file and I would like to extract the two lines of data following a line containing only "Name" as a value. Name 032 Express intent to engage in diplomatic cooperation (such ...
2
votes
1answer
45 views

Better way to run a series of seven identical commands in sed? [duplicate]

In the post Sed - Replace first k instances of a word in a file, a user suggests: sed 's/old/new/;s/old/new/;s/old/new/;s/old/new/;s/old/new/' filename as a method of replacing the first k ...
1
vote
4answers
28 views

Change dictionary from finnish-czech to czech finnish

I have this file http://sinivalkoiset.wz.cz/ostatni/finnish-czech.txt and I would like to swap first collumn with the second one. I used the sed commend sed -ne 's/\([^a-z A-Z].*\) \(.*\)$/\2 \1/ p' ...
0
votes
2answers
35 views

backreference sed

i would like to capture as backreference something like this. Example: (a-z) But somehow anything I have tried, didn't work I try this: \(([a-z])\)\ or \('('[a-z]')'\)\
0
votes
0answers
15 views

Dictionary - synonyms

I have czech czech-finnish dictionary and I have two czech synonyms in one row. Example: záchod (m); toaleta (f) WC; käymälä; miestenhuone; naistenhuone; vessa Synonyms are divided by semicolon ...
-3
votes
1answer
34 views

How do i create a sed script to prompt the user to replace a number in a file? [duplicate]

I'm new in Unix, and I'm learning about sed command. I need to write a very small script for searching a number in a file and replacing it. I tried doing grep, and than deleting the specific phone ...
1
vote
1answer
28 views

How can I pass the sed command output to variable without losing data format

I have a file with five lines with pipe separated data.Using sed command I am removing the pipe Only at the end of every line. But when I am passing below command output to a variable then data lose ...
0
votes
4answers
53 views

How do I insert the first line of one file into the first line of another?

I've got a head command to get the first line of a file and a sed command to insert before the first line of a file. How do I combine these so I can insert the extracted first line of one file before ...
0
votes
2answers
51 views

Piping sed/grep output does not work

If I run the commands bellow, only out1 has output, the out2 and out3 are empty. # this is just to generate a self-signed certificate openssl genrsa -out /tmp/ssl.key 2048 openssl req -sha256 -new ...
4
votes
4answers
14k views

Using sed/awk to remove anything after first space

aaaaaaaa 09 bbbbbbbb 90 ccccccccccccccc 89 ddddd 09 Using sed/awk/replace, in the above text I want to remove anything that comes after the first space in each line. For example the output ...
3
votes
3answers
97 views

Replace multiple lines with a string when the line numbers are stored in a file

I have a file with few million lines, all the same. just for an example: Known Known Known Known Known Known ... I have another file with few thousand line numbers, for an example: 3 5 6 ... I ...
0
votes
3answers
45 views

How can i extract data from this line?

xyz /mnp/opq/p/1926081/sp/192608100/serveFlavor/entryId/0_ffx7sljc/v/2/flavorId/0_,n6h1xlhl,ea3anltg,jfs8boo9,w2v1vsk2,ze8e1q2s,to8w2p18,/forceproxy/true/name/new in this line this part ...