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

learn more… | top users | synonyms

3
votes
2answers
61 views

How to remove certain characters (but not all) before one or more character(s)?

If I wanted to remove everything before a character (say "("), I would just do something like this 's/.*(//g'. Now I want to remove only certain things before a character/some characters, in my case - ...
6
votes
5answers
639 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 examples:...
3
votes
2answers
113 views

Search in specific column for pattern and output entire line

I'm working in HDFS and am trying to get the entire line where the 4th column starts with the number 5: 100|20151010|K|5001 695|20151010|K|1010 309|20151010|R|5005 410|20151010|K|5001 107|20151010|K|...
1
vote
2answers
58 views

advanced sed print + replace [duplicate]

I am interested if there's a more elegant solution to the one posted below. []$ find ./abc -type f -exec sed -n 's/test/best/pg' {} + ; find ./abc -type f -exec sed -i 's/test/best/g' {} + The ...
1
vote
3answers
121 views

How to adjust the spacing between characters following a specific string?

I have a large file ~/foo.txt. Thousands of lines of the form SIZE : X Y are scattered throughout the file where X and Y are integers. My problem is that sometimes the integers X and Y are ...
2
votes
3answers
254 views

Can sed `y` commands be applied only to matching text (instead of to the entire line)

After writing this answer, I did some googling to find out if I could make the sed y// command be applied only to the match and not to the entire line. I wasn't able to find anything relevant. $ ...
1
vote
3answers
91 views

Sed entire word with when replacing single letters, with BSD sed?

I think I found some GNU solutions but I need BSD sed solutions. What I want to do is replace the entire word at once. If the word is "clayii", and my code is sed 's/c/k/g;s/l/i/g;s/a/e/g;s/y/i/g;s/...
4
votes
1answer
192 views

Script that Outputs the Average of Multiple Group lines, from a File

This is a "Small" portion of the file car_sales.txt that I am trying to extract information from (the full file has about 700 lines, with more vehicle brands than are listed here): first_name,...
1
vote
1answer
127 views

“extra characters after command” in the sed command a

#!/bin/bash SVG=$1 CSV=$2 for p in "$CSV"; do LINEA=$(cut -d',' -f1 $p | sed '1d') LINEB=$(cut -d',' -f2 $p | sed '1d') if grep -iq "$LINEA" "$SVG"; then sed '/g id=\"..\"/a style=\"fill:'"$LINEB"';\"...
1
vote
2answers
53 views

sed command to append some lines

I want append some lines through a bash script with sed. The script works but the output is wrong. LINEB="word" sed '/g id=\"..\"/a style=\"fill:$LINEB;\"' file.txt > foo.txt I get <g id="ai" ...
2
votes
1answer
72 views

Find and replace using parts of the found string in the replace string

Let foo be any string. I have to search a file for all occurrences of \LeftLabel{foo} and replace each occurrence with \LeftLabel{\textsf{foo}} but only if foo does not start with a $ ...
2
votes
3answers
200 views

Arithmetic on variable containing ip address

I've got variable network="198.168.1.x" # for ex. 198.168.1.254 I want to create new variables: next_serv="198.168.1.(x-1)" # for ex. 198.168.1.253 max_host="198.168.1.(x-2)" # for ex. 198.168....
2
votes
1answer
110 views

Grepping and sedding IP from ip addr show

I'm trying to get just the local IP of my Arch VM. I've managed to get just the line containing what I want with grep, but I also want to trim it down with sed. inet <<192.168.0.16>>/24 ...
2
votes
1answer
52 views

Explanation for 'sed'

I have a huge text file called helo.lst in which every line starts with the format: #=GS N4TFW2_FUSC1/96-161 AC N4TFW21 I'm only interested in the N4TFW2_FUSC1 part. I need to extract this ...
1
vote
1answer
26 views

Displaying a file in another file [duplicate]

I was wondering how to print the contents of a file above the occurrence of a pattern in another file. I know that using sed -n '/^PATTERN/i\\ INSERTING' filename would insert above the pattern but ...
0
votes
2answers
115 views

How to display each day of a month on a separate line using tr, sed and cal

How to use commands, cal, sed and tr and the pipe to display the numerical days (e.g., 1, 2, 3, etc.) of a month, with each day on a separate line I've tried: cal 12 2015| sed '3,7p' | tr -cs "[:...
-1
votes
1answer
26 views

