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

learn more… | top users | synonyms

4
votes
6answers
234 views

Changing matrices into single lines of data

I have multiple matrices in a .txt file and I need each matrix to be in a single line. For example, matrices.txt: 1 2 3 4 2 3 4 5 3 4 5 6 3 4 5 6 2 3 2 5 2 3 4 5 2 3 5 6 2 3 4 5 ... What I would ...
0
votes
5answers
74 views

Appending One File to the Second Until End of File

I have two files. The first is just a single number. This number is calculated and changes every time. The second file has a bunch of columns, and I do not know the number of rows (which can change). ...
1
vote
1answer
139 views

Is sed run as a different user?

I have a sed script that changes some content in /etc/shadow. The actual change isn't important, I will put it just as an example: root@device:~ sed -i 's/root:\(.*\):0:0/root:\1:10:0/' /etc/shadow ...
1
vote
1answer
220 views

sed command to delete variable number of lines

To delete the first 10 lines from a text file, the following command is used: sed -i -e "1,10d" filename what if I want to delete n number of lines, where n is a variable. i.e. I am using ...
1
vote
1answer
158 views

Script to remove certain span elements from an HTML file

I want to write a script that completely removes every instance of a particular span class from a given HTML file. For example, if my undesired span class is "foo" then running my script on the file ...
0
votes
2answers
132 views

sed in while read loop doesn't complete commands

I have a test file called testPage with the following text: || '''Title1''' || '''Title2''' || '''Title3''' || || Cell11 || Cell12 || Cell13 || || Cell21 || Cell22 || || Cell31 || Cell32 || || Cell41 ...
0
votes
1answer
99 views

How to replace \1\ with new line

I have a record like: "evSchema"\1\"UAT" "evSN"\1\"uadb" "evDirsep"\1\"/" "evRootPath"\1\"/work_area/APP_UAT/" "evSchema"\1\"RMS13" "evUser"\1\"STAGE" I want to replace \1\ with new line. I tried ...
0
votes
1answer
74 views

Repeatedly extract every second and third line of a file with 3-line data blocks

I have a file where I have data in 3-line sets. I want the 2nd and 3rd line from each set. How do I get that? For an example from: [set no][no of line in set] 11 12 13 21 22 23 31 32 33 I want: ...
0
votes
2answers
330 views

Find line within file matching pattern and make a substitution

I'm trying to simplify my life here but I'm stuck. I have a file containing multiple lines. Each line has a user and a number. I'd like to know a bash one-liner to search for a user and change the ...
-1
votes
1answer
44 views

sed: regular expression,how to substitute?

I have those line on a text file 36) SIGCANCEL 37) SIGLOST 38) SIGXRES 39) SIGJVM1 40) SIGJVM2 41) SIGRTMIN 42) SIGRTMIN+1 43) SIGRTMIN+2 44) SIGRTMIN+3 45) SIGRTMAX-3 46) SIGRTMAX-2 47) SIGRTMAX-1 ...
4
votes
2answers
438 views

How to change all lines consisting of one “0” character to something else?

I have a file with lines similar to the below: 0 0.1 0.0002 0 0.30 I want to replace the lines with a single 0 to something else like 0.0001. For the lines with a decimal and digits after, I don't ...
2
votes
3answers
479 views

remove lines that contain a specific text in a file

I have a file with a list of emails in it and each line has an email in it. I want to remove lines that contain the string example.com or test.com. I tried this: sed -i 's/example\.com//ig' file....
0
votes
2answers
80 views

Match a word with optional delimiters with sed, grep and PCRE

How do I match a word with optional delimiters? Say I have a file named file a b "a" "a c and I want to match only quoted or unquoted a (I don't want to match "a). I'd like to do such match with ...
-2
votes
2answers
118 views

Grouping input using awk,sed,grep

I have an input file as below: 1001 Ivanov I.I: chess 2021 Petrov P.P: chess, football 3352 Sidorov S.S: 1000 Putin V.V: judo 8773 Schwarzenegger A: judo, chess, football I expect ...
2
votes
2answers
528 views

Extract multiple instances of text between two words inclusive of starting word but exclusive of ending word

A PDB file contains numerous paragraphs of conformations of a protein. Each conformation starts with the keyword ATOM and ends with keyword END. I am trying to read the file in bash such that I ...
2
votes
1answer
103 views

How to combine POSIX character classes of a regex?

I need to combine different character classes in a regular expresion used by sed. I need to match [:word:] and the minus symbol -. How does that look like? All my attempts failed at trying or ...
1
vote
5answers
563 views

