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

learn more… | top users | synonyms

0
votes
1answer
21 views

sed - how to refer to the outer match from within a block?

For example: I have a piece of sed as follows: 36 sed $' 37 /^waitForVisible/ { 38 s/this/& and that/ 39 s/waitForVisible/expect/ 40 s/)$/).to be_visible\\\n/ 41 } with ...
0
votes
1answer
95 views

Need to capture part of file name into variable

I have bunch of CSV files named like so in a directory: s_m_u_supplemental_fields_account_9_4_2015_11_26_Error.csv s_m_u_supplemental_fields_account_9_4_2015_12_3_Error.csv ...
0
votes
1answer
49 views

Sed command with complex pattern and replacement

I have the following two strings saved as variables: str1='ds-cfg-global-aci: (targetcontrol="2.16.840.1.113730.3.4.2 || 2.16.840.1.113730.3.4.17 || 2.16.840.1.113730.3.4.19 || 1.3.6.1.4.1.4203.1.10....
1
vote
2answers
60 views

Remove last comma within a pattern

If I have the following text within a file (somefile.txt): CREATE TABLE "table_name" ( "id" int(11) unsigned NOT NULL, "some_field" varchar(10), ); CREATE TABLE "another_table" ( "id" int(...
1
vote
1answer
54 views

Remove word after match

Consider the following line of text (taken from a MySQL insert): "description" varchar(255) COLLATE utf8_unicode_ci NOT NULL DEFAULT '', I want to return "description" varchar(255) NOT NULL ...
1
vote
2answers
108 views

Change time format in a file

I have output of a sar commands and the time is not POSIX: Linux 2.6.32-431.29.2.el6.x86_64 (test.server.com) 2015-08-01 _x86_64_ (32 CPU) 12:00:01 AM CPU %usr %nice %sys %...
0
votes
2answers
28 views

Delete multiple lines with text and curly braces

I have a multiple files that contains something like: this is a test1 { test 123 test 456 test 789 } this is a test2 { test 123 test 456 test 789 } this is a test3 { test 123 test 456 test 789 } ...
0
votes
2answers
31 views

Merge labels from lines with duplicate fields

I have a rather large text file(thousands of lines) in following format: 123.123 5 123.123 91 156.456 45 233.185 85 233.185 4 257.448 1 455.456 60 455.456 2 The first column are ...
0
votes
3answers
71 views

print last line of !! command

so I want to be able to print helloworld using the !! command. I tried creating a text file with the text helloworld on it. Then I say cat textFileName It prints helloworld. Then I say !! and it ...
0
votes
3answers
129 views

How to replace contents of each line depending on some formula?

I've been working with bash to get some data output to a multiline file with the data stored in this format for each row: text1 "text2" text3 integer1,integer2 A "space" separates the first four ...
0
votes
1answer
48 views

How do I correctly add line ends with sed? [closed]

I have several sed statements that at one points uses newlines in the output with \n (Actually they are sed's using $' with \\\n's`). However subsequent sed operations that wish to operate on the end ...
8
votes
8answers
939 views

Split text file into lines with fixed number of words

Related, but no satisfactory answers: How can I split a large text file into chunks of 500 words or so? I'm trying to take a text file (http://mattmahoney.net/dc/text8.zip) with > 10^7 words all in ...
2
votes
4answers
410 views

extract url between 2 strings in a file

I have a file in which each line is like this "372"^""^"2015-09-03 06:59:44.475"^"NEW"^"N/A"^""^0^"105592"^"https://example-url.com"^"example-domain < MEN'S ULTRA < UltraSeriesViewAll (18)"^"...
2
votes
3answers
77 views

How to add “wrappers” around methods in source code based on a pattern, using sed, awk, grep and friends

How can I add wrappers to a file based on a pattern? For instance I have the following: ... find(css_policy_form_stage3).click with_ajax_wait expect(css_coverage_type_everquote).to ...
1
vote
1answer
38 views

How to query-replace after a given character in a string?

How to replace all / by . only in the sentence after the = ? For example, from this: /buttress_unit/buttress_top/buttress_i_ps_but_cio_m1_i_but_m1_tx_wrapper_m1_pref_fifo_array_29__stream_id_3_0 = ...
5
votes
8answers
708 views

awk or sed command to match regex at specific line, exit true if success, false otherwise

I need to determine if a file contains a certain regex at a certain line and to return true (exit 0) if found, and otherwise false. Maybe I'm overthinking this, but my attempts proved a tad unwieldy. ...
-1
votes
3answers
62 views

How to delete certain line from file?

I have a text file with line containing only comma. How can I delete this particular line? Consider following example: CREATE TABLE ENTERPRISE.TSDAILYACCTSUMMARY ( TSDAILYACCTID FOR COLUMN ...
-1
votes
1answer
85 views

How to add double quotes to a various strings in a same pattern in a file

I have a file like this CREATE TABLE ENTDTA.$$BAPWD ( PERSID NUMBER(10) DEFAULT 0 NOT NULL , "PASSWORD" CHAR(10) DEFAULT '' NOT NULL , UPDATE_IDENT NUMBER(7, 0) DEFAULT 0 NOT NULL ) ...
0
votes
1answer
113 views

Why is sed coming back with “unterminated `s' command”?

So I have a Bash script which looks like this: #!/bin/bash extentemplate=" exten => `$prevchannel`,n,Queue(`$prevchannelname`)\ exten => `$prevchannel`,n,Hangup()\ \ ;`$channelname`\ exten =&...
-1
votes
1answer
31 views

Look for a particular pattern and do a swap on that line

I have a set of files containing VIEW_NAME FOR COLUMN DNAME , VIEW_OWNER FOR COLUMN DCREATOR , OBJECT_NAME FOR COLUMN ONAME , OBJECT_SCHEMA FOR COLUMN OSCHEMA So, ...
0
votes
1answer
94 views

Why won't my change to the sshd_config file persist? [duplicate]

I am writing a script to modify settings on my Ubuntu server. I have the following line in it: sudo sed 's/PasswordAuthentication yes/PasswordAuthentication no/' /etc/ssh/sshd_config The problem ...
0
votes
1answer
42 views

replacing first link of a text “awk equivalent of sed command”

I wanted to replace the first line of a text with sed it should be like sed -i.bak "1 s/^.*$/"CompoundState\tMethod\tApproach\tS^2\tEnergy\tPath"/" awk.xls but I also need to run it on OSX but it ...
-1
votes
1answer
67 views

Using Sed or awk to put a string at the end of a line

I am trying to place a different sting at the end of each line using. The below works but not when the file is large. x=line # in the file. The data goes to the end with $. Let data=0 Let x=0 while [...
4
votes
2answers
350 views

How to change all strings in python file from snake_case to camelCase in sed

I've tried some sed patterns like this from commandlinefu sed -r "s/('[a-z]+)_([a-z])([a-z]+)/\1\U\2\L\3/" But somehow it's not working. For one thing they forgot the digits, which I can fix, but ...
6
votes
4answers
1k views

How to add a header and/or footer to a sed or awk stream?

I have a bunch of output going through sed and awk. How can I prefix the output with START and suffix the answer with END? For example, if I have All this code on all these lines and all these ...
1
vote
5answers
86 views

Making a *nicely* formatted list of number of files in directories - awk & sed probably

I have lots and lots of files in a series of directories, and would like a nicely formatted list of how many files are in each directory. The directories are ordered thus: Parent directories named A,...
2
votes
1answer
49 views

Question about replacing character in text file

So I have some data that is parsed in this format seperated by colons: username:email@email.com:password:salt Sometimes the username/salt can contain a colon (:) in it. I want to replace the ...
0
votes
0answers
75 views

Sort Checkpoint Firewall log into easier output

I am trying to sort out this log: Aug 27 01:23:38 1.1.1.1/1.1.1.1/1.1.1.1 fw1-loggrabber[0000]: loc=12569|time=2015-08-27 01:23:37|action=block|orig=192.168.1.1|i/f_dir=outbound|i/f_name=eth1-04|...
0
votes
1answer
63 views

Big XML - copy data from nth Occurrences to another file

<XML> <Employee> <firstname></firstname><lastname></lastname> </Employee> <Employee> <firstname></firstname><lastname></lastname>...
1
vote
5answers
216 views

lines to columns with awk

I have to following sample output: <HARDWARE> <NAME>WIN1</NAME> <OS>Windows 7</OS> <IP>1.2.3.4</IP> <DOMAIN>contoso.com</DOMAIN> &...
0
votes
1answer
361 views

SED - Find and replace with special characters (#, , % )

I'm aware that this is a really simple question, but I'm struggling to find a solution to this . I need to automatically find and replace in the /etc/aliases file the following section: # Person ...
3
votes
3answers
138 views

Find a pattern and insert # at the beginning of 2 line befoe that and 1 line after that

I have a big file (more than 2000 lines). Where i have to insert a # at the beginning of 2 above lines and at the beginning of 1 below line after finding a pattern. Also insert # at the beginning of ...
1
vote
2answers
105 views

How to replace string in files except with a line begins with # [duplicate]

I've looked at some other questions related to using sed to replace strings, but I'm having trouble figuring out how to apply it to my particular need. In a case like the snippet below, I want to ...
4
votes
6answers
232 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
121 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
180 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
148 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
130 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
97 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
66 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
316 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
43 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
421 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
415 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
78 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
116 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
513 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
96 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
468 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 ...