Display only records if the value in the last column ends with a 4

Display only those records in a file if the value in the last column ends with a 4. How do i do this?
0
votes
1answer
31 views

Filter log on final letter in string [closed]

I have a below requirement to filter a word. My logs will be reporting with codes TML2345I,TML8383E & TML9929W. I want to filter the errors only with strings which have the last character as E. ...
1
vote
2answers
35 views

Print indentations with whitespace only in tree command

I want to list the subdirectories of a directory using tree command. But I don't want to print indentation lines. I only want to have the whitespaces instead. I couldn't find the correct parameter ...
2
votes
2answers
350 views

Curl url txt file, but grep each url seperately from single file

Ok, here's one for you guys that is puzzling me. So I have a text file with lots of url's in it. I'm using curl -K "$urls" > $output to spit the output to my output file. Now for the output of ...
2
votes
2answers
282 views

Insert text in specific lines of a file? with special characters

i know this has been answered several times, but i can't figure out how to use it for my project. i have several php files and want to add a new string between line 11 and 12. this is the string i ...
6
votes
4answers
212 views

Substitute Column With Result of Command

I'm trying to find a way to quickly and efficiently replace a column value for every row in a file with the output of a command. I need to work on multiple files with about 500,000 rows each daily so ...
9
votes
5answers
2k views

Merge two files line by line with the delimiter triple pipe symbol “|||”

I have two parallel files with the same number of lines in two languages and plan to merge these two files line by line with the delimiter |||. E.g., the two files are as follows: File A: 1Mo 1,1 I ...
2
votes
1answer
46 views

To Apply Answers to MCQs

I am now extending my own answers to MCQs, as made How to SED these paragraphs to MCQ format?. Data in file.txt 1 c 2 a Data in exam.tex \item Which of the following lorem ipsun are the best ...
3
votes
1answer
90 views

sed: cut line X and append to top of the file

I would like to be able to cut line X and insert the line X on position y (top). This would help me to be able to reorganize some config files. Text example line number 1 <--- Append line 10 ...
3
votes
2answers
132 views

Replacing strings in file using different values to replace the same pattern

I have such strings in my /etc/mail/virtusertable mail1@example.com error:nouser Account temporary disabled mail2@example.com error:nouser Account temporary disabled mail3@example.com error:...
2
votes
4answers
86 views

Remove partial duplicates consecutive lines but keep first and last

I have log files with a time stamp and six values in each line i want to reduce the amount of data, by removing consecutive lines with the same values (ignoring time stamps) and keeping the first and ...
1
vote
1answer
270 views

Find and delete lines containing multiple patterns in a file

I have a list of strings and want to find and delete lines containing these strings in a file. A short example of the list of strings is listed as below. File S 1Mo 32,33 2Mo 7,26 2Mo 7,27 2Mo 7,28 ...
1
vote
0answers
168 views

Why is sed changing permissions of a file on a cifs mounted share?

I have an Arch linux machine that uses the following fstab entry to mount a share: //192.168.3.1/Documents /mnt/ cifs credentials=/home/tal/.smbcredentials,rw,x-systemd.automount,iocharset=...
3
votes
1answer
103 views

sed -e 's/^[0-9]//' does not work for the first line

The following is the text I want to parse with sed (Mac OS X 10.11.1 bash) 1 00:25:43,959 --> 00:25:46,502 Here you are, sir. Main level, please. I can delete the first line with sed -e 's/[0-9]/...
3
votes
4answers
155 views

how to use sed when a variable contain '/' in it?

I have the following line to execute in a script and that delete two lines above it. But now my $TEST variable contain values like '/DATA/test10' and my variable ID contain '10'. How can i use this ...
1
vote
1answer
146 views

How can I replace a literal \n with a newline character? (on OS X) [duplicate]

I have a script which pulls something out of my shell history and saves it to a file, but if there is a newline in the history entry it gets returned to me with a literal \n instead of the newline ...
5
votes
5answers
233 views

How to SED these paragraphs to MCQ format?

My data: Question Nr. 311 Main proteins are in the lorem ipsun A Lorem RNA test B Cells C Metoclom D Cells E Muscles Question Nr. 312 Main proteins are in the lorem ipsun A Lorem B Cells C ...
1
vote
4answers
285 views

How can I delete multiple random lines from a text file using sed?

I want to delete 10 random lines from a text file that has 90 lines and then output this to a new file. I've been trying to do this using sed, but I have two problems. I'm using: sed -i $((1 + RANDOM ...
0
votes
2answers
123 views

How to execute shell command produced using echo and sed?

I have tried doing: echo " mv /server/today/logfile1 /nfs/logs/ && gzip /nfs/logs/logfile1" | sed 's|logfile1|logfile2|g' It printed: mv /server/today/logfile2 /nfs/logs/ && gzip /...
3
votes
1answer
34 views

Replace a control character with another control character

A csv file is being generated with a form feed (\f) character. This makes it hard to read in excel, etc. How can I convert the form feed character into a newline (\n) character? All my searches so ...
2
votes
1answer
234 views

How to find and replace using sed text containing a star *

I would like to find and replace text within many .procmailrc files, and using grep and sed. I would like to remove the following line (including the new line break at the beginning) from the ....
0
votes
2answers
23 views

Surround a pattern, not changing it, while searching for it case-insensitively

Sorry if this has been asked before, but I haven't found anything so far. Here's the thing: I have a string and a pattern, and want to surround the pattern inside the string with brackets. While I ...
0
votes
0answers
67 views

“Cannot reallocate” when create file?

Am trying to create excel sheet base on multifiles on a root, I read files line by line append in the final excel sheet. Am trying this shell script on small files and it worked 100% bu when I try it ...
1
vote
3answers
70 views

Is it possible to make sed doing some logic in the replacement part (regex group)

I have few sed command: to extract relevant information My file sample.log (format is ncsa.log) looks like: 2012_04_01_filename.log:29874:192.168.1.12 - - [16/Aug/2012:12:54:21 +0000] "GET /cxf/...
6
votes
2answers
436 views

Is sed \{x,y\} range greedy?

I would like to know if, for instance, \{x,y\} in sed will try to match as much or as little as possible characters. Also, can someone explain to me the bellow unexpected behaviour of sed? echo "...
1
vote
3answers
48 views

How to grep a heading in a manpage containing control characters

I have a manpage, and I want to find something in a specific section. Grep is having a hard time, and I figured out the issue was headings have control characters in them, e.g. SEE ALSO is really: ...
0
votes
1answer
143 views

Delete lines beginning and ending with a digit

I thought this was easy even for a beginner like me, but I'm stuck - piping a text file like this: cat file1.txt | sed '/^[0-9].*[0-9]$/d' > file2.txt This regex catches the lines in a text ...
2
votes
1answer
108 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 ...
1
vote
2answers
166 views

Using awk and grep for iftop results

I wanted to monitor my server network send and receive rate using iftop. This is what works for me: iftop -t -s 1 -n -N | grep 'Total send and receive rate: ' | awk '{print $8}' Mac address and Ips ...
2
votes
3answers
103 views

How can I use (sed|awk) to mask an e-mail address with asterisks like <j******e@g***.com>

How can I mask a list of e-mail addresses like: John Doe <john.doe@gmail.com> John Doe <johndoe@hotmail.net> Jane Doe <j.doe@outlook.org> ... with sed (or awk) into something like:...
1
vote
3answers
161 views

Match lines beginning with the same pattern in two text files

I am now having two text files with some of the lines beginning with the same pattern. For example, File A 1Mo 1,1 I love you. 1Mo 1,2 I like you. Hi 23,3 I am hungry. Hi 23,4 I am foolish. File ...
0
votes
1answer
40 views

How to use the sed command in shell script [closed]

I want to replace the <name>, <place>, and <address> in my input file (for example, letter.txt): To : <name> Address : <address> Place : <place> with the ...
1
vote
1answer
321 views

sed and sudo with string replace

I want to replace a string with sed and save changes to same file which needs sudo as its a root owned file. But while doing that I am getting below error raja@raja-UbuntuVM:~$ sudo sed 's|gateway ...
0
votes
1answer
63 views

sed how to replace from standard input

I have to substitute (search and replace) a password field for files found in different directories. All fields are formatted as: field = value What I have attempted is: grep -i fieldofinterest /...