How to extract string between two characters with sed [duplicate]

I need some help with sed. I have tried to look for information to do this but I couldn't find any specific and people hardly provide an explanation of the provided solution so this makes difficult to ...
1
vote
1answer
168 views

How to use sed to replace keys in a file using a regex?

I would like to modify a file to replace keys, suppose these keys are in the file as: 42NM 52NM 23NO XNNM I would like to replace anything with NM, with the word "Okay". 1 #!bin/bash/ 2 3 if [ ...
0
votes
1answer
56 views

echo or sed out of a ks-file to generate a mysql_secure script

i will create a KS File (Kickstart for automated OS installation). i install mariadb with the ks file. But it seems a problem to execute the mysql_secure script. My idea is to generate a script in ...
2
votes
3answers
134 views

Join pattern line and consecutive non-empty lines, separated by commas and enclosed in ()

I have a file that has click css_add_violation_false click css_add_claim_false and select css_driver1_birth_date_month birth_date_month select css_driver1_birth_date_day birth_date_day How ...
4
votes
1answer
310 views

POSIX behavior of sed's ^ and $ regex anchors with multiline pattern spaces

What is the POSIX behavior of the sed ^ and $ anchors when used at the beginning and end of a regex if the pattern space contains embedded newlines, like after you use the N command? Should they ...
3
votes
4answers
668 views

sed - how to remove all lines that do not match

I have a html file. I want to remove all lines that do not start with <tr>. I tried: cat my_file | sed $' s/^[^tr].*// ' | sed '/^$/d' but it deleted all the lines.
0
votes
2answers
304 views

AWK, SED or GREP for extracting data from HTML file

Can anyone tell me which is best to use to extract the following data from a html file which was received with curl . <script>document.getElementById("test-summary").innerHTML = "<strong>...
3
votes
2answers
195 views

sed delete lines httpd.conf

I have a httpd config file from which I would like to delete the a whole block: <Directory "/var/www/html"> # # Possible values for the Options directive are "None", "All", # or any combination ...
10
votes
6answers
784 views

Remove lines from a file depending on lines found in another file

File file1.txt contains lines like: /api/purchase/<hash>/index.html For example: /api/purchase/12ab09f46/index.html File file2.csv contains lines like: <hash>,timestamp,ip_address ...
-1
votes
2answers
29 views

How could I use sed to convert a js file with storedVars to a ruby file with methods?

I have lines like: storedVars["css_delete_driver"] = "css=.driver:nth-child(2) *[data-method=delete]"; storedVars["css_delete_driver2_mobile"] = "css=a.remove-driver"; and I want to create a file ...
6
votes
3answers
461 views

Print random lines respecting the order of the source file

I have a large text file (~500K lines) with short sentences (couple of words long). Additionally, there is some XML markup in most of the lines. Finally, the text file has been sorted before the ...
0
votes
2answers
53 views

sed command to append different text at multiple occurrence of string

I want to use sed command to append text at multiple occurrence of string. For Ex: Hello world types="program" Mario types="Game" Hello world types="program" Mario types="Game" So at first ...
0
votes
1answer
128 views

Replace command in Bash script from another script

I'm trying to setup a script that will fetch changes from github where a script has a command that's not executable in Mac OS X. What I'm aiming for is to use sed etc to replace the command within ...
-2
votes
4answers
154 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.
0
votes
2answers
35 views

Prevent sed from printing empty stuff

