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

learn more… | top users | synonyms

0
votes
3answers
37 views

Extract a word from a line, doing this to a specific number of lines [closed]

I want to extract a word from a line, doing this to a specific number of lines. I have something like this. 1. Bill 2. Joe 1 3. Sue 1 I want to extract separately the words Bill, Joe and Sue. I ...
4
votes
5answers
11k views

How to add text to end of line when pattern is 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 ...
-3
votes
1answer
27 views

AWK interval expressions to clean IP [closed]

Credit to a great many entries from you online and manuals. It works, but, is it efficient enough? Do you approve? {Reading logs +- 5k items fast as I can see} Source data from file "myAwk3.txt": ...
2
votes
7answers
2k views

Adding a character to every other text line

I have a huge (ca. 20G) text file which contains millions of passages (a.k.a. paragraphs) with headers. Headers and paragraphs are always one line each, e.g., Sunshine This is a sunny day. ...
5
votes
9answers
4k views

Is there a faster way to remove a line (given a line number) from a file?

A related question is here. I often have to edit a large file by removing a few lines from the middle of it. I know which lines I wish to remove and I typically do the following: sed "linenum1,...
4
votes
4answers
1k views

Delete the matching line and several more from a file

I have a text file called file_a.txt. My first command is grep -A 12 ".production =" file_a.txt The output is a few block. Each block of string contains 13 rows I specifically want to delete all ...
1
vote
1answer
44 views

Sed, Awk Multiline log file, a number {0-999] identify relation

Short version of issue: Need to extract from logs: {Using sed and awk on Linux} Time, PlayerName{Can have many special characters}, GUID, IP from log example at end. Longer version: How to combine 2 ...
3
votes
3answers
1k 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
5answers
10k views

Deleting Lines Containing a Specified String in Different Files

Suppose there are two files in web/ named foo.php and bar.php. The 1st line of foo.php is "sdajgeSTRINGdsad" and the 10th line of bar.php is "gdfhu98324STRING". The task is to first locate these two ...
1
vote
4answers
299 views

Add line to configuration file from a bash script?

I am trying to figure out to locate a line in a configuration file, then to drop down two lines then to insert a line of code. I was attempting to do this in awk/sed but got stuck on the carriage ...
4
votes
3answers
6k views

Sed — replace a character in a matched line in place?

In a file containing lines like this one: # lorem ipsum blah variable I would like to remove # (comment) character in the same line that contains a specific string, in place. Is sed good for this? ...
4
votes
3answers
292 views

sed insert in the beginning of multiple files is not working

I came across a lot of answers, including theses: sed on OSX insert at a certain line http://stackoverflow.com/questions/14846304/sed-command-error-on-macos-x http://stackoverflow.com/questions/...
7
votes
3answers
6k 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 ...
2
votes
3answers
174 views

Append a column

I've trawled through here and a few other places to try and find an answer to this, but it seems so stupidly simple that no one has answered it. All I need to do is add a single value in a new column ...
6
votes
4answers
1k views

sed with multiple expression for in-place arguement

I am trying to replace multiple words in file by using sed -i #expression1 #expression2 file Something 123 item1 Something 456 item2 Something 768 item3 Something 353 item4 Output (Desired) ...
11
votes
5answers
9k views

Efficient in-place header removing for large files using sed?

The commands below may takes minutes depends on the file size. Is there any more effient method? sed -i 1d large_file
5
votes
4answers
938 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
5k 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
44 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
31 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
72 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
160 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
59 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
161 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. ...
4
votes
4answers
960 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 http://s.wordpress.com/imgpress?resize=136,136&url=http%3A%2F%2Fdata.whicdn.com%2Fimages%...
-2
votes
4answers
155 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
532 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
39 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
63 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
83 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 ...
16
votes
4answers
15k 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 ...
48
votes
8answers
114k 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
56 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
40 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, ...
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
47 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
51 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
30 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
49 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" ...
12
votes
6answers
53k 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 '{...
1
vote
2answers
185 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
36 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" ...
0
votes
3answers
23 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 # ...
1
vote
1answer
34 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
61 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
53 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
57 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 ...