sed -e 's/\([A-Za-z]*\)\( \)\([A-Za-z]*\)\(:\?\)\([A-Za-z]*\)\?\( \)\?\(.*\)\?/[\1][\2][\3][\4][\5][\6][\7]/' will decorate class VoxelbuilderAdda:public VoxelBuilder So it looks like [class][ ][...
1
vote
3answers
2k views

How to add an attribute in a tag in XML file by command-line?

I am trying to add field at the end of tag using sed script. Suppose I have a tag in XML file: <book name="Sed tutorial" price="250"/> Now I want to add field as Book_Width="A" after end of &...
1
vote
1answer
263 views

Replace LF with CRLF at the beginning of a file

I am writing a script that iterates through all the files and replaces line feeds at the beginning of each file. For a file like this, \n \n A line \r\n Another line \r\n \r \f \n \n Few more lines \...
1
vote
2answers
56 views

join two files based on column when there is no one-to-one correspondence in bash script (awk, grep , sed)

file1.txt 112|9305|/inst.exe 112|9305|/lkj.exe 112|9305|/dje.jar 112|9305|/ind.pdf 112|9306|/ma.exe 112|9306|/ngg.pdf 112|9307|/jhhh.dat 112|9312|/ee.dat 112|9312|/qwq.dll file2.txt 117|9305|www....
3
votes
5answers
501 views

How to use awk or sed to convert rows to columns

I need to transpose a file. Input file: 1/1/1111 1 2 3 4 2/2/2222 5 6 7 8 Output: 1/1/1111 1 2 3 4 2/2/2222 5 6 7 8
1
vote
1answer
58 views

What is the correct way of inserting \u using sed?

I'm trying to find a string that contains \u and adding something to it. For example, $ echo \user | sed 's/\us/\\usX/' - correctly produces \usXer However, if I have the contents in a file and ...
1
vote
4answers
283 views

Replacing a character at a random position using sed?

I'm trying to replace a character in a file at a random position. My file looks something like: aab babab abab I'm trying to replace a random character for 'c'. So the output might look ...
0
votes
2answers
132 views

To add line numbers in every line using shell command?

My file, PSS-A (Primary A) PSS-B (Primary B) PSS-C (Primary C) PSS-D (Primary D) PSS-E (Primary E) PSS-F (Primary F) PSS-G (Primary G) PSS-H (Primary H) PSS-I (Primary I) SPARE (...
1
vote
2answers
10k views

How to remove a column or multiple columns from file using shell command?

My file, ARCHIVE B1_NAME B2_NAME B3_NAME ELEMENT INFO_NAM WERT PROCID -------- -------- -------- -------- -------- -------- ---- ------ ...
1
vote
1answer
21 views

Removing Zeroes before and after data sample

I have several data samples that I need to grop the zeroes before and after the data sample. However there are zeroes between the data sample that I must keep for obvious reasons. How can I do this ...
4
votes
2answers
149 views

Find if any line of a file is a subset of data in another file

I have a file file1.txt with the following data: MIME_HTML_ONLY SUSPICIOUS_RECIPS SORTED_RECIPS HFILTER_HELO_5 and another file file2.txt with lines as: HFILTER_FROM_BOUNCE FORGED_OUTLOOK_HTML ...
2
votes
2answers
297 views

search for exact string in unix

I have a file, xyz.txt, which has one string per line. How do I search xyz.txt for an exact match? I get an input string, eg. defabc, from another app. If defabc is found, it should return true, else ...
1
vote
0answers
35 views

How can I easily deal with differences between BSD and GNU sed? [duplicate]

BSD and GNU sed have some small differences in what arguments they accept. I just learned that I have to use different command lines when using -i: sed -i '' # BSD sed -i # GNU I solved this ...
-1
votes
2answers
118 views

How to remove line break if line character count less than n chars [closed]

Could anyone help me to concatenate the line with next line if the line char count is less than x chars.
0
votes
2answers
1k views

Delete trailing commas in line

How do I delete a set of trailing commas in bash: a,b,c,d,,,, 1,2,3,,,, Desired Output: a,b,c,d 1,2,3 Tried doing this: grep "5628" test.csv | sed 's/,*$//g' but it doesn't work. If it ...
22
votes
9answers
5k views

How to delete every second line from a file?

File: Data inserted into table. Total count 13 No error occurred Data inserted into table. Total count 45 No error occurred Data inserted into table. Total count 14 No error occurred Data inserted ...
-1
votes
1answer
52 views

How to add a word at the first of every lines? [duplicate]

File1: Data will be inserted on Thursday Messages has been sent at 6pm Office hours is from 9am to 6pm All the employees are present Output what I want: DMS Data will be inserted on ...
2
votes
3answers
78 views

Extract specific value from blocks of data

I have the blocks of data which are differentiated by the global Ids as below. I need to extract the last block of data with the highest id value, in the below example, id="82". <gc type="global" ...
1
vote
1answer
82 views

error in awk/sed usage of file manipulation line-by-line [duplicate]

I have a requirement of appending the string " 1" at the end of every line of the file. After the execution of below two commands, an empty file is created. awk '{ print $0 " 1" }' < file.txt > ...
1
vote
3answers
55 views

Adding a pattern to each field of the following line

I need some help here please. I have the following input: MX04A; DMX04A; DMX04A; LMX04A; LMX04A; -17.2; -15.3; -14.3; -13.6; -16.8; -15.4; -16.0; -15.3; LH36A; DLH36A; DLH36A; -11; -117.2; -11; -17.